mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-04 05:39:57 -05:00
Arbitrary videos can be downloaded. (#63)
* Squashed commit of the following:
commit c527475fcb4d4906457be5c10f0e9c6dee7fedc1
Author: Roland <roland@learningselfreliance.com>
Date: Tue Dec 14 22:16:36 2021 -0700
Only saving a favorite video to the config if it has a source_id.
commit ae8b712ebeaef467b8d5a5b87ca0f4f276373a0d
Author: Roland <roland@learningselfreliance.com>
Date: Tue Dec 14 22:14:26 2021 -0700
Ading description and changing Save button to Download.
commit 03a72be6dfdfa383915950a80e3f1ee9febf6705
Author: Roland <roland@learningselfreliance.com>
Date: Tue Dec 14 22:09:44 2021 -0700
Removing uploader for now.
commit 5d0231284bf84e459687f6ab903b06bf4e3762bd
Author: Roland <roland@learningselfreliance.com>
Date: Tue Dec 14 22:07:17 2021 -0700
Removing unique data from example info.
commit 9cc721a32d4715d9d6433e51db3b35e039c78d68
Author: Roland <roland@learningselfreliance.com>
Date: Tue Dec 14 22:02:33 2021 -0700
Updating downloader tests.
commit 8e193b03349ae4613f105418cd3e8176c7101304
Author: Roland <roland@learningselfreliance.com>
Date: Tue Dec 14 21:53:31 2021 -0700
Toasting error when WROL Mode is enabled. Disabling new downloads during WROL Mode.
commit 48c4311c31db8f2d2c8dd0a65f979a5f0a49a911
Author: Roland <roland@learningselfreliance.com>
Date: Tue Dec 14 21:45:06 2021 -0700
Raising error if download exits without a 0. Clearing kill event when starting a new download.
commit 54ba3a890d59facc837410d34bf7afb40db3b685
Author: Roland <roland@learningselfreliance.com>
Date: Tue Dec 14 21:16:46 2021 -0700
Downloads can be restarted and killed.
commit bd60751262247259c3e9ceb5116e017d6314c219
Author: Roland <roland@learningselfreliance.com>
Date: Mon Dec 13 16:28:18 2021 -0700
Downloads can be started again. Downloads are sorted on Downloads page.
commit ea7f39231b92982ae0b263b76c5bd1902cc8169d
Author: Roland <roland@learningselfreliance.com>
Date: Sat Dec 11 21:24:16 2021 -0700
Building Downloads tab.
commit d3321872ac3743ff6df781b07ef0fb537f1d4c98
Author: Roland <roland@learningselfreliance.com>
Date: Sat Dec 11 20:41:46 2021 -0700
Not scheduling channels withtout a URL/frequency.
commit 84f268a0d4f58c85f86b6377900492e0766f6619
Author: Roland <roland@learningselfreliance.com>
Date: Sat Dec 11 19:56:08 2021 -0700
Channel downloads will be spread out when each channel is created.
commit 26fa6beea38ef3f8d6b7bf218f18222a51a87a60
Author: Roland <roland@learningselfreliance.com>
Date: Sat Dec 11 13:08:11 2021 -0700
Recurring downloads are renewed on startup.
commit fc2ab850533f1c3ad073f4692c7dea2be43e85d6
Author: Roland <roland@learningselfreliance.com>
Date: Fri Dec 10 17:03:57 2021 -0700
Fetching download again after do_download.
commit fdadad327fc09c17a49eb63b11e632fb72cc3195
Author: Roland <roland@learningselfreliance.com>
Date: Fri Dec 10 16:20:51 2021 -0700
Fixing error with test. Mock was not being reset.
commit 070dee238e53377acd22097c63cf3794e68ed423
Author: Roland <roland@learningselfreliance.com>
Date: Fri Dec 10 15:36:39 2021 -0700
Fixed bug in date handling (big surprise).
commit e53a8992268d68620c679455adaec3a590f20602
Author: Roland <roland@learningselfreliance.com>
Date: Wed Dec 1 16:48:36 2021 -0700
Using chunks for regular channel refresh.
commit 3bd60c58d1bef0226325f321978c0434b36b3be5
Author: Roland <roland@learningselfreliance.com>
Date: Wed Dec 1 15:22:02 2021 -0700
Reformatting.
commit 4fdc325f25a32c311ca6543fe321624b2bd564b2
Author: Roland <roland@learningselfreliance.com>
Date: Wed Dec 1 15:13:34 2021 -0700
Creating function that refreshes NO CHANNEL videos.
commit d67feafb6c2994f5a4a1295920a70db369cc6816
Author: Roland <roland@learningselfreliance.com>
Date: Wed Dec 1 14:35:54 2021 -0700
Removing unused functions.
commit 6b3918e4e78fbac64920f85c4de0df873ec366a1
Author: Roland <roland@learningselfreliance.com>
Date: Wed Dec 1 14:31:48 2021 -0700
Removing reporter and events. This will be replaced in the future with some sort of SSE.
commit 54fa1b0f5e93ca25c80b780dcdc3876b700273b1
Author: Roland <roland@learningselfreliance.com>
Date: Sun Nov 28 17:55:43 2021 -0700
Better error handling for archive button.
commit c31d124e511b27f626ed2df71486d47302b396f3
Author: Roland <roland@learningselfreliance.com>
Date: Sun Nov 28 17:29:43 2021 -0700
Displaying View Count in a human readable format.
commit 0ee2bd65588fd93b475cad6144e76f1b45191b39
Author: Roland <roland@learningselfreliance.com>
Date: Sat Nov 27 09:29:41 2021 -0700
Fixing tests.
commit e64aff32bab95c311ea3747e1a21b58b99750d5e
Author: Roland <roland@learningselfreliance.com>
Date: Sat Nov 27 08:59:51 2021 -0700
Handling when a Video has no Channel in UI.
commit b9549ecd5aac03c002adca7b7667f0a7189b662a
Author: Roland <roland@learningselfreliance.com>
Date: Fri Nov 26 08:35:36 2021 -0700
Downloaders can raise UnrecoverableDownloadError to signal a downlaod should not be retried.
commit da88798e2584d3e84900a70e34166c8c208c64fb
Author: Roland <roland@learningselfreliance.com>
Date: Wed Nov 24 20:40:29 2021 -0700
Downloaders can refuse a download, which will then be deleted.
commit b865d856121db5ec8a3660c5d3c13b9846fde688
Author: Roland <roland@learningselfreliance.com>
Date: Wed Nov 24 19:35:43 2021 -0700
Removing relative path handling.
commit 6d7757870b02ba030d78b3ef7e43f28b906eea61
Author: Roland <roland@learningselfreliance.com>
Date: Wed Nov 24 19:28:27 2021 -0700
Using __json__ for Video and Channel models.
commit 649e4d6a61608272759d1e7ee02b490bb5c20b3e
Author: Roland <roland@learningselfreliance.com>
Date: Wed Nov 24 18:23:38 2021 -0700
Fixing video links in UI.
commit 9d081af12742070b8519bd752812130cb5dbe393
Author: Roland <roland@learningselfreliance.com>
Date: Wed Nov 24 14:42:38 2021 -0700
Fixing docs.
commit 1202cbe0f9ae744d7801b53a5071cded8ea39924
Author: Roland <roland@learningselfreliance.com>
Date: Wed Nov 24 14:40:36 2021 -0700
Fixing wrolpi tests.
commit 3f6af8cc1123f2f63a3fc0f21ec4d994c642cf13
Author: Roland <roland@learningselfreliance.com>
Date: Wed Nov 24 14:13:35 2021 -0700
Fixing tests to use MediaPath.
commit faec04f2a5e4c267a502c03884940a9c5bcfe387
Author: Roland <roland@learningselfreliance.com>
Date: Fri Nov 19 10:21:20 2021 -0700
Testing when a video has no channel.
commit cbf22aad7a0b72e61685e0236e843554b57c4af1
Author: Roland <roland@learningselfreliance.com>
Date: Thu Nov 18 16:24:03 2021 -0700
Using MediaPath for Video and Channel models. Testing VideoDownloader.do_download.
commit 7916ed61e43439e6cd60f23ce3fdf6c1a75bf02d
Author: Roland <roland@learningselfreliance.com>
Date: Thu Nov 18 12:43:44 2021 -0700
Channels/Videos downloaders can get gotten.
commit 808ef796cec01db6e7009065f9e26d25d9500ce2
Author: Roland <roland@learningselfreliance.com>
Date: Wed Nov 17 21:45:19 2021 -0700
Upgrading to Sanic 21.9.1.
commit ea32813be28e78393ac456ee782a7245c098188f
Author: Roland <roland@learningselfreliance.com>
Date: Wed Nov 17 20:29:01 2021 -0700
Creating Downloader and DownloaderManager classes which handle downloading URLs.
commit e859a77a77277ddf8b6e221ec9c9383c8ab91d66
Author: Roland <roland@learningselfreliance.com>
Date: Wed Nov 17 12:41:39 2021 -0700
Creating UI segement which allows the download/upload of media.
* Displaying all recurring downloads.
* Not downloading channels with empty url.
* Showing positive row for recurring downloads.
* Removing default last_successful_download.
* Only displaying Last Successful Download when there is one.
* Refreshing videos before finding missing videos.
* Using short video path name for log.
* Removing download history feature in favor of new Downloads page.
* Only showing info date if it exists.
* Making get_or_create_channel more reliable.
* Allowing null for channel download frequency.
* Logging when searching for channel.
* Storing channel's source_id in config.
* Importing using link instead of name.
* Getting channel id when importing channels.
* Removing redunant parentheses from repr.
* Preserving database channel values if there are not values in the config.
* Only highlighting pending downloads.
* Deleting old downloads periodically.
* Sorting recurring by next_download/frequency.
* Creating fixtures for channels, media directory and db session.
* Removing unused function. Wrapping config for tests.
* Deleting channel download when deleting a channel.
* Testing that a channel's download is deleted when the channel is deleted.
* Updating youtube-dl
* Switching from youtube-dl in favor of yt-dlp.
* Sorting once-downlaods by last_successful_download then id.
* Adding special handling to video_url_resolver for full urls.
* Consolidating downloader methods and functions. Reversing once-download sort order so the most recently downloaded are at the top.
* Getting session if one is not provided.
* Adding test for create_downloads.
* Not passing format option when getting filename.
* Linting
Signed-off-by: Roland <roland@learningselfreliance.com>
This commit is contained in:
parent
2fff678aee
commit
46796f783a
61 changed files with 3347 additions and 1525 deletions
|
|
@ -1,6 +1,31 @@
|
|||
from abc import ABC
|
||||
|
||||
from wrolpi.common import logger
|
||||
from wrolpi.downloader import Downloader, Download
|
||||
from wrolpi.errors import UnrecoverableDownloadError
|
||||
from . import lib
|
||||
from .api import bp # noqa
|
||||
|
||||
PRETTY_NAME = 'Archive'
|
||||
|
||||
logger = logger.getChild(__name__)
|
||||
|
||||
|
||||
class ArchiveDownloader(Downloader, ABC):
|
||||
@staticmethod
|
||||
def valid_url(url: str) -> bool:
|
||||
"""
|
||||
Archiver will attempt to archive anything, so it should be last!
|
||||
"""
|
||||
return True
|
||||
|
||||
def do_download(self, download: Download):
|
||||
if download.attempts > 3:
|
||||
raise UnrecoverableDownloadError(f'Max download attempts reached for {download.url}')
|
||||
|
||||
lib.new_archive(download.url, sync=True)
|
||||
return True
|
||||
|
||||
|
||||
# Archive downloader is the last downloader which should be used.
|
||||
archive_downloader = ArchiveDownloader(priority=100)
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ async def search_archives(_: Request, data: dict):
|
|||
return json_response(ret)
|
||||
|
||||
|
||||
@bp.post(':refresh')
|
||||
@bp.post('/refresh')
|
||||
@wrol_mode_check
|
||||
async def refresh_archives(_: Request):
|
||||
asyncio.ensure_future(lib.refresh_archives())
|
||||
|
|
|
|||
|
|
@ -6,17 +6,17 @@ import pathlib
|
|||
import re
|
||||
from itertools import groupby
|
||||
from typing import Iterator
|
||||
from urllib.parse import urlparse
|
||||
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from modules.archive.models import URL, Domain, Archive
|
||||
from wrolpi.common import get_media_directory, logger, chunks
|
||||
from wrolpi.common import get_media_directory, logger, chunks, extract_domain
|
||||
from wrolpi.dates import now, strptime_ms, strftime_ms
|
||||
from wrolpi.db import get_db_session, get_db_curs
|
||||
from wrolpi.errors import InvalidDomain, UnknownURL, PendingArchive, InvalidArchive
|
||||
from wrolpi.vars import PYTEST
|
||||
|
||||
logger = logger.getChild(__name__)
|
||||
|
||||
|
|
@ -27,21 +27,6 @@ def get_archive_directory() -> pathlib.Path:
|
|||
return get_media_directory() / 'archive'
|
||||
|
||||
|
||||
def extract_domain(url):
|
||||
"""
|
||||
Extract the domain from a URL. Remove leading www.
|
||||
|
||||
>>> extract_domain('https://www.example.com/foo')
|
||||
'example.com'
|
||||
"""
|
||||
parsed = urlparse(url)
|
||||
domain = parsed.netloc
|
||||
if domain.startswith('www.'):
|
||||
# Remove leading www.
|
||||
domain = domain[4:]
|
||||
return domain
|
||||
|
||||
|
||||
def get_domain_directory(url: str) -> pathlib.Path:
|
||||
"""
|
||||
Get the archive directory for a particular domain.
|
||||
|
|
@ -161,7 +146,7 @@ def new_archive(url: str, sync: bool = False):
|
|||
url_.latest_id = archive_id
|
||||
url_.latest_datetime = now()
|
||||
|
||||
if sync:
|
||||
if PYTEST or sync:
|
||||
return _do_archive(url, archive_id)
|
||||
else:
|
||||
# Run the real archive process in the future.
|
||||
|
|
|
|||
|
|
@ -312,7 +312,7 @@ class TestArchive(TestAPI):
|
|||
archive: Archive = session.query(Archive).one()
|
||||
self.assertEqual(url.latest, archive)
|
||||
# latest_datetime is set, the timestamp in the file name is assumed to be UTC.
|
||||
self.assertEqual(str(url.latest_datetime), '2021-10-05 22:20:10.346823-06:00')
|
||||
self.assertEqual(str(url.latest_datetime), '2021-10-05 16:20:10.346823-06:00')
|
||||
|
||||
# Running the refresh does not result in a new archive.
|
||||
_refresh_archives()
|
||||
|
|
|
|||
|
|
@ -27,8 +27,8 @@ class Item(Base, ModelHelper):
|
|||
inventory = relationship('Inventory', primaryjoin="Item.inventory_id==Inventory.id", back_populates='items')
|
||||
|
||||
def __repr__(self):
|
||||
return f'<Item(id={self.id}, name={self.name}, brand={self.brand}, ' \
|
||||
f'count={self.count}, item_size={self.item_size}, unit={self.unit}, inventory={self.inventory_id})>'
|
||||
return f'<Item id={self.id}, name={self.name}, brand={self.brand}, ' \
|
||||
f'count={self.count}, item_size={self.item_size}, unit={self.unit}, inventory={self.inventory_id} >'
|
||||
|
||||
|
||||
class Inventory(Base, ModelHelper):
|
||||
|
|
@ -43,7 +43,7 @@ class Inventory(Base, ModelHelper):
|
|||
items: InstrumentedList = relationship('Item', foreign_keys='Item.inventory_id')
|
||||
|
||||
def __repr__(self):
|
||||
return f'<Inventory(id={self.id}, name={self.name!r}, deleted={self.deleted_at})>'
|
||||
return f'<Inventory id={self.id}, name={self.name!r}, deleted={self.deleted_at} >'
|
||||
|
||||
def dict(self):
|
||||
d = super().dict()
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
from modules.videos import downloader
|
||||
|
|
@ -1,28 +1,25 @@
|
|||
import asyncio
|
||||
import random
|
||||
from functools import wraps
|
||||
from http import HTTPStatus
|
||||
from multiprocessing import Event
|
||||
|
||||
import requests
|
||||
from sanic import Blueprint, response
|
||||
from sanic.request import Request
|
||||
|
||||
from wrolpi import before_startup
|
||||
from wrolpi.common import create_websocket_feed, get_sanic_url, \
|
||||
wrol_mode_check, ProgressReporter, get_config
|
||||
wrol_mode_check
|
||||
from wrolpi.common import logger
|
||||
from wrolpi.root_api import add_blueprint, json_response
|
||||
from wrolpi.schema import validate_doc, JSONErrorResponse
|
||||
from .channel.api import channel_bp
|
||||
from .downloader import update_channels, download_all_missing_videos
|
||||
from .lib import process_video_meta_data, _refresh_videos, get_statistics, get_download_history
|
||||
from .channel.lib import download_channel, spread_channel_downloads
|
||||
from .lib import _refresh_videos, get_statistics
|
||||
from .schema import StreamResponse, \
|
||||
FavoriteRequest, FavoriteResponse, VideosStatisticsResponse, DownloadHistoryResponse
|
||||
FavoriteRequest, FavoriteResponse, VideosStatisticsResponse
|
||||
from .video.api import video_bp
|
||||
from .video.lib import set_video_favorite
|
||||
|
||||
content_bp = Blueprint('Video Content', '/api/videos')
|
||||
content_bp = Blueprint('VideoContent', '/api/videos')
|
||||
bp = Blueprint('Videos', '/api/videos').group(
|
||||
content_bp, # view and manage video content and settings
|
||||
channel_bp, # view and manage channels
|
||||
|
|
@ -35,8 +32,8 @@ logger = logger.getChild(__name__)
|
|||
refresh_queue, refresh_event = create_websocket_feed('refresh', '/feeds/refresh', content_bp)
|
||||
|
||||
|
||||
@content_bp.post(':refresh')
|
||||
@content_bp.post(':refresh/<link:string>')
|
||||
@content_bp.post('/refresh')
|
||||
@content_bp.post('/refresh/<link:str>')
|
||||
@validate_doc(
|
||||
summary='Search for videos that have previously been downloaded and stored.',
|
||||
produces=StreamResponse,
|
||||
|
|
@ -63,8 +60,7 @@ async def refresh(_, link: str = None):
|
|||
await refresh_videos(channel_links)
|
||||
|
||||
refresh_logger.info('refresh complete')
|
||||
except Exception as e:
|
||||
refresh_queue.put({'error': 'Refresh failed. See server logs.', 'message': str(e)})
|
||||
except Exception:
|
||||
raise
|
||||
finally:
|
||||
refresh_event.clear()
|
||||
|
|
@ -78,8 +74,8 @@ async def refresh(_, link: str = None):
|
|||
download_queue, download_event = create_websocket_feed('download', '/feeds/download', content_bp)
|
||||
|
||||
|
||||
@content_bp.post(':download')
|
||||
@content_bp.post(':download/<link:string>')
|
||||
@content_bp.post('/download')
|
||||
@content_bp.post('/download/<link:str>')
|
||||
@validate_doc(
|
||||
summary='Update channel catalogs, download any missing videos',
|
||||
produces=StreamResponse,
|
||||
|
|
@ -88,7 +84,7 @@ download_queue, download_event = create_websocket_feed('download', '/feeds/downl
|
|||
],
|
||||
)
|
||||
@wrol_mode_check
|
||||
async def download(_, link: str = None):
|
||||
def download(_, link: str = None):
|
||||
download_logger = logger.getChild('download')
|
||||
|
||||
stream_url = get_sanic_url(scheme='ws', path='/api/videos/feeds/download')
|
||||
|
|
@ -98,44 +94,18 @@ async def download(_, link: str = None):
|
|||
if refresh_event.is_set():
|
||||
return response.json({'error': 'Refresh is running. Cannot download.'})
|
||||
|
||||
download_event.set()
|
||||
download_channel(link)
|
||||
|
||||
async def do_download():
|
||||
reporter = ProgressReporter(download_queue, 2)
|
||||
reporter.set_progress_total(0, 1)
|
||||
reporter.send_progress(0, 1, 'Download started')
|
||||
|
||||
try:
|
||||
update_channels(reporter, link)
|
||||
download_logger.info('Updated all channel catalogs')
|
||||
download_all_missing_videos(reporter, link)
|
||||
reporter.finish(1, 'All videos have been downloaded')
|
||||
|
||||
# Fill in any missing data for all videos.
|
||||
reporter.message(0, 'Processing and cleaning files')
|
||||
process_video_meta_data()
|
||||
reporter.finish(0, 'Processing and cleaning complete')
|
||||
|
||||
download_logger.info('download complete')
|
||||
except Exception as e:
|
||||
logger.fatal(f'Download failed: {e}')
|
||||
reporter.error(0, 'Download failed. See server logs.')
|
||||
raise
|
||||
finally:
|
||||
download_event.clear()
|
||||
|
||||
coro = do_download()
|
||||
asyncio.ensure_future(coro)
|
||||
download_logger.debug('do_download scheduled')
|
||||
return response.json({'code': 'stream-started', 'stream_url': stream_url})
|
||||
|
||||
|
||||
@wraps(_refresh_videos)
|
||||
async def refresh_videos(channel_links: list = None):
|
||||
return _refresh_videos(refresh_queue, channel_links=channel_links)
|
||||
return _refresh_videos(channel_links=channel_links)
|
||||
|
||||
|
||||
@content_bp.post(':favorite')
|
||||
@content_bp.post('/favorite')
|
||||
@validate_doc(
|
||||
summary='Toggle the favorite flag on a video',
|
||||
consumes=FavoriteRequest,
|
||||
|
|
@ -160,56 +130,6 @@ async def statistics(_: Request):
|
|||
return json_response(ret, HTTPStatus.OK)
|
||||
|
||||
|
||||
@content_bp.get('/download_history')
|
||||
@validate_doc(
|
||||
summary='Retrieve video download history',
|
||||
produces=DownloadHistoryResponse,
|
||||
)
|
||||
async def download_history(_: Request):
|
||||
history = await get_download_history()
|
||||
ret = dict(history=history)
|
||||
return json_response(ret, HTTPStatus.OK)
|
||||
|
||||
|
||||
MIN_DOWNLOAD_FREQUENCY = 60 * 60 * 4 # 4 hours
|
||||
MAX_DOWNLOAD_FREQUENCY = 60 * 60 * 12 # 12 hours
|
||||
PERIODIC_DOWNLOAD_EVENT = Event()
|
||||
|
||||
|
||||
async def _periodic_download(min_download_frequency: int, max_download_frequency: int):
|
||||
"""
|
||||
Wait some amount of time, download, then schedule the next download.
|
||||
"""
|
||||
sleep_seconds = random.randint(min_download_frequency, max_download_frequency)
|
||||
logger.debug(f'Waiting {sleep_seconds} seconds before next periodic download')
|
||||
await asyncio.sleep(sleep_seconds)
|
||||
|
||||
url = get_sanic_url(path='/api/videos:download')
|
||||
resp = requests.post(url)
|
||||
if resp.status_code != HTTPStatus.OK and resp.status_code != HTTPStatus.CONFLICT:
|
||||
# Download failed and wasn't already running.
|
||||
logger.warning(f'Periodic download failed with status_code={resp.status_code}')
|
||||
logger.warning(f'Periodic download response={resp}')
|
||||
|
||||
# Schedule the next download
|
||||
asyncio.ensure_future(_periodic_download(min_download_frequency, max_download_frequency))
|
||||
|
||||
|
||||
@before_startup
|
||||
def start_periodic_download():
|
||||
"""
|
||||
Periodically download the videos.
|
||||
"""
|
||||
if not PERIODIC_DOWNLOAD_EVENT.is_set():
|
||||
PERIODIC_DOWNLOAD_EVENT.set()
|
||||
logger.info(f'Starting periodic download')
|
||||
|
||||
config = get_config()
|
||||
try:
|
||||
min_download_frequency = int(config.get('min_download_frequency', MIN_DOWNLOAD_FREQUENCY))
|
||||
max_download_frequency = int(config.get('max_download_frequency', MAX_DOWNLOAD_FREQUENCY))
|
||||
except Exception:
|
||||
logger.fatal(f'Failed to get min/max download frequencies from config!', exc_info=True)
|
||||
raise
|
||||
|
||||
asyncio.ensure_future(_periodic_download(min_download_frequency, max_download_frequency))
|
||||
def startup_spread_channel_downloads():
|
||||
spread_channel_downloads()
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
#! /usr/bin/env python3
|
||||
import pathlib
|
||||
from pathlib import Path
|
||||
from typing import Generator, List, Union
|
||||
|
||||
|
|
@ -7,8 +8,7 @@ import webvtt
|
|||
|
||||
from wrolpi.common import logger, chunks
|
||||
from wrolpi.db import get_db_session
|
||||
from wrolpi.errors import UnknownCaptionFile
|
||||
from .common import get_absolute_video_caption
|
||||
from wrolpi.media_path import MediaPath
|
||||
from .models import Video
|
||||
|
||||
|
||||
|
|
@ -43,9 +43,11 @@ def process_captions(video: Video):
|
|||
"""
|
||||
Parse and insert captions for a video record.
|
||||
"""
|
||||
caption_path = get_absolute_video_caption(video)
|
||||
if not caption_path.exists():
|
||||
raise UnknownCaptionFile()
|
||||
if isinstance(video.caption_path, MediaPath):
|
||||
caption_path = video.caption_path.path
|
||||
else:
|
||||
caption_path = pathlib.Path(video.caption_path)
|
||||
|
||||
try:
|
||||
lines = get_unique_caption_lines(str(caption_path))
|
||||
block = '\n'.join(lines)
|
||||
|
|
|
|||
|
|
@ -5,8 +5,8 @@ from sanic import response, Blueprint
|
|||
from sanic.request import Request
|
||||
|
||||
from wrolpi.common import logger, wrol_mode_check, get_relative_to_media_directory, make_media_directory
|
||||
from wrolpi.root_api import json_response
|
||||
from wrolpi.errors import UnknownDirectory
|
||||
from wrolpi.root_api import json_response
|
||||
from wrolpi.schema import validate_doc, JSONErrorResponse
|
||||
from .lib import get_minimal_channels, delete_channel, update_channel, get_channel, create_channel
|
||||
from ..schema import ChannelsResponse, ChannelResponse, ChannelPostRequest, \
|
||||
|
|
@ -27,7 +27,7 @@ async def get_channels(_: Request):
|
|||
return json_response({'channels': channels})
|
||||
|
||||
|
||||
@channel_bp.route('/<link:string>', methods=['GET', 'OPTIONS'])
|
||||
@channel_bp.route('/<link:str>', methods=['GET', 'OPTIONS'])
|
||||
@validate_doc(
|
||||
summary='Get a Channel',
|
||||
produces=ChannelResponse,
|
||||
|
|
@ -36,7 +36,7 @@ async def get_channels(_: Request):
|
|||
),
|
||||
)
|
||||
def channel_get(_: Request, link: str):
|
||||
channel = get_channel(link)
|
||||
channel = get_channel(link=link)
|
||||
channel.pop('info_json')
|
||||
return json_response({'channel': channel})
|
||||
|
||||
|
|
@ -61,6 +61,15 @@ def channel_post(_: Request, data: dict):
|
|||
else:
|
||||
raise
|
||||
|
||||
if download_frequency := data.get('download_frequency'):
|
||||
try:
|
||||
download_frequency = int(download_frequency)
|
||||
except ValueError:
|
||||
if download_frequency in ('null', 'None'):
|
||||
download_frequency = None
|
||||
|
||||
data['download_frequency'] = download_frequency
|
||||
|
||||
channel = create_channel(data)
|
||||
|
||||
# Refresh the videos asynchronously
|
||||
|
|
@ -72,12 +81,11 @@ def channel_post(_: Request, data: dict):
|
|||
{'Location': f'/api/videos/channels/{channel["link"]}'})
|
||||
|
||||
|
||||
@channel_bp.put('/<link:string>')
|
||||
@channel_bp.patch('/<link:string>')
|
||||
@channel_bp.put('/<link:str>')
|
||||
@channel_bp.patch('/<link:str>')
|
||||
@validate_doc(
|
||||
summary='Update a Channel',
|
||||
consumes=ChannelPutRequest,
|
||||
produces=SuccessResponse,
|
||||
responses=(
|
||||
(HTTPStatus.NOT_FOUND, JSONErrorResponse),
|
||||
(HTTPStatus.BAD_REQUEST, JSONErrorResponse),
|
||||
|
|
@ -90,7 +98,7 @@ def channel_update(_: Request, link: str, data: dict):
|
|||
headers={'Location': f'/api/videos/channels/{channel.link}'})
|
||||
|
||||
|
||||
@channel_bp.delete('/<link:string>')
|
||||
@channel_bp.delete('/<link:str>')
|
||||
@validate_doc(
|
||||
summary='Delete a Channel',
|
||||
produces=SuccessResponse,
|
||||
|
|
|
|||
|
|
@ -1,16 +1,19 @@
|
|||
from typing import List, Dict
|
||||
from collections import defaultdict
|
||||
from datetime import timedelta
|
||||
from typing import List, Dict, Union
|
||||
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
|
||||
from wrolpi.common import sanitize_link, run_after, get_relative_to_media_directory, make_media_directory
|
||||
from wrolpi.common import sanitize_link, run_after, get_relative_to_media_directory, make_media_directory, logger
|
||||
from wrolpi.dates import today
|
||||
from wrolpi.db import get_db_session, get_db_curs
|
||||
from wrolpi.downloader import download_manager, Download
|
||||
from wrolpi.errors import UnknownChannel, UnknownDirectory, APIError, ValidationError
|
||||
from ..common import check_for_channel_conflicts
|
||||
from ..lib import save_channels_config
|
||||
from ..models import Channel
|
||||
|
||||
DEFAULT_DOWNLOAD_FREQUENCY = 60 * 60 * 24 * 7 # weekly
|
||||
DEFAULT_DOWNLOAD_TIMEOUT = 60.0 * 10.0 # Ten minutes
|
||||
logger = logger.getChild(__name__)
|
||||
|
||||
|
||||
async def get_minimal_channels() -> List[dict]:
|
||||
|
|
@ -21,7 +24,7 @@ async def get_minimal_channels() -> List[dict]:
|
|||
# Get all channels, even if they don't have videos.
|
||||
stmt = '''
|
||||
SELECT
|
||||
c.id, name, link, directory, url, download_frequency, info_date, next_download
|
||||
c.id, name, link, directory, url, download_frequency, info_date
|
||||
FROM
|
||||
channel AS c
|
||||
ORDER BY LOWER(name)
|
||||
|
|
@ -64,15 +67,69 @@ async def get_channels_video_count() -> Dict[int, int]:
|
|||
return video_counts
|
||||
|
||||
|
||||
@run_after(save_channels_config)
|
||||
def delete_channel(link):
|
||||
with get_db_session(commit=True) as session:
|
||||
try:
|
||||
channel = session.query(Channel).filter_by(link=link).one()
|
||||
except NoResultFound:
|
||||
raise UnknownChannel()
|
||||
def get_channel(source_id: str = None, link: str = None, url: str = None, return_dict: bool = True
|
||||
) -> Union[dict, Channel]:
|
||||
"""
|
||||
Attempt to find a Channel using the provided params. The params are in order of reliability.
|
||||
|
||||
channel.delete_with_videos()
|
||||
Raises UnknownChannel if no channel is found.
|
||||
"""
|
||||
with get_db_session() as session:
|
||||
# Try by source_id first.
|
||||
channel = None
|
||||
if source_id:
|
||||
channel = session.query(Channel).filter_by(source_id=source_id).one_or_none()
|
||||
if not channel and link:
|
||||
channel = session.query(Channel).filter_by(link=link).one_or_none()
|
||||
if not channel and url:
|
||||
channel = session.query(Channel).filter_by(url=url).one_or_none()
|
||||
|
||||
if not channel:
|
||||
raise UnknownChannel(f'No channel matches {link=} {source_id=} {url=}')
|
||||
|
||||
logger.debug(f'Found {channel=} using {source_id=} {link=} {url=}')
|
||||
return channel.dict() if return_dict else channel
|
||||
|
||||
|
||||
def _spread_by_frequency(channels: List[Channel]):
|
||||
channels_by_frequency = defaultdict(lambda: [])
|
||||
for channel in channels:
|
||||
channels_by_frequency[channel.download_frequency].append(channel)
|
||||
|
||||
for frequency, channels in channels_by_frequency.items():
|
||||
# The seconds between each download.
|
||||
chunk = frequency // len(channels)
|
||||
for channel in channels:
|
||||
# My position in the channel group.
|
||||
index = channels.index(channel)
|
||||
# My next download will be distributed by my frequency and my position.
|
||||
position = chunk * index
|
||||
next_download = today() + timedelta(seconds=frequency + position)
|
||||
yield dict(url=channel.url, frequency=frequency, next_download=next_download)
|
||||
|
||||
|
||||
def spread_channel_downloads():
|
||||
"""
|
||||
Channels should be downloaded in a manner that is spread out over their frequency. For example, three channels
|
||||
with a frequency of a week should be downloaded on different days that week.
|
||||
"""
|
||||
with get_db_session(commit=True) as session:
|
||||
channels = list(session.query(Channel).filter(
|
||||
Channel.url != None,
|
||||
Channel.url != '',
|
||||
Channel.download_frequency != None
|
||||
).order_by(Channel.link).all()) # noqa
|
||||
|
||||
url_next_download = _spread_by_frequency(channels)
|
||||
|
||||
for info in url_next_download:
|
||||
download = download_manager.get_download(session, url=info['url'])
|
||||
if not download:
|
||||
download = Download(url=info['url'])
|
||||
session.add(download)
|
||||
|
||||
download.frequency = info['frequency']
|
||||
download.next_download = info['next_download']
|
||||
|
||||
|
||||
@run_after(save_channels_config)
|
||||
|
|
@ -101,7 +158,10 @@ def update_channel(data, link):
|
|||
try:
|
||||
data['download_frequency'] = int(data['download_frequency'])
|
||||
except ValueError:
|
||||
raise APIError('Invalid download frequency')
|
||||
if data['download_frequency'] in ('null', 'None', ''):
|
||||
data['download_frequency'] = None
|
||||
else:
|
||||
raise APIError(f'Invalid download frequency {data["download_frequency"]}')
|
||||
|
||||
if data.get('match_regex') in ('None', 'null'):
|
||||
data['match_regex'] = None
|
||||
|
|
@ -119,35 +179,28 @@ def update_channel(data, link):
|
|||
# Apply the changes now that we've OK'd them
|
||||
channel.update(data)
|
||||
|
||||
spread_channel_downloads()
|
||||
|
||||
return channel
|
||||
|
||||
|
||||
def get_channel(link) -> dict:
|
||||
"""
|
||||
Get a Channel by it's `link`. Raise UnknownChannel if it does not exist.
|
||||
"""
|
||||
with get_db_session() as session:
|
||||
try:
|
||||
channel = session.query(Channel).filter_by(link=link).one()
|
||||
except NoResultFound:
|
||||
raise UnknownChannel()
|
||||
return channel.dict()
|
||||
|
||||
|
||||
@run_after(save_channels_config)
|
||||
def create_channel(data: dict) -> dict:
|
||||
@run_after(spread_channel_downloads)
|
||||
def create_channel(data: dict, return_dict: bool = True) -> Union[Channel, dict]:
|
||||
"""
|
||||
Create a new Channel. Check for conflicts with existing Channels.
|
||||
"""
|
||||
with get_db_session(commit=True) as session:
|
||||
link = sanitize_link(data['name'])
|
||||
try:
|
||||
# Verify that the URL/Name/Link aren't taken
|
||||
check_for_channel_conflicts(
|
||||
session,
|
||||
url=data.get('url'),
|
||||
name=data['name'],
|
||||
link=sanitize_link(data['name']),
|
||||
link=link,
|
||||
directory=str(data['directory']),
|
||||
source_id=data.get('source_id'),
|
||||
)
|
||||
except APIError as e:
|
||||
raise ValidationError() from e
|
||||
|
|
@ -156,13 +209,34 @@ def create_channel(data: dict) -> dict:
|
|||
name=data['name'],
|
||||
url=data.get('url'),
|
||||
match_regex=data.get('match_regex'),
|
||||
link=sanitize_link(data['name']),
|
||||
link=link,
|
||||
directory=str(data['directory']),
|
||||
download_frequency=data.get('download_frequency', DEFAULT_DOWNLOAD_FREQUENCY),
|
||||
download_frequency=data.get('download_frequency'),
|
||||
source_id=data.get('source_id'),
|
||||
)
|
||||
session.add(channel)
|
||||
session.commit()
|
||||
session.flush()
|
||||
session.refresh(channel)
|
||||
|
||||
return channel.dict()
|
||||
if return_dict:
|
||||
return channel.dict()
|
||||
else:
|
||||
return channel
|
||||
|
||||
|
||||
@run_after(save_channels_config)
|
||||
def delete_channel(link):
|
||||
with get_db_session(commit=True) as session:
|
||||
try:
|
||||
channel = session.query(Channel).filter_by(link=link).one()
|
||||
except NoResultFound:
|
||||
raise UnknownChannel()
|
||||
|
||||
channel.delete_with_videos()
|
||||
|
||||
|
||||
def download_channel(link: str):
|
||||
channel = get_channel(link=link, return_dict=False)
|
||||
with get_db_session(commit=True) as session:
|
||||
download_manager.create_download(channel.url, session)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import json
|
||||
import os
|
||||
import pathlib
|
||||
import tempfile
|
||||
from datetime import timedelta
|
||||
|
|
@ -9,14 +10,16 @@ import mock
|
|||
import pytest
|
||||
|
||||
from modules.videos.api import refresh_queue
|
||||
from modules.videos.channel.lib import spread_channel_downloads, delete_channel
|
||||
from modules.videos.lib import upsert_video
|
||||
from modules.videos.models import Channel, Video
|
||||
from modules.videos.test.common import create_channel_structure
|
||||
from wrolpi.dates import now
|
||||
from wrolpi.db import get_db_session
|
||||
from wrolpi.downloader import download_manager, Download
|
||||
from wrolpi.errors import UnknownFile
|
||||
from wrolpi.root_api import api_app
|
||||
from wrolpi.test.common import wrap_test_db, get_all_messages_in_queue, TestAPI
|
||||
from wrolpi.test.common import wrap_test_db, TestAPI
|
||||
|
||||
|
||||
class TestVideoAPI(TestAPI):
|
||||
|
|
@ -39,83 +42,83 @@ class TestVideoAPI(TestAPI):
|
|||
|
||||
@wrap_test_db
|
||||
def test_channel(self):
|
||||
with tempfile.TemporaryDirectory() as media_dir, \
|
||||
mock.patch('wrolpi.common.get_media_directory', lambda: pathlib.Path(media_dir)):
|
||||
channel_directory = tempfile.TemporaryDirectory(dir=media_dir).name
|
||||
pathlib.Path(channel_directory).mkdir()
|
||||
new_channel = dict(
|
||||
directory=channel_directory,
|
||||
match_regex='asdf',
|
||||
name='Example Channel 1',
|
||||
url='https://example.com/channel1',
|
||||
)
|
||||
channel_directory = tempfile.TemporaryDirectory(dir=self.tmp_dir.name).name
|
||||
channel_directory_name = channel_directory.split('/')[-1]
|
||||
pathlib.Path(channel_directory).mkdir()
|
||||
|
||||
# Channel doesn't exist
|
||||
request, response = api_app.test_client.get('/api/videos/channels/examplechannel1')
|
||||
assert response.status_code == HTTPStatus.NOT_FOUND, f'Channel exists: {response.json}'
|
||||
new_channel = dict(
|
||||
directory=channel_directory,
|
||||
match_regex='asdf',
|
||||
name='Example Channel 1',
|
||||
url='https://example.com/channel1',
|
||||
)
|
||||
|
||||
# Create it
|
||||
request, response = api_app.test_client.post('/api/videos/channels', data=json.dumps(new_channel))
|
||||
assert response.status_code == HTTPStatus.CREATED
|
||||
location = response.headers['Location']
|
||||
request, response = api_app.test_client.get(location)
|
||||
created = response.json['channel']
|
||||
self.assertIsNotNone(created)
|
||||
self.assertIsNotNone(created['id'])
|
||||
# Channel doesn't exist
|
||||
request, response = api_app.test_client.get('/api/videos/channels/examplechannel1')
|
||||
assert response.status_code == HTTPStatus.NOT_FOUND, f'Channel exists: {response.json}'
|
||||
|
||||
# Channel directory should be relative to the media directory
|
||||
assert not pathlib.Path(created['directory']).is_absolute(), \
|
||||
f'Channel directory is absolute: {created["directory"]}'
|
||||
# Create it
|
||||
request, response = api_app.test_client.post('/api/videos/channels', data=json.dumps(new_channel))
|
||||
assert response.status_code == HTTPStatus.CREATED
|
||||
location = response.headers['Location']
|
||||
request, response = api_app.test_client.get(location)
|
||||
created = response.json['channel']
|
||||
self.assertIsNotNone(created)
|
||||
self.assertIsNotNone(created['id'])
|
||||
|
||||
# Get the link that was computed
|
||||
new_channel['link'] = response.json['channel']['link']
|
||||
assert new_channel['link']
|
||||
# Channel directory should be relative to the media directory
|
||||
assert not pathlib.Path(created['directory']).is_absolute(), \
|
||||
f'Channel directory is absolute: {created["directory"]}'
|
||||
|
||||
# Can't create it again
|
||||
request, response = api_app.test_client.post('/api/videos/channels', data=json.dumps(new_channel))
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST
|
||||
# Get the link that was computed
|
||||
new_channel['link'] = response.json['channel']['link']
|
||||
assert new_channel['link']
|
||||
|
||||
# Update it
|
||||
new_channel['name'] = 'Example Channel 2'
|
||||
new_channel['directory'] = str(new_channel['directory'])
|
||||
request, response = api_app.test_client.put(location, data=json.dumps(new_channel))
|
||||
assert response.status_code == HTTPStatus.NO_CONTENT, response.status_code
|
||||
request, response = api_app.test_client.get(location)
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
self.assertDictContains(response.json['channel'], {
|
||||
'id': 1,
|
||||
'name': 'Example Channel 2',
|
||||
'directory': channel_directory,
|
||||
'match_regex': 'asdf',
|
||||
'url': 'https://example.com/channel1',
|
||||
})
|
||||
# Can't create it again
|
||||
request, response = api_app.test_client.post('/api/videos/channels', data=json.dumps(new_channel))
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST
|
||||
|
||||
# Patch it
|
||||
patch = {'name': 'new name'}
|
||||
request, response = api_app.test_client.patch(location, data=json.dumps(patch))
|
||||
assert response.status_code == HTTPStatus.NO_CONTENT, response.status_code
|
||||
request, response = api_app.test_client.get(location)
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
self.assertDictContains(response.json['channel'], {
|
||||
'id': 1,
|
||||
'name': 'new name',
|
||||
'directory': channel_directory,
|
||||
'match_regex': 'asdf',
|
||||
'url': 'https://example.com/channel1',
|
||||
})
|
||||
# Update it
|
||||
new_channel['name'] = 'Example Channel 2'
|
||||
new_channel['directory'] = str(new_channel['directory'])
|
||||
request, response = api_app.test_client.put(location, data=json.dumps(new_channel))
|
||||
assert response.status_code == HTTPStatus.NO_CONTENT, response.status_code
|
||||
request, response = api_app.test_client.get(location)
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
self.assertDictContains(response.json['channel'], {
|
||||
'id': 1,
|
||||
'name': 'Example Channel 2',
|
||||
'directory': channel_directory_name,
|
||||
'match_regex': 'asdf',
|
||||
'url': 'https://example.com/channel1',
|
||||
})
|
||||
|
||||
# Can't update channel that doesn't exist
|
||||
request, response = api_app.test_client.put('/api/videos/channels/doesnt_exist',
|
||||
data=json.dumps(new_channel))
|
||||
assert response.status_code == HTTPStatus.NOT_FOUND
|
||||
# Patch it
|
||||
patch = {'name': 'new name'}
|
||||
request, response = api_app.test_client.patch(location, data=json.dumps(patch))
|
||||
assert response.status_code == HTTPStatus.NO_CONTENT, response.status_code
|
||||
request, response = api_app.test_client.get(location)
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
self.assertDictContains(response.json['channel'], {
|
||||
'id': 1,
|
||||
'name': 'new name',
|
||||
'directory': channel_directory_name,
|
||||
'match_regex': 'asdf',
|
||||
'url': 'https://example.com/channel1',
|
||||
})
|
||||
|
||||
# Delete the new channel
|
||||
request, response = api_app.test_client.delete(location)
|
||||
assert response.status_code == HTTPStatus.NO_CONTENT
|
||||
# Can't update channel that doesn't exist
|
||||
request, response = api_app.test_client.put('/api/videos/channels/doesnt_exist',
|
||||
data=json.dumps(new_channel))
|
||||
assert response.status_code == HTTPStatus.NOT_FOUND
|
||||
|
||||
# Cant delete it again
|
||||
request, response = api_app.test_client.delete(location)
|
||||
assert response.status_code == HTTPStatus.NOT_FOUND
|
||||
# Delete the new channel
|
||||
request, response = api_app.test_client.delete(location)
|
||||
assert response.status_code == HTTPStatus.NO_CONTENT
|
||||
|
||||
# Cant delete it again
|
||||
request, response = api_app.test_client.delete(location)
|
||||
assert response.status_code == HTTPStatus.NOT_FOUND
|
||||
|
||||
@wrap_test_db
|
||||
def test_channel_conflicts(self):
|
||||
|
|
@ -210,15 +213,61 @@ class TestVideoAPI(TestAPI):
|
|||
assert response.status_code == HTTPStatus.CREATED, response.json
|
||||
assert location != response.headers['Location']
|
||||
|
||||
@wrap_test_db
|
||||
def test_refresh_no_channel(self):
|
||||
with get_db_session() as session:
|
||||
videos_path = pathlib.Path(self.tmp_dir.name) / 'videos'
|
||||
videos_path.mkdir()
|
||||
|
||||
# A regular user-defined channel
|
||||
channel1_path = videos_path / 'channel1'
|
||||
channel1_path.mkdir()
|
||||
vid1 = channel1_path / 'MyChannelName_20000101_abcdefghijk_title1.mp4'
|
||||
vid1.touch()
|
||||
vid2 = channel1_path / 'MyChannelName_20000101_abcdefghijk_title2.mp4'
|
||||
vid2.touch()
|
||||
|
||||
channel1 = Channel(name='channel1', link='channel1', directory=channel1_path)
|
||||
session.add(channel1)
|
||||
session.commit()
|
||||
|
||||
# The special NO CHANNEL directory.
|
||||
from modules.videos.downloader import get_no_channel_directory
|
||||
no_channel_path = get_no_channel_directory()
|
||||
no_channel_path.mkdir()
|
||||
vid3 = no_channel_path / 'some video.mp4'
|
||||
vid3.touch()
|
||||
no_channel_subdir = no_channel_path / 'subdir'
|
||||
no_channel_subdir.mkdir()
|
||||
vid4 = no_channel_subdir / 'other video.mp4'
|
||||
vid4.touch()
|
||||
|
||||
api_app.test_client.post('/api/videos/refresh')
|
||||
|
||||
videos = session.query(Video).order_by(Video.title).all()
|
||||
self.assertEqual(len(videos), 4)
|
||||
|
||||
# Add some fake videos, they should be removed.
|
||||
session.add(Video(title='vid5'))
|
||||
session.add(Video(title='vid6', channel_id=channel1.id))
|
||||
session.commit()
|
||||
|
||||
videos = session.query(Video).order_by(Video.title).all()
|
||||
self.assertEqual(len(videos), 6)
|
||||
|
||||
api_app.test_client.post('/api/videos/refresh')
|
||||
|
||||
videos = session.query(Video).order_by(Video.title).all()
|
||||
self.assertEqual(len(videos), 4)
|
||||
|
||||
@wrap_test_db
|
||||
def test_refresh_videos(self):
|
||||
# There should be no messages until a refresh is called.
|
||||
pytest.raises(Empty, refresh_queue.get_nowait)
|
||||
|
||||
# Setup a fake channel directory.
|
||||
with get_db_session() as session, \
|
||||
tempfile.TemporaryDirectory() as channel_dir:
|
||||
channel_path = pathlib.Path(channel_dir)
|
||||
with get_db_session() as session:
|
||||
channel_path = pathlib.Path(self.tmp_dir.name)
|
||||
|
||||
# Files in subdirectories should be found and handled properly.
|
||||
subdir = channel_path / 'subdir'
|
||||
|
|
@ -243,26 +292,26 @@ class TestVideoAPI(TestAPI):
|
|||
poster2.touch()
|
||||
|
||||
# Create a channel, associate videos with it.
|
||||
channel = Channel(directory=channel_dir, link='foo', name='foo')
|
||||
channel = Channel(directory=channel_path, link='foo', name='foo')
|
||||
session.add(channel)
|
||||
session.flush()
|
||||
session.refresh(channel)
|
||||
video1 = upsert_video(session, vid1, channel)
|
||||
video2 = upsert_video(session, vid2, channel)
|
||||
session.commit()
|
||||
self.assertEqual({i.video_path for i in channel.videos},
|
||||
{pathlib.Path('subdir/' + vid1.name), pathlib.Path(vid2.name)})
|
||||
self.assertEqual({str(i.video_path.path) for i in channel.videos},
|
||||
{f'{channel_path}/subdir/{vid1.name}', f'{channel_path}/{vid2.name}'})
|
||||
|
||||
# Poster files were found.
|
||||
self.assertEqual(video1.poster_path, pathlib.Path('subdir/' + poster1.name))
|
||||
self.assertEqual(video2.poster_path, pathlib.Path(poster2.name))
|
||||
self.assertEqual(video1.poster_path.path, subdir / poster1.name)
|
||||
self.assertEqual(video2.poster_path.path, channel_path / poster2.name)
|
||||
|
||||
# Add a bogus file, this should be removed during the refresh
|
||||
self.assertNotIn('foo', {i.video_path for i in channel.videos})
|
||||
self.assertNotIn('foo', {i.video_path.path for i in channel.videos})
|
||||
session.add(Video(video_path='foo', channel_id=channel.id))
|
||||
session.flush()
|
||||
session.refresh(channel)
|
||||
self.assertIn('foo', {str(i.video_path) for i in channel.videos})
|
||||
self.assertIn(f'{self.tmp_dir.name}/foo', {str(i.video_path.path) for i in channel.videos})
|
||||
self.assertEqual(len(channel.videos), 3)
|
||||
|
||||
# Add a video that isn't in the DB, it should be found and any meta files associated with it
|
||||
|
|
@ -277,10 +326,10 @@ class TestVideoAPI(TestAPI):
|
|||
|
||||
# Finally, call the refresh. Again, it should remove the "foo" video, then discover this 3rd video
|
||||
# file and it's description.
|
||||
api_app.test_client.post('/api/videos:refresh')
|
||||
api_app.test_client.post('/api/videos/refresh')
|
||||
|
||||
# Bogus file was removed
|
||||
self.assertNotIn('foo', {i.video_path for i in channel.videos})
|
||||
self.assertNotIn(f'{self.tmp_dir.name}/foo', {i.video_path.path for i in channel.videos})
|
||||
|
||||
# Final channel video list we built
|
||||
expected = {
|
||||
|
|
@ -290,7 +339,7 @@ class TestVideoAPI(TestAPI):
|
|||
}
|
||||
|
||||
def str_or_none(i):
|
||||
return str(i) if i else None
|
||||
return str(i.__json__()) if i else None
|
||||
|
||||
self.assertEqual(
|
||||
{(str_or_none(i.video_path), str_or_none(i.poster_path), str_or_none(i.description_path))
|
||||
|
|
@ -300,10 +349,6 @@ class TestVideoAPI(TestAPI):
|
|||
|
||||
assert poster3.is_file(), 'Orphan jpg file was deleted!'
|
||||
|
||||
# During the refresh process, messages are pushed to a queue, make sure there are messages there
|
||||
messages = get_all_messages_in_queue(refresh_queue)
|
||||
assert 'refresh-started' in [i.get('code') for i in messages]
|
||||
|
||||
@wrap_test_db
|
||||
def test_get_channel_videos(self):
|
||||
with get_db_session(commit=True) as session:
|
||||
|
|
@ -466,3 +511,93 @@ class TestVideoAPI(TestAPI):
|
|||
response = do_search('b', 2)
|
||||
assert [i['id'] for i in response.json['videos']] == [1, 2]
|
||||
assert response.json['totals']['videos'] == 4
|
||||
|
||||
@wrap_test_db
|
||||
def test_channel_no_download_frequency(self):
|
||||
"""
|
||||
A channel does not require a download frequency.
|
||||
"""
|
||||
channel_directory = tempfile.TemporaryDirectory(dir=self.tmp_dir.name).name
|
||||
os.mkdir(channel_directory)
|
||||
|
||||
new_channel = dict(
|
||||
directory=channel_directory,
|
||||
name='Example Channel 1',
|
||||
url='https://example.com/channel1',
|
||||
download_frequency=None,
|
||||
)
|
||||
|
||||
# Create the Channel
|
||||
request, response = api_app.test_client.post('/api/videos/channels', data=json.dumps(new_channel))
|
||||
assert response.status_code == HTTPStatus.CREATED
|
||||
location = response.headers['Location']
|
||||
|
||||
# No downloads are scheduled.
|
||||
spread_channel_downloads()
|
||||
with get_db_session() as session:
|
||||
downloads = list(download_manager.get_new_downloads(session))
|
||||
self.assertEqual(len(downloads), 0)
|
||||
|
||||
# Get the Channel
|
||||
request, response = api_app.test_client.get(location)
|
||||
channel = response.json['channel']
|
||||
self.assertEqual(channel['download_frequency'], None)
|
||||
|
||||
# Update the Channel with a frequency.
|
||||
new_channel = dict(
|
||||
directory=channel['directory'],
|
||||
name=channel['name'],
|
||||
url=channel['url'],
|
||||
download_frequency=10,
|
||||
)
|
||||
request, response = api_app.test_client.put(f'/api/videos/channels/{channel["link"]}',
|
||||
data=json.dumps(new_channel))
|
||||
self.assertEqual(response.status_code, 204, response.json)
|
||||
|
||||
# Remove the frequency.
|
||||
new_channel = dict(
|
||||
directory=channel['directory'],
|
||||
name=channel['name'],
|
||||
url=channel['url'],
|
||||
download_frequency=None,
|
||||
)
|
||||
request, response = api_app.test_client.put(f'/api/videos/channels/{channel["link"]}',
|
||||
data=json.dumps(new_channel))
|
||||
self.assertEqual(response.status_code, 204, response.json)
|
||||
|
||||
|
||||
def test_channel_frequency_update(download_channel, test_client, test_session):
|
||||
"""
|
||||
A Channel's Download record is updated when the Channel's frequency is updated.
|
||||
"""
|
||||
old_frequency = download_channel.get_download().frequency
|
||||
assert old_frequency
|
||||
|
||||
data = dict(
|
||||
directory=str(download_channel.directory.path),
|
||||
name=download_channel.name,
|
||||
url=download_channel.url,
|
||||
download_frequency=100,
|
||||
)
|
||||
request, response = test_client.put(f'/api/videos/channels/{download_channel.link}', json=data)
|
||||
assert response.status_code == HTTPStatus.NO_CONTENT, response.json
|
||||
|
||||
download = download_channel.get_download()
|
||||
assert download.frequency == 100
|
||||
|
||||
# Only one download
|
||||
downloads = test_session.query(Download).all()
|
||||
assert len(list(downloads)) == 1
|
||||
|
||||
|
||||
def test_delete_channel_delete_download(download_channel, test_session):
|
||||
"""
|
||||
Deleting a Channel deletes it's Download.
|
||||
"""
|
||||
downloads = test_session.query(Download).all()
|
||||
assert all(i.url == download_channel.url for i in downloads)
|
||||
|
||||
delete_channel(download_channel.link)
|
||||
|
||||
downloads = test_session.query(Download).all()
|
||||
assert not list(downloads)
|
||||
|
|
|
|||
|
|
@ -1,21 +0,0 @@
|
|||
import pathlib
|
||||
import unittest
|
||||
|
||||
from modules.videos.models import Channel
|
||||
from modules.videos.test.common import create_channel_structure
|
||||
from wrolpi.db import get_db_session
|
||||
from wrolpi.test.common import wrap_test_db
|
||||
|
||||
|
||||
class TestChannel(unittest.TestCase):
|
||||
|
||||
@wrap_test_db
|
||||
@create_channel_structure(
|
||||
{
|
||||
'Foo': ['vid1.mp4'],
|
||||
}
|
||||
)
|
||||
def test_channel(self, tempdir):
|
||||
with get_db_session() as session:
|
||||
channel = session.query(Channel).one()
|
||||
self.assertIsInstance(channel.directory, pathlib.Path)
|
||||
|
|
@ -12,11 +12,12 @@ from PIL import Image
|
|||
from sqlalchemy.orm import Session
|
||||
|
||||
from wrolpi import before_startup
|
||||
from wrolpi.common import sanitize_link, logger, CONFIG_PATH, get_config, iterify, chunks, get_media_directory, \
|
||||
get_absolute_media_path, minimize_dict
|
||||
from wrolpi.common import logger, CONFIG_PATH, get_config, iterify, chunks, get_media_directory, \
|
||||
minimize_dict
|
||||
from wrolpi.db import get_db_session, get_db_curs
|
||||
from wrolpi.errors import UnknownFile, UnknownDirectory, ChannelNameConflict, ChannelURLConflict, \
|
||||
ChannelLinkConflict, ChannelDirectoryConflict
|
||||
ChannelLinkConflict, ChannelDirectoryConflict, ChannelSourceIdConflict
|
||||
from wrolpi.media_path import MediaPath
|
||||
from wrolpi.vars import DOCKERIZED, DEFAULT_FILE_PERMISSIONS
|
||||
from .models import Channel, Video
|
||||
|
||||
|
|
@ -24,7 +25,7 @@ logger = logger.getChild(__name__)
|
|||
|
||||
REQUIRED_OPTIONS = ['name', 'directory']
|
||||
MINIMUM_CHANNEL_KEYS = {'id', 'name', 'directory', 'url', 'video_count', 'link'}
|
||||
MINIMUM_INFO_JSON_KEYS = {'description', 'view_count'}
|
||||
MINIMUM_INFO_JSON_KEYS = {'description', 'view_count', 'webpage_url'}
|
||||
MINIMUM_VIDEO_KEYS = {'id', 'title', 'upload_date', 'duration', 'channel', 'channel_id', 'favorite', 'size',
|
||||
'poster_path', 'caption_path', 'video_path', 'info_json', 'channel', 'viewed', 'source_id',
|
||||
'view_count'}
|
||||
|
|
@ -67,35 +68,38 @@ def import_videos_config():
|
|||
config = load_channels_config()
|
||||
|
||||
with get_db_session(commit=True) as session:
|
||||
for section in config:
|
||||
for option in (i for i in REQUIRED_OPTIONS if i not in config[section]):
|
||||
raise ConfigError(f'Channel "{section}" is required to have "{option}"')
|
||||
for link in config:
|
||||
for option in (i for i in REQUIRED_OPTIONS if i not in config[link]):
|
||||
raise ConfigError(f'Channel "{link}" is required to have "{option}"')
|
||||
|
||||
name, directory = config[section]['name'], config[section]['directory']
|
||||
name, directory = config[link]['name'], config[link]['directory']
|
||||
|
||||
link = sanitize_link(name)
|
||||
matches = session.query(Channel).filter(Channel.link == link)
|
||||
if not matches.count():
|
||||
# Channel not yet in the DB, add it
|
||||
channel = session.query(Channel).filter(Channel.link == link).one_or_none()
|
||||
if not channel:
|
||||
# Channel not yet in the DB, add it.
|
||||
channel = Channel(link=link)
|
||||
else:
|
||||
channel = matches.one()
|
||||
|
||||
# Only name and directory are required
|
||||
channel.name = name
|
||||
channel.directory = str(directory)
|
||||
|
||||
channel.calculate_duration = config[section].get('calculate_duration')
|
||||
channel.download_frequency = config[section].get('download_frequency')
|
||||
channel.generate_posters = config[section].get('generate_posters')
|
||||
channel.match_regex = config[section].get('match_regex')
|
||||
channel.skip_download_videos = list(set(config[section].get('skip_download_videos', {})))
|
||||
channel.url = config[section].get('url')
|
||||
channel.calculate_duration = config[link].get('calculate_duration') or channel.calculate_duration
|
||||
channel.download_frequency = config[link].get('download_frequency') or channel.download_frequency
|
||||
channel.generate_posters = config[link].get('generate_posters') or channel.generate_posters
|
||||
channel.match_regex = config[link].get('match_regex') or channel.match_regex
|
||||
channel.skip_download_videos = list(set(config[link].get('skip_download_videos', {})))
|
||||
channel.source_id = config[link].get('source_id') or channel.source_id
|
||||
channel.url = config[link].get('url') or channel.url
|
||||
|
||||
if not channel.source_id and channel.url:
|
||||
# If we can download from a channel, we must have its source_id.
|
||||
from .downloader import get_channel_source_id
|
||||
channel.source_id = get_channel_source_id(channel.url)
|
||||
|
||||
session.add(channel)
|
||||
|
||||
# Set favorite Videos of this Channel.
|
||||
favorites = config[section].get('favorites', {})
|
||||
favorites = config[link].get('favorites', {})
|
||||
if favorites:
|
||||
videos = session.query(Video).filter(Video.source_id.in_(favorites.keys()))
|
||||
for video in videos:
|
||||
|
|
@ -107,13 +111,12 @@ def import_videos_config():
|
|||
VALID_VIDEO_KINDS = {'video', 'caption', 'poster', 'description', 'info_json'}
|
||||
|
||||
|
||||
def get_absolute_video_path(video: Video, kind: str = 'video') -> Path:
|
||||
def get_absolute_video_path(video: Video, kind: str = 'video') -> MediaPath:
|
||||
if kind not in VALID_VIDEO_KINDS:
|
||||
raise Exception(f'Unknown video path kind {kind}')
|
||||
directory = get_absolute_media_path(video.channel.directory)
|
||||
path = getattr(video, f'{kind}_path')
|
||||
if directory and path:
|
||||
return directory / path
|
||||
if path:
|
||||
return path
|
||||
raise UnknownFile()
|
||||
|
||||
|
||||
|
|
@ -123,7 +126,7 @@ get_absolute_video_description = partial(get_absolute_video_path, kind='descript
|
|||
get_absolute_video_info_json = partial(get_absolute_video_path, kind='info_json')
|
||||
|
||||
|
||||
def get_absolute_video_files(video: Video) -> List[Path]:
|
||||
def get_absolute_video_files(video: Video) -> List[MediaPath]:
|
||||
"""
|
||||
Get all video files that exist.
|
||||
"""
|
||||
|
|
@ -173,7 +176,7 @@ def any_extensions(filename: str, extensions: Iterable = ()):
|
|||
match_video_extensions = partial(any_extensions, extensions=VIDEO_EXTENSIONS)
|
||||
|
||||
|
||||
def generate_video_paths(directory: Union[str, pathlib.Path], relative_to=None) -> Tuple[str, pathlib.Path]:
|
||||
def generate_video_paths(directory: Union[str, pathlib.Path]) -> Tuple[str, pathlib.Path]:
|
||||
"""
|
||||
Generate a list of video paths in the provided directory.
|
||||
"""
|
||||
|
|
@ -182,10 +185,7 @@ def generate_video_paths(directory: Union[str, pathlib.Path], relative_to=None)
|
|||
for child in directory.iterdir():
|
||||
if child.is_file() and match_video_extensions(child.name):
|
||||
child = child.absolute()
|
||||
if relative_to:
|
||||
yield child.relative_to(relative_to)
|
||||
else:
|
||||
yield child
|
||||
yield child
|
||||
elif child.is_dir():
|
||||
yield from generate_video_paths(child)
|
||||
|
||||
|
|
@ -232,7 +232,8 @@ def remove_duplicate_video_paths(paths: Iterable[Path]) -> Set[Path]:
|
|||
yield sorted(paths)[0]
|
||||
|
||||
|
||||
def check_for_channel_conflicts(session: Session, id_=None, url=None, name=None, link=None, directory=None):
|
||||
def check_for_channel_conflicts(session: Session, id_=None, url=None, name=None, link=None, directory=None,
|
||||
source_id=None):
|
||||
"""
|
||||
Search for any channels that conflict with the provided args, raise a relevant exception if any conflicts are found.
|
||||
"""
|
||||
|
|
@ -263,6 +264,10 @@ def check_for_channel_conflicts(session: Session, id_=None, url=None, name=None,
|
|||
conflicts = base_where.filter(Channel.directory == directory)
|
||||
if list(conflicts):
|
||||
raise ChannelDirectoryConflict()
|
||||
if source_id:
|
||||
conflicts = base_where.filter(Channel.source_id == source_id)
|
||||
if list(conflicts):
|
||||
raise ChannelSourceIdConflict()
|
||||
|
||||
|
||||
def verify_config():
|
||||
|
|
@ -295,10 +300,14 @@ def get_matching_directories(path: Union[str, Path]) -> List[str]:
|
|||
"""
|
||||
path = str(path)
|
||||
|
||||
ignored_directories = {
|
||||
str(get_media_directory() / 'videos/NO CHANNEL'),
|
||||
}
|
||||
|
||||
if os.path.isdir(path):
|
||||
# The provided path is a directory, return it's subdirectories, or itself if no subdirectories exist
|
||||
paths = [os.path.join(path, i) for i in os.listdir(path)]
|
||||
paths = sorted(i for i in paths if os.path.isdir(i))
|
||||
paths = sorted(i for i in paths if os.path.isdir(i) and i not in ignored_directories)
|
||||
if len(paths) == 0:
|
||||
return [path]
|
||||
return paths
|
||||
|
|
@ -307,7 +316,8 @@ def get_matching_directories(path: Union[str, Path]) -> List[str]:
|
|||
paths = os.listdir(head)
|
||||
paths = [os.path.join(head, i) for i in paths]
|
||||
pattern = path.lower()
|
||||
paths = sorted(i for i in paths if os.path.isdir(i) and i.lower().startswith(pattern))
|
||||
paths = sorted(
|
||||
i for i in paths if os.path.isdir(i) and i.lower().startswith(pattern) and i not in ignored_directories)
|
||||
|
||||
return paths
|
||||
|
||||
|
|
@ -354,14 +364,12 @@ async def generate_bulk_posters(video_ids: List[int]):
|
|||
with get_db_session(commit=True) as session:
|
||||
videos = session.query(Video).filter(Video.id.in_(video_ids))
|
||||
for video in videos:
|
||||
video_path = get_absolute_video_path(video)
|
||||
video_path = video.video_path.path
|
||||
|
||||
poster_path = replace_extension(video_path, '.jpg')
|
||||
if not poster_path.exists():
|
||||
generate_video_poster(video_path)
|
||||
channel_dir = get_absolute_media_path(video.channel.directory)
|
||||
poster_path = poster_path.relative_to(channel_dir)
|
||||
video.poster_path = str(poster_path)
|
||||
video.poster_path = poster_path
|
||||
logger.info('Done generating video posters')
|
||||
|
||||
|
||||
|
|
@ -400,10 +408,9 @@ def bulk_validate_posters(video_ids: List[int]):
|
|||
for video_ids in chunks(video_ids, 10):
|
||||
for video_id in video_ids:
|
||||
with get_db_session(commit=True) as session:
|
||||
video = session.query(Video).filter_by(id=video_id).one()
|
||||
channel = video.channel
|
||||
video: Video = session.query(Video).filter_by(id=video_id).one()
|
||||
|
||||
poster_path: Path = get_absolute_video_poster(video)
|
||||
poster_path: Path = video.poster_path.path
|
||||
new_poster_path = poster_path.with_suffix('.jpg')
|
||||
|
||||
if poster_path != new_poster_path and new_poster_path.exists():
|
||||
|
|
@ -423,10 +430,8 @@ def bulk_validate_posters(video_ids: List[int]):
|
|||
else:
|
||||
logger.debug(f'Poster was already valid: {new_poster_path}')
|
||||
|
||||
channel_dir = get_absolute_media_path(channel.directory)
|
||||
|
||||
# Update the video with the new poster path. Mark it as validated.
|
||||
video.poster_path = str(new_poster_path.relative_to(channel_dir))
|
||||
video.poster_path = new_poster_path
|
||||
video.validated_poster = True
|
||||
logger.info('Done validating video posters.')
|
||||
|
||||
|
|
@ -465,6 +470,10 @@ async def get_bulk_video_info_json(video_ids: List[int]):
|
|||
video = session.query(Video).filter_by(id=video_id).one()
|
||||
logger.debug(f'Getting video info_json data: {video}')
|
||||
|
||||
if not video.video_path:
|
||||
logger.warning(f'Refusing to get info_json for video without a video file: {video}')
|
||||
continue
|
||||
|
||||
try:
|
||||
info_json = video.get_info_json()
|
||||
if info_json:
|
||||
|
|
@ -475,8 +484,7 @@ async def get_bulk_video_info_json(video_ids: List[int]):
|
|||
video.duration = info_json.get('duration')
|
||||
elif not video.duration:
|
||||
# As a last resort, get duration from the video file.
|
||||
video_path = get_absolute_video_path(video)
|
||||
video.duration = get_video_duration(video_path)
|
||||
video.duration = get_video_duration(video.video_path.path)
|
||||
|
||||
except Exception:
|
||||
logger.warning(f'Unable to get meta data of {video}', exc_info=True)
|
||||
|
|
@ -493,16 +501,15 @@ async def get_bulk_video_size(video_ids: List[int]):
|
|||
for video_id in video_ids:
|
||||
video = session.query(Video).filter_by(id=video_id).one()
|
||||
logger.debug(f'Getting video size: {video.id} {video.video_path}')
|
||||
video_path = get_absolute_video_path(video)
|
||||
|
||||
size = video_path.stat().st_size
|
||||
size = video.video_path.path.stat().st_size
|
||||
video.size = size
|
||||
logger.info('Done getting video sizes')
|
||||
|
||||
|
||||
def update_view_count(channel_id: int):
|
||||
"""
|
||||
Update view_count for all Videos in a channel.
|
||||
Update view_count for all Videos in a channel using it's info_json file.
|
||||
"""
|
||||
with get_db_session() as session:
|
||||
channel = session.query(Channel).filter_by(id=channel_id).one()
|
||||
|
|
|
|||
33
modules/videos/conftest.py
Normal file
33
modules/videos/conftest.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import pytest
|
||||
|
||||
from modules.videos.channel.lib import spread_channel_downloads
|
||||
from modules.videos.models import Channel
|
||||
from wrolpi.downloader import DownloadFrequency
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def simple_channel(test_session, test_directory) -> Channel:
|
||||
"""
|
||||
Get a Channel with the minimum properties. This Channel has no download!
|
||||
"""
|
||||
channel = Channel(
|
||||
directory=test_directory,
|
||||
name='Example Channel 1',
|
||||
url='https://example.com/channel1',
|
||||
download_frequency=None, # noqa
|
||||
link='examplechannel1',
|
||||
)
|
||||
test_session.add(channel)
|
||||
test_session.commit()
|
||||
return channel
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def download_channel(simple_channel) -> Channel:
|
||||
"""
|
||||
Get a test Channel that has a download frequency.
|
||||
"""
|
||||
# Add a frequency to the test channel, then give it a download.
|
||||
simple_channel.download_frequency = DownloadFrequency.weekly
|
||||
spread_channel_downloads()
|
||||
return simple_channel
|
||||
|
|
@ -1,21 +1,27 @@
|
|||
#! /usr/bin/env python3
|
||||
import json
|
||||
import pathlib
|
||||
import re
|
||||
from queue import Queue
|
||||
from random import shuffle
|
||||
from typing import Tuple, List, Dict
|
||||
from abc import ABC
|
||||
from typing import Tuple, List
|
||||
|
||||
from sqlalchemy import or_
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
from youtube_dl import YoutubeDL
|
||||
from yt_dlp import YoutubeDL
|
||||
from yt_dlp.extractor import YoutubeTabIE
|
||||
from yt_dlp.utils import UnsupportedError, DownloadError
|
||||
|
||||
from wrolpi.common import logger, ProgressReporter, run_after, get_absolute_media_path
|
||||
from wrolpi.dates import now, today
|
||||
from wrolpi.common import logger, sanitize_link, extract_domain, \
|
||||
get_media_directory
|
||||
from wrolpi.dates import now
|
||||
from wrolpi.db import get_db_session, get_db_curs
|
||||
from wrolpi.errors import UnknownChannel, ChannelURLEmpty
|
||||
from wrolpi.downloader import Downloader, Download
|
||||
from wrolpi.errors import UnknownChannel, ChannelURLEmpty, UnrecoverableDownloadError
|
||||
from wrolpi.vars import PYTEST
|
||||
from .channel.lib import create_channel, get_channel
|
||||
from .common import load_downloader_config, update_view_count
|
||||
from .lib import save_channels_config, upsert_video
|
||||
from .lib import upsert_video, _refresh_videos
|
||||
from .models import Video, Channel
|
||||
from .video_url_resolver import video_url_resolver
|
||||
|
||||
logger = logger.getChild(__name__)
|
||||
ydl_logger = logger.getChild('youtube-dl')
|
||||
|
|
@ -24,17 +30,219 @@ YDL = YoutubeDL()
|
|||
YDL.params['logger'] = ydl_logger
|
||||
YDL.add_default_info_extractors()
|
||||
|
||||
# Channels are handled differently than a single video.
|
||||
ChannelIEs = {
|
||||
YoutubeTabIE,
|
||||
}
|
||||
|
||||
def update_channel(channel=None, link: str = None):
|
||||
PREFERRED_VIDEO_EXTENSION = 'mp4'
|
||||
PREFERRED_VIDEO_FORMAT = 'best[height=720]'
|
||||
|
||||
|
||||
def get_no_channel_directory():
|
||||
return get_media_directory() / 'videos/NO CHANNEL'
|
||||
|
||||
|
||||
class ChannelDownloader(Downloader, ABC):
|
||||
"""
|
||||
Handling downloading of all Videos in a Channel.
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def valid_url(url) -> bool:
|
||||
for ie in ChannelIEs:
|
||||
if ie.suitable(url):
|
||||
return True
|
||||
return False
|
||||
|
||||
def do_download(self, download: Download):
|
||||
"""
|
||||
Update a Channel's catalog, then schedule downloads of every missing video.
|
||||
"""
|
||||
with get_db_session() as session:
|
||||
channel = session.query(Channel).filter_by(url=download.url).one()
|
||||
|
||||
update_channel(channel)
|
||||
|
||||
domain = extract_domain(download.url)
|
||||
missing_videos = find_all_missing_videos(channel.link)
|
||||
with get_db_session(commit=True) as session:
|
||||
for video_id, source_id, missing_video in missing_videos:
|
||||
url = video_url_resolver(domain, missing_video)
|
||||
# Schedule any missing videos for download.
|
||||
self.manager.create_download(url, session, skip_download=True)
|
||||
|
||||
if PYTEST:
|
||||
self.manager.do_downloads_sync()
|
||||
else:
|
||||
self.manager.start_downloads()
|
||||
|
||||
return True
|
||||
|
||||
|
||||
class VideoDownloader(Downloader, ABC):
|
||||
"""
|
||||
Download a single video. Store the video in it's channel's directory, otherwise store it in `videos/NO CHANNEL`.
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def valid_url(url) -> bool:
|
||||
"""
|
||||
Match against all Youtube-DL Info Extractors, except those that match a Channel.
|
||||
"""
|
||||
for ie in YDL._ies.values():
|
||||
if ie.suitable(url) and not ChannelDownloader.valid_url(url):
|
||||
try:
|
||||
YDL.extract_info(url, download=False, process=False)
|
||||
return True
|
||||
except UnsupportedError:
|
||||
logger.debug(f'Video downloader extract_info failed for {url}')
|
||||
return False
|
||||
except DownloadError:
|
||||
logger.debug(f'Video downloader extract_info failed for {url}')
|
||||
return False
|
||||
return False
|
||||
|
||||
def do_download(self, download: Download):
|
||||
if download.attempts >= 10:
|
||||
raise UnrecoverableDownloadError('Max download attempts reached')
|
||||
|
||||
url = download.url
|
||||
try:
|
||||
info = YDL.extract_info(url, download=False, process=False)
|
||||
except UnsupportedError as e:
|
||||
# Video wasn't really valid... probably the GenericIE.
|
||||
raise UnrecoverableDownloadError() from e
|
||||
|
||||
channel_name = info.get('channel')
|
||||
channel_id = info.get('channel_id')
|
||||
channel = None
|
||||
if channel_name or channel_id:
|
||||
channel_url = info.get('channel_url')
|
||||
channel = get_or_create_channel(channel_id, None, channel_url, channel_name)
|
||||
|
||||
# Use the default directory if this video has no channel.
|
||||
out_dir = get_no_channel_directory()
|
||||
if channel:
|
||||
out_dir = channel.directory.path
|
||||
out_dir.mkdir(exist_ok=True)
|
||||
|
||||
try:
|
||||
video_path, entry = self.prepare_filename(url, out_dir)
|
||||
# Do the real download.
|
||||
file_name_format = '%(uploader)s_%(upload_date)s_%(id)s_%(title)s.%(ext)s'
|
||||
cmd = [
|
||||
'yt-dlp',
|
||||
'-cw', # Continue downloads, do not clobber existing files.
|
||||
'-f', PREFERRED_VIDEO_FORMAT,
|
||||
'--write-subs',
|
||||
'--write-auto-subs',
|
||||
'--write-thumbnail',
|
||||
'--write-info-json',
|
||||
'--merge-output-format', PREFERRED_VIDEO_EXTENSION,
|
||||
'-o', file_name_format,
|
||||
url,
|
||||
]
|
||||
return_code = self.process_runner(url, cmd, out_dir)
|
||||
|
||||
if return_code != 0:
|
||||
raise ValueError(f'youtube-dl exited with {return_code}')
|
||||
|
||||
if not video_path.is_file():
|
||||
raise ValueError(f'Video file could not be found! {video_path}')
|
||||
except UnrecoverableDownloadError:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.warning(f'Failed to download "{info["title"]}"', exc_info=e)
|
||||
if _skip_download(e):
|
||||
# The video failed to download, and the error will never be fixed. Skip it forever.
|
||||
source_id = info.get('id')
|
||||
logger.warning(f'Adding video "{source_id}" to skip list for this channel. WROLPi will not '
|
||||
f'attempt to download it again.')
|
||||
|
||||
with get_db_session(commit=True) as session:
|
||||
channel = session.query(Channel).filter_by(id=channel.id).one()
|
||||
channel.add_video_to_skip_list(source_id)
|
||||
return
|
||||
|
||||
with get_db_session(commit=True) as session:
|
||||
# If the video is from a channel, it will already be in the database.
|
||||
source_id = entry['id']
|
||||
existing_video = session.query(Video).filter_by(source_id=source_id).one_or_none()
|
||||
id_ = existing_video.id if existing_video else None
|
||||
|
||||
upsert_video(session, video_path, channel, id_=id_)
|
||||
|
||||
# Successful download.
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def prepare_filename(url: str, out_dir: pathlib.Path) -> Tuple[pathlib.Path, dict]:
|
||||
if not out_dir.is_dir():
|
||||
raise ValueError(f'Output directory does not exist! {out_dir=}')
|
||||
|
||||
# YoutubeDL expects specific options, add onto the default options
|
||||
config = load_downloader_config()
|
||||
options = config.copy()
|
||||
options['outtmpl'] = f'{out_dir}/{config["file_name_format"]}'
|
||||
# We don't need format when getting the filename.
|
||||
del options['format']
|
||||
|
||||
logger.debug(f'Downloading {url} to {out_dir}')
|
||||
|
||||
# Create a new YoutubeDL for the output directory.
|
||||
ydl = YoutubeDL(options)
|
||||
ydl.params['logger'] = ydl_logger
|
||||
ydl.add_default_info_extractors()
|
||||
|
||||
# Perform the real download. This saves multiple files.
|
||||
entry = ydl.extract_info(url, download=False, process=True)
|
||||
final_filename = pathlib.Path(ydl.prepare_filename(entry))
|
||||
return final_filename, entry
|
||||
|
||||
|
||||
channel_downloader = ChannelDownloader()
|
||||
# Videos may match the ChannelDownloader, give it a higher priority.
|
||||
video_downloader = VideoDownloader(40)
|
||||
|
||||
|
||||
def get_or_create_channel(source_id: str = None, link: str = None, url: str = None, name: str = None) -> Channel:
|
||||
"""
|
||||
Attempt to find a Channel using the provided params. The params are in order of reliability.
|
||||
|
||||
Creates a new Channel if one cannot be found.
|
||||
"""
|
||||
if not link and name:
|
||||
link = sanitize_link(name)
|
||||
|
||||
try:
|
||||
channel = get_channel(link=link, source_id=source_id, url=url, return_dict=False)
|
||||
return channel
|
||||
except UnknownChannel:
|
||||
pass
|
||||
|
||||
channel_directory = get_media_directory() / f'videos/{name}'
|
||||
data = dict(
|
||||
name=name,
|
||||
url=url,
|
||||
link=link,
|
||||
domain=extract_domain(url),
|
||||
directory=str(channel_directory),
|
||||
source_id=source_id,
|
||||
)
|
||||
channel = create_channel(data, return_dict=False)
|
||||
# Create the directory now that the channel is approved.
|
||||
channel_directory.mkdir(exist_ok=True)
|
||||
|
||||
return channel
|
||||
|
||||
|
||||
def update_channel(channel: Channel):
|
||||
"""
|
||||
Connect to the Channel's host website and pull a catalog of all videos. Insert any new videos into the DB.
|
||||
|
||||
It is expected that any missing videos will be downloaded later.
|
||||
"""
|
||||
with get_db_session() as session:
|
||||
if not channel:
|
||||
channel = session.query(Channel).filter_by(link=link).one()
|
||||
|
||||
logger.info(f'Downloading video list for {channel.name} at {channel.url} This may take several minutes.')
|
||||
info = YDL.extract_info(channel.url, download=False, process=False)
|
||||
if 'url' in info:
|
||||
|
|
@ -63,16 +271,19 @@ def update_channel(channel=None, link: str = None):
|
|||
logger.warning(f'entries: {entries}')
|
||||
raise KeyError('No id key for entry!') from e
|
||||
|
||||
# In order to store the Video's URL, we will need a quick lookup.
|
||||
urls = {i['id']: i.get('webpage_url') for i in entries}
|
||||
|
||||
with get_db_session(commit=True) as session:
|
||||
# Get the channel in this new context.
|
||||
channel = session.query(Channel).filter_by(id=channel.id).one()
|
||||
|
||||
channel.info_json = info
|
||||
channel.info_date = now()
|
||||
channel.increment_next_download()
|
||||
channel.source_id = info.get('id')
|
||||
|
||||
with get_db_curs() as curs:
|
||||
# Insert any new videos.
|
||||
# Get all known videos in this channel.
|
||||
query = 'SELECT source_id FROM video WHERE channel_id=%s AND source_id IS NOT NULL'
|
||||
curs.execute(query, (channel.id,))
|
||||
known_source_ids = {i[0] for i in curs.fetchall()}
|
||||
|
|
@ -82,59 +293,19 @@ def update_channel(channel=None, link: str = None):
|
|||
logger.info(f'Got {len(new_source_ids)} new videos for channel {channel.name}')
|
||||
channel_id = channel.id
|
||||
for source_id in new_source_ids:
|
||||
session.add(Video(source_id=source_id, channel_id=channel_id))
|
||||
url = urls.get(source_id)
|
||||
session.add(Video(source_id=source_id, channel_id=channel_id, url=url))
|
||||
|
||||
# Write the Channel's info to a JSON file.
|
||||
if channel.directory:
|
||||
info_json_path = channel.directory.path / f'{channel.name}.info.json'
|
||||
with info_json_path.open('wt') as fh:
|
||||
json.dump(info, fh, indent=2)
|
||||
|
||||
# Update all view counts using the latest from the Channel's info_json.
|
||||
update_view_count(channel_id)
|
||||
|
||||
|
||||
def update_channels(reporter: ProgressReporter, link: str = None):
|
||||
"""Update all information for each channel. (No downloads performed)"""
|
||||
|
||||
with get_db_session() as session:
|
||||
if session.query(Channel).count() == 0:
|
||||
raise UnknownChannel('No channels exist yet')
|
||||
|
||||
if link:
|
||||
try:
|
||||
channel = session.query(Channel).filter_by(link=link).one()
|
||||
except NoResultFound:
|
||||
raise UnknownChannel(f'No channel with link: {link}')
|
||||
channels = [channel, ]
|
||||
else:
|
||||
channels = session.query(Channel).filter(
|
||||
Channel.url != None, # noqa
|
||||
Channel.url != '',
|
||||
or_(
|
||||
Channel.next_download == None, # noqa
|
||||
Channel.next_download <= today(),
|
||||
)
|
||||
).all()
|
||||
|
||||
if len(channels) == 0:
|
||||
logger.warning(f'All channels are up to date')
|
||||
|
||||
reporter.set_progress_total(0, len(channels))
|
||||
reporter.send_progress(0, 0, f'{len(channels)} channels scheduled for update')
|
||||
|
||||
# Randomize downloading of channels.
|
||||
shuffle(channels)
|
||||
|
||||
logger.debug(f'Getting info for {len(channels)} channels')
|
||||
for idx, channel in enumerate(channels):
|
||||
reporter.send_progress(0, idx, f'Getting video list for {channel.name}')
|
||||
try:
|
||||
update_channel(channel)
|
||||
except Exception:
|
||||
logger.critical(f'Unable to get video list for {channel.name}', exc_info=True)
|
||||
continue
|
||||
|
||||
if channels:
|
||||
reporter.send_progress(0, len(channels), 'Done downloading video lists')
|
||||
else:
|
||||
reporter.finish(0, 'Done downloading video lists')
|
||||
|
||||
|
||||
def _find_all_missing_videos(link: str = None) -> List[Tuple]:
|
||||
"""
|
||||
Get all Video entries which don't have the required media files (i.e. hasn't been downloaded). Restrict to a
|
||||
|
|
@ -162,11 +333,12 @@ def _find_all_missing_videos(link: str = None) -> List[Tuple]:
|
|||
WHERE
|
||||
channel.url IS NOT NULL
|
||||
AND channel.url != ''
|
||||
AND source_id IS NOT NULL
|
||||
AND video.source_id IS NOT NULL
|
||||
{where}
|
||||
AND channel_id IS NOT NULL
|
||||
AND video.channel_id IS NOT NULL
|
||||
AND (video_path IS NULL OR video_path = '' OR poster_path IS NULL OR poster_path = '')
|
||||
'''
|
||||
logger.debug(query)
|
||||
curs.execute(query, params)
|
||||
missing_videos = list(curs.fetchall())
|
||||
return missing_videos
|
||||
|
|
@ -193,6 +365,27 @@ def find_all_missing_videos(link: str = None) -> Tuple[dict, dict]:
|
|||
# Get all channels while in the db context.
|
||||
channels = list(channels)
|
||||
|
||||
# Check that each channel has some videos. We can't be sure what is missing if we don't know what we have.
|
||||
for channel in channels:
|
||||
refresh = False
|
||||
with get_db_curs() as curs:
|
||||
curs.execute('''
|
||||
SELECT
|
||||
COUNT(v.id)
|
||||
FROM
|
||||
channel c
|
||||
LEFT OUTER JOIN video v on c.id = v.channel_id
|
||||
WHERE
|
||||
c.id = %s
|
||||
''', (channel.id,))
|
||||
video_count = curs.fetchall()[0]
|
||||
if video_count == 0:
|
||||
# No videos for this channel. We can't be sure if any videos are missing without a refresh.
|
||||
refresh = True
|
||||
if refresh:
|
||||
# Refresh this channel's videos.
|
||||
_refresh_videos([channel.link])
|
||||
|
||||
channels = {i.id: i for i in channels}
|
||||
|
||||
match_regexen = {i: re.compile(j.match_regex) for i, j in channels.items() if j.match_regex}
|
||||
|
|
@ -221,31 +414,7 @@ def find_all_missing_videos(link: str = None) -> Tuple[dict, dict]:
|
|||
match_regex: re.compile = match_regexen.get(channel_id)
|
||||
if not match_regex or (match_regex and missing_video['title'] and match_regex.match(missing_video['title'])):
|
||||
# No title match regex, or the title matches the regex.
|
||||
yield channel, id_, missing_video
|
||||
|
||||
|
||||
def download_video(channel: Channel, video: Dict) -> Tuple[pathlib.Path, Dict]:
|
||||
"""
|
||||
Download a video (and associated posters/etc) to it's channel's directory.
|
||||
|
||||
:param channel:
|
||||
:param video: A YoutubeDL info entry dictionary
|
||||
:return:
|
||||
"""
|
||||
# YoutubeDL expects specific options, add onto the default options
|
||||
config = load_downloader_config()
|
||||
options = dict(config)
|
||||
directory = get_absolute_media_path(channel.directory)
|
||||
options['outtmpl'] = f'{directory}/{config["file_name_format"]}'
|
||||
|
||||
ydl = YoutubeDL(options)
|
||||
ydl.add_default_info_extractors()
|
||||
source_id = video['id']
|
||||
url = f'https://www.youtube.com/watch?v={source_id}'
|
||||
entry = ydl.extract_info(url, download=True, process=True)
|
||||
final_filename = ydl.prepare_filename(entry)
|
||||
final_filename = pathlib.Path(final_filename)
|
||||
return final_filename, entry
|
||||
yield id_, source_id, missing_video
|
||||
|
||||
|
||||
UNRECOVERABLE_ERRORS = {
|
||||
|
|
@ -268,42 +437,6 @@ def _skip_download(error):
|
|||
return False
|
||||
|
||||
|
||||
@run_after(save_channels_config)
|
||||
def download_all_missing_videos(reporter: ProgressReporter, link: str = None):
|
||||
"""Find any videos identified by the info packet that haven't yet been downloaded, download them."""
|
||||
missing_videos = list(find_all_missing_videos(link))
|
||||
reporter.set_progress_total(1, len(missing_videos))
|
||||
reporter.message(1, f'Found {len(missing_videos)} missing videos.')
|
||||
|
||||
for idx, (channel, id_, missing_video) in enumerate(missing_videos):
|
||||
reporter.send_progress(1, idx, f'Downloading {channel.name}: {missing_video["title"]}')
|
||||
try:
|
||||
video_path, info_json = download_video(channel, missing_video)
|
||||
except Exception as e:
|
||||
logger.warning(f'Failed to download "{missing_video["title"]}"', exc_info=e)
|
||||
if _skip_download(e):
|
||||
# The video failed to download, and the error will never be fixed. Skip it forever.
|
||||
source_id = missing_video.get('id')
|
||||
logger.warning(f'Adding video "{source_id}" to skip list for this channel. WROLPi will not '
|
||||
f'attempt to download it again.')
|
||||
|
||||
with get_db_session(commit=True) as session:
|
||||
channel = session.query(Channel).filter_by(id=channel.id).one()
|
||||
channel.add_video_to_skip_list(source_id)
|
||||
|
||||
reporter.error(1, f'Failed to download "{missing_video["title"]}", see server logs...')
|
||||
continue
|
||||
|
||||
with get_db_session(commit=True) as session:
|
||||
upsert_video(session, video_path, channel, id_=id_, info_json=info_json)
|
||||
|
||||
reporter.finish(1, 'All videos are downloaded')
|
||||
|
||||
|
||||
def main(args=None):
|
||||
"""Find and download any missing videos. Parse any arguments passed by the cmd-line."""
|
||||
q = Queue()
|
||||
reporter = ProgressReporter(q, 2)
|
||||
update_channels(reporter)
|
||||
download_all_missing_videos(reporter)
|
||||
return 0
|
||||
def get_channel_source_id(url: str) -> str:
|
||||
channel_info = YDL.extract_info(url, download=False, process=False)
|
||||
return channel_info['id']
|
||||
|
|
|
|||
|
|
@ -2,15 +2,15 @@ import asyncio
|
|||
import json
|
||||
import pathlib
|
||||
import re
|
||||
from multiprocessing.queues import Queue
|
||||
from typing import Tuple
|
||||
from uuid import uuid1
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from wrolpi.common import ProgressReporter, save_settings_config, get_absolute_media_path
|
||||
from wrolpi.common import logger
|
||||
from wrolpi.common import logger, chunks
|
||||
from wrolpi.common import save_settings_config
|
||||
from wrolpi.db import get_db_curs, get_db_session
|
||||
from wrolpi.vars import PYTEST
|
||||
from .captions import insert_bulk_captions, process_captions
|
||||
from .common import generate_bulk_posters, get_bulk_video_info_json, get_bulk_video_size, \
|
||||
generate_video_paths, remove_duplicate_video_paths, bulk_validate_posters, \
|
||||
|
|
@ -19,6 +19,8 @@ from .models import Channel, Video
|
|||
|
||||
logger = logger.getChild(__name__)
|
||||
|
||||
DEFAULT_DOWNLOAD_FREQUENCY = 60 * 60 * 24 * 7 # weekly
|
||||
|
||||
|
||||
def refresh_channel_video_captions() -> bool:
|
||||
with get_db_curs() as curs:
|
||||
|
|
@ -86,6 +88,7 @@ def refresh_channel_info_json() -> bool:
|
|||
WHERE
|
||||
v.video_path IS NOT NULL
|
||||
AND v.info_json_path IS NOT NULL
|
||||
AND v.info_json_path != ''
|
||||
AND (v.duration IS NULL OR v.view_count IS NULL)
|
||||
'''
|
||||
curs.execute(query)
|
||||
|
|
@ -128,50 +131,38 @@ def process_video_meta_data():
|
|||
refresh_channel_calculate_size()
|
||||
|
||||
|
||||
def refresh_channel_videos(channel: Channel, reporter: ProgressReporter):
|
||||
def refresh_channel_videos(channel: Channel):
|
||||
"""
|
||||
Find all video files in a channel's directory. Add any videos not in the DB to the DB.
|
||||
"""
|
||||
# This function is hard to predict, so we will simply progress in chunks :(
|
||||
reporter.set_progress_total(1, 7)
|
||||
reporter.send_progress(1, 0, 'Preparing channel.')
|
||||
|
||||
# Set the idempotency key so we can remove any videos not touched during this search
|
||||
with get_db_curs(commit=True) as curs:
|
||||
curs.execute('UPDATE video SET idempotency=NULL WHERE channel_id=%s', (channel.id,))
|
||||
|
||||
reporter.send_progress(1, 1, 'Finding all videos, checking for duplicates.')
|
||||
|
||||
idempotency = str(uuid1())
|
||||
directory = get_absolute_media_path(channel.directory)
|
||||
directory = channel.directory.path
|
||||
|
||||
# A set of absolute paths that exist in the file system
|
||||
possible_new_paths = generate_video_paths(directory)
|
||||
possible_new_paths = remove_duplicate_video_paths(possible_new_paths)
|
||||
|
||||
reporter.send_progress(1, 2, 'Matching all videos to the database.')
|
||||
|
||||
# Update all videos that match the current video paths
|
||||
relative_new_paths = [str(i.relative_to(directory)) for i in possible_new_paths]
|
||||
new_paths = [str(i) for i in possible_new_paths]
|
||||
with get_db_curs(commit=True) as curs:
|
||||
query = 'UPDATE video SET idempotency = %s WHERE channel_id = %s AND video_path = ANY(%s) RETURNING video_path'
|
||||
curs.execute(query, (idempotency, channel.id, relative_new_paths))
|
||||
curs.execute(query, (idempotency, channel.id, new_paths))
|
||||
existing_paths = {i for (i,) in curs.fetchall()}
|
||||
|
||||
reporter.send_progress(1, 3)
|
||||
|
||||
# Get the paths for any video not yet in the DB
|
||||
# (paths in DB are relative, but we need to pass an absolute path)
|
||||
new_videos = {p for p in possible_new_paths if str(p.relative_to(directory)) not in existing_paths}
|
||||
new_videos = {i for i in possible_new_paths if str(i) not in existing_paths}
|
||||
|
||||
reporter.send_progress(1, 4, f'Inserting {len(new_videos)} new videos.')
|
||||
|
||||
for video_path in new_videos:
|
||||
for chunk in chunks(new_videos, 20):
|
||||
with get_db_session(commit=True) as session:
|
||||
upsert_video(session, pathlib.Path(video_path), channel, idempotency=idempotency)
|
||||
logger.debug(f'{channel.name}: Added {video_path}')
|
||||
|
||||
reporter.send_progress(1, 5, 'Deleting unnecessary video entries.')
|
||||
for video_path in chunk:
|
||||
video_path = pathlib.Path(video_path)
|
||||
upsert_video(session, video_path, channel, idempotency=idempotency)
|
||||
logger.debug(f'{channel.name}: Added {video_path.name}')
|
||||
|
||||
with get_db_curs(commit=True) as curs:
|
||||
curs.execute('DELETE FROM video WHERE channel_id=%s AND idempotency IS NULL RETURNING id', (channel.id,))
|
||||
|
|
@ -183,14 +174,50 @@ def refresh_channel_videos(channel: Channel, reporter: ProgressReporter):
|
|||
|
||||
logger.info(f'{channel.name}: {len(new_videos)} new videos, {len(existing_paths)} already existed. ')
|
||||
|
||||
reporter.send_progress(1, 6, f'Updating video view counts')
|
||||
|
||||
update_view_count(channel.id)
|
||||
|
||||
reporter.send_progress(1, 7, f'Processed all videos for {channel.name}')
|
||||
|
||||
def refresh_no_channel_videos():
|
||||
"""
|
||||
Refresh the Videos in the NO CHANNEL directory.
|
||||
"""
|
||||
from modules.videos.downloader import get_no_channel_directory
|
||||
directory = get_no_channel_directory()
|
||||
if not directory.is_dir():
|
||||
return
|
||||
|
||||
logger.info('Refreshing NO CHANNEL videos')
|
||||
|
||||
idempotency = str(uuid1())
|
||||
|
||||
# Get all WROLPi compatible videos, remove any duplicates (different formats).
|
||||
possible_new_paths = generate_video_paths(directory)
|
||||
possible_new_paths = remove_duplicate_video_paths(possible_new_paths)
|
||||
|
||||
new_paths = [str(i) for i in possible_new_paths]
|
||||
with get_db_curs(commit=True) as curs:
|
||||
query = 'UPDATE video SET idempotency = %s WHERE video_path = ANY(%s) RETURNING video_path'
|
||||
curs.execute(query, (idempotency, new_paths))
|
||||
existing_paths = {i for (i,) in curs.fetchall()}
|
||||
|
||||
new_videos = {i for i in possible_new_paths if str(i) not in existing_paths}
|
||||
|
||||
for chunk in chunks(new_videos, 20):
|
||||
with get_db_session(commit=True) as session:
|
||||
for video_path in chunk:
|
||||
upsert_video(session, pathlib.Path(video_path), idempotency=idempotency)
|
||||
logger.debug(f'Added NO CHANNEL video {video_path}')
|
||||
|
||||
with get_db_curs(commit=True) as curs:
|
||||
curs.execute('DELETE FROM video WHERE channel_id IS NULL AND idempotency IS NULL RETURNING id')
|
||||
deleted_count = len(curs.fetchall())
|
||||
|
||||
if deleted_count:
|
||||
deleted_status = f'Deleted {deleted_count} video records in NO CHANNEL.'
|
||||
logger.info(deleted_status)
|
||||
|
||||
|
||||
def _refresh_videos(q: Queue, channel_links: list = None):
|
||||
def _refresh_videos(channel_links: list = None):
|
||||
"""
|
||||
Find any videos in the channel directories and add them to the DB. Delete DB records of any videos not in the
|
||||
file system.
|
||||
|
|
@ -201,10 +228,6 @@ def _refresh_videos(q: Queue, channel_links: list = None):
|
|||
"""
|
||||
logger.info('Refreshing video files')
|
||||
with get_db_session() as session:
|
||||
reporter = ProgressReporter(q, 2)
|
||||
reporter.code('refresh-started')
|
||||
reporter.set_progress_total(0, session.query(Channel).count())
|
||||
|
||||
if channel_links:
|
||||
channels = session.query(Channel).filter(Channel.link.in_(channel_links))
|
||||
else:
|
||||
|
|
@ -218,14 +241,15 @@ def _refresh_videos(q: Queue, channel_links: list = None):
|
|||
raise Exception(f'No channels in DB. Have you created any?')
|
||||
|
||||
for idx, channel in enumerate(channels):
|
||||
reporter.send_progress(0, idx, f'Checking {channel.name} directory for new videos')
|
||||
refresh_channel_videos(channel, reporter)
|
||||
refresh_channel_videos(channel)
|
||||
|
||||
if not channel_links:
|
||||
# Refresh NO CHANNEL videos when not refreshing a specific channel.
|
||||
refresh_no_channel_videos()
|
||||
|
||||
# Fill in any missing data for all videos.
|
||||
process_video_meta_data()
|
||||
|
||||
reporter.send_progress(0, len(channels), 'All videos refreshed.')
|
||||
reporter.code('refresh-complete')
|
||||
if not PYTEST:
|
||||
process_video_meta_data()
|
||||
|
||||
|
||||
def get_channels_config(session: Session) -> dict:
|
||||
|
|
@ -319,43 +343,20 @@ async def get_statistics():
|
|||
return ret
|
||||
|
||||
|
||||
async def get_download_history():
|
||||
"""
|
||||
Get the info_date for all channels that have been downloaded. Order by the most recent download first.
|
||||
"""
|
||||
query = '''
|
||||
SELECT
|
||||
link, name, info_date
|
||||
FROM
|
||||
channel
|
||||
WHERE
|
||||
info_date IS NOT NULL
|
||||
ORDER BY channel.info_date DESC
|
||||
'''
|
||||
with get_db_curs() as curs:
|
||||
curs.execute(query)
|
||||
results = [dict(i) for i in curs.fetchall()]
|
||||
return results
|
||||
|
||||
|
||||
NAME_PARSER = re.compile(r'(.*?)_((?:\d+?)|(?:NA))_(?:(.{11})_)?(.*)\.'
|
||||
r'(jpg|webp|flv|mp4|part|info\.json|description|webm|..\.srt|..\.vtt)')
|
||||
|
||||
|
||||
def upsert_video(session: Session, video_path: pathlib.Path, channel: Channel, idempotency: str = None,
|
||||
skip_captions=False,
|
||||
id_: str = None, info_json: dict = None) -> Video:
|
||||
def upsert_video(session: Session, video_path: pathlib.Path, channel: Channel = None, idempotency: str = None,
|
||||
skip_captions=False, id_: str = None) -> Video:
|
||||
"""
|
||||
Insert a video into the DB. Also, find any meta-files near the video file and store them on the video row.
|
||||
|
||||
If id_ is provided, update that entry.
|
||||
"""
|
||||
channel_dir = get_absolute_media_path(channel.directory)
|
||||
poster_path, description_path, caption_path, info_json_path = find_meta_files(video_path, relative_to=channel_dir)
|
||||
|
||||
# Video paths should be relative to the channel's directory
|
||||
if video_path.is_absolute():
|
||||
video_path = video_path.relative_to(channel_dir)
|
||||
if not video_path.is_absolute():
|
||||
raise ValueError(f'Video path is not absolute: {video_path}')
|
||||
poster_path, description_path, caption_path, info_json_path = find_meta_files(video_path)
|
||||
|
||||
name_match = NAME_PARSER.match(video_path.name)
|
||||
_ = upload_date = source_id = title = ext = None
|
||||
|
|
@ -369,28 +370,33 @@ def upsert_video(session: Session, video_path: pathlib.Path, channel: Channel, i
|
|||
upload_date = None
|
||||
|
||||
duration = None
|
||||
url = None
|
||||
if info_json_path:
|
||||
path = (channel_dir / info_json_path).absolute()
|
||||
try:
|
||||
with open(path) as fh:
|
||||
with info_json_path.open('rt') as fh:
|
||||
json_contents = json.load(fh)
|
||||
duration = json_contents['duration']
|
||||
duration = json_contents.get('duration')
|
||||
url = json_contents.get('webpage_url')
|
||||
except json.decoder.JSONDecodeError:
|
||||
logger.warning(f'Failed to load JSON file to get duration: {path}')
|
||||
logger.warning(f'Failed to load JSON file to get duration: {info_json_path}')
|
||||
|
||||
size = video_path.stat().st_size
|
||||
|
||||
video_dict = dict(
|
||||
channel_id=channel.id,
|
||||
caption_path=str(caption_path) if caption_path else None,
|
||||
channel_id=channel.id if channel else None,
|
||||
description_path=str(description_path) if description_path else None,
|
||||
duration=duration,
|
||||
ext=ext,
|
||||
idempotency=idempotency,
|
||||
info_json_path=str(info_json_path) if info_json_path else None,
|
||||
poster_path=str(poster_path) if poster_path else None,
|
||||
size=size,
|
||||
source_id=source_id,
|
||||
title=title,
|
||||
upload_date=upload_date,
|
||||
url=url,
|
||||
video_path=str(video_path),
|
||||
caption_path=str(caption_path) if caption_path else None,
|
||||
idempotency=idempotency,
|
||||
info_json_path=str(info_json_path) if info_json_path else None,
|
||||
duration=duration,
|
||||
)
|
||||
|
||||
if id_:
|
||||
|
|
@ -410,8 +416,7 @@ def upsert_video(session: Session, video_path: pathlib.Path, channel: Channel, i
|
|||
return video
|
||||
|
||||
|
||||
def find_meta_files(path: pathlib.Path, relative_to=None) -> Tuple[
|
||||
pathlib.Path, pathlib.Path, pathlib.Path, pathlib.Path]:
|
||||
def find_meta_files(path: pathlib.Path) -> Tuple[pathlib.Path, pathlib.Path, pathlib.Path, pathlib.Path]:
|
||||
"""
|
||||
Find all files that share a file's full path, except their extensions. It is assumed that file with the
|
||||
same name, but different extension is related to that file. A None will be yielded if the meta file doesn't exist.
|
||||
|
|
@ -429,11 +434,7 @@ def find_meta_files(path: pathlib.Path, relative_to=None) -> Tuple[
|
|||
for meta_ext in meta_exts:
|
||||
meta_path = path.with_suffix(meta_ext)
|
||||
if meta_path.exists():
|
||||
if relative_to:
|
||||
yield meta_path.relative_to(relative_to)
|
||||
break
|
||||
else:
|
||||
yield meta_path
|
||||
break
|
||||
yield meta_path
|
||||
break
|
||||
else:
|
||||
yield None
|
||||
|
|
|
|||
|
|
@ -1,27 +1,20 @@
|
|||
import json
|
||||
from datetime import timedelta, datetime
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
from typing import Optional, List
|
||||
|
||||
from sqlalchemy import Column, Integer, String, Boolean, JSON, Date, ARRAY, ForeignKey, Computed, types
|
||||
from sqlalchemy import Column, Integer, String, Boolean, JSON, Date, ARRAY, ForeignKey, Computed
|
||||
from sqlalchemy.orm import relationship, Session
|
||||
from sqlalchemy.orm.collections import InstrumentedList
|
||||
|
||||
from wrolpi.common import Base, tsvector, ModelHelper, PathColumn, get_absolute_media_path
|
||||
from wrolpi.dates import now, today, TZDateTime
|
||||
from wrolpi.common import Base, tsvector, ModelHelper, logger
|
||||
from wrolpi.dates import now, TZDateTime
|
||||
from wrolpi.db import get_db_curs
|
||||
from wrolpi.errors import UnknownVideo, NoFrequency, UnknownFile, UnknownDirectory
|
||||
from wrolpi.downloader import Download
|
||||
from wrolpi.errors import UnknownVideo, UnknownFile, UnknownDirectory
|
||||
from wrolpi.media_path import MediaPathType, MediaPath
|
||||
|
||||
|
||||
class ChannelPath(types.TypeDecorator):
|
||||
impl = types.String
|
||||
|
||||
def process_bind_param(self, value, dialect):
|
||||
return str(value) if value else None
|
||||
|
||||
def process_result_value(self, value, dialect):
|
||||
if value:
|
||||
return Path(value)
|
||||
logger = logger.getChild(__name__)
|
||||
|
||||
|
||||
class Video(ModelHelper, Base):
|
||||
|
|
@ -32,12 +25,12 @@ class Video(ModelHelper, Base):
|
|||
idempotency = Column(String)
|
||||
|
||||
# File paths
|
||||
caption_path = Column(ChannelPath)
|
||||
description_path = Column(ChannelPath)
|
||||
caption_path = Column(MediaPathType)
|
||||
description_path = Column(MediaPathType)
|
||||
ext = Column(String)
|
||||
info_json_path = Column(ChannelPath)
|
||||
poster_path = Column(ChannelPath)
|
||||
video_path = Column(ChannelPath)
|
||||
info_json_path = Column(MediaPathType)
|
||||
poster_path = Column(MediaPathType)
|
||||
video_path = Column(MediaPathType)
|
||||
|
||||
caption = Column(String)
|
||||
duration = Column(Integer)
|
||||
|
|
@ -49,12 +42,13 @@ class Video(ModelHelper, Base):
|
|||
validated_poster = Column(Boolean, default=False)
|
||||
viewed = Column(TZDateTime)
|
||||
view_count = Column(Integer)
|
||||
url = Column(String)
|
||||
textsearch = Column(tsvector, Computed('''to_tsvector('english'::regconfig,
|
||||
((COALESCE(title, ''::text) || ' '::text) ||
|
||||
COALESCE(caption, ''::text)))'''))
|
||||
|
||||
def __repr__(self):
|
||||
return f'<Video id={self.id}, title={self.title}, path={self.video_path}, channel={self.channel_id} ' \
|
||||
return f'<Video id={self.id}, title={self.title}, path={self.video_path.path}, channel={self.channel_id} ' \
|
||||
f'source_id={self.source_id}>'
|
||||
|
||||
def dict(self) -> dict:
|
||||
|
|
@ -83,13 +77,13 @@ class Video(ModelHelper, Base):
|
|||
"""
|
||||
Remove all files related to this video. Add it to it's Channel's skip list.
|
||||
|
||||
Raises: UnknownFile if a the video has no files.
|
||||
Raises: UnknownFile if the video has no files.
|
||||
"""
|
||||
from modules.videos.common import get_absolute_video_files
|
||||
video_files = get_absolute_video_files(self)
|
||||
for path in video_files:
|
||||
try:
|
||||
path.unlink()
|
||||
path.path.unlink()
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
|
||||
|
|
@ -117,13 +111,9 @@ class Video(ModelHelper, Base):
|
|||
"""
|
||||
If this Video has an info_json file, return it's contents. Otherwise, return None.
|
||||
"""
|
||||
if not self.channel or not self.channel.directory:
|
||||
return
|
||||
|
||||
try:
|
||||
if self.info_json_path:
|
||||
path = self.channel.get_relative_path(self.info_json_path)
|
||||
with open(path, 'rb') as fh:
|
||||
with open(self.info_json_path.path, 'rb') as fh:
|
||||
contents = json.load(fh)
|
||||
return contents
|
||||
except UnknownFile:
|
||||
|
|
@ -142,12 +132,10 @@ class Video(ModelHelper, Base):
|
|||
if description:
|
||||
return description
|
||||
|
||||
if self.description_path and self.channel_id:
|
||||
path = self.channel.get_relative_path(self.description_path)
|
||||
if path.exists():
|
||||
with open(path, 'rt') as fh:
|
||||
contents = fh.read()
|
||||
return contents
|
||||
if self.description_path:
|
||||
with open(self.description_path, 'rt') as fh:
|
||||
contents = fh.read()
|
||||
return contents
|
||||
|
||||
def get_surrounding_videos(self):
|
||||
"""
|
||||
|
|
@ -215,6 +203,28 @@ class Video(ModelHelper, Base):
|
|||
|
||||
return previous_video, next_video
|
||||
|
||||
def __json__(self):
|
||||
from modules.videos.common import minimize_video_info_json
|
||||
d = dict(
|
||||
caption_path=self.caption_path,
|
||||
channel=self.channel,
|
||||
channel_id=self.channel_id,
|
||||
duration=self.duration,
|
||||
favorite=self.favorite,
|
||||
id=self.id,
|
||||
info_json=minimize_video_info_json(self.get_info_json()) if self.info_json_path else None,
|
||||
poster_path=self.poster_path,
|
||||
size=self.size,
|
||||
source_id=self.source_id,
|
||||
title=self.title,
|
||||
upload_date=self.upload_date,
|
||||
url=self.url,
|
||||
video_path=self.video_path,
|
||||
view_count=self.view_count,
|
||||
viewed=self.viewed,
|
||||
)
|
||||
return d
|
||||
|
||||
|
||||
class Channel(ModelHelper, Base):
|
||||
__tablename__ = 'channel'
|
||||
|
|
@ -224,12 +234,12 @@ class Channel(ModelHelper, Base):
|
|||
idempotency = Column(String)
|
||||
url = Column(String)
|
||||
match_regex = Column(String)
|
||||
directory = Column(PathColumn)
|
||||
directory: MediaPath = Column(MediaPathType)
|
||||
skip_download_videos = Column(ARRAY(String))
|
||||
generate_posters = Column(Boolean)
|
||||
calculate_duration = Column(Boolean)
|
||||
download_frequency = Column(Integer)
|
||||
next_download = Column(Date)
|
||||
source_id = Column(String)
|
||||
|
||||
info_json = Column(JSON)
|
||||
info_date = Column(Date)
|
||||
|
|
@ -240,9 +250,9 @@ class Channel(ModelHelper, Base):
|
|||
videos: InstrumentedList = relationship('Video', primaryjoin='Channel.id==Video.channel_id')
|
||||
|
||||
def __repr__(self):
|
||||
return f'<Channel(id={self.id}, name={self.name})>'
|
||||
return f'<Channel id={self.id}, name={repr(self.name)}>'
|
||||
|
||||
def add_video_to_skip_list(self, source_id):
|
||||
def add_video_to_skip_list(self, source_id: str):
|
||||
if not source_id:
|
||||
raise UnknownVideo(f'Cannot skip video with empty source id: {source_id}')
|
||||
|
||||
|
|
@ -250,36 +260,6 @@ class Channel(ModelHelper, Base):
|
|||
skip_download_videos.add(source_id)
|
||||
self.skip_download_videos = skip_download_videos
|
||||
|
||||
def increment_next_download(self):
|
||||
"""
|
||||
Set the next download predictably during the next download iteration.
|
||||
|
||||
For example, two channels that download weekly will need to be downloaded on different days. We want a channel
|
||||
to always be downloaded on it's day. That may be Monday, or Tuesday, etc.
|
||||
|
||||
This is true for all download frequencies (30 days, 90 days, etc.).
|
||||
|
||||
The order that channels will be downloaded/distributed will be by `link`.
|
||||
"""
|
||||
if not self.download_frequency:
|
||||
raise NoFrequency(f'{self} does not have a frequency!')
|
||||
|
||||
session = Session.object_session(self)
|
||||
|
||||
# All the channels that share the my frequency.
|
||||
channel_group = session.query(self.__class__).filter_by(download_frequency=self.download_frequency)
|
||||
channel_group = list(channel_group.order_by(self.__class__.link).all())
|
||||
|
||||
# My position in the channel group.
|
||||
index = channel_group.index(self)
|
||||
|
||||
# The seconds between each download.
|
||||
chunk = self.download_frequency // len(channel_group)
|
||||
|
||||
# My next download will be distributed by my frequency and my position.
|
||||
position = chunk * (index + 1)
|
||||
self.next_download = today() + timedelta(seconds=self.download_frequency + position)
|
||||
|
||||
def delete_with_videos(self):
|
||||
"""
|
||||
Delete all Video records (but not video files) related to this Channel. Then delete the Channel.
|
||||
|
|
@ -287,6 +267,9 @@ class Channel(ModelHelper, Base):
|
|||
session = Session.object_session(self)
|
||||
# Delete the video records, but not the video files!
|
||||
session.query(Video).filter_by(channel_id=self.id).delete()
|
||||
if download := self.get_download():
|
||||
session.delete(download)
|
||||
|
||||
session.delete(self)
|
||||
|
||||
def update(self, data: dict):
|
||||
|
|
@ -298,35 +281,49 @@ class Channel(ModelHelper, Base):
|
|||
Retrieve the data about this Channel that should be stored in a config file.
|
||||
"""
|
||||
config = dict(
|
||||
directory=str(self.directory),
|
||||
calculate_duration=self.calculate_duration,
|
||||
directory=str(self.directory.path),
|
||||
download_frequency=self.download_frequency,
|
||||
favorites={},
|
||||
generate_posters=self.generate_posters,
|
||||
match_regex=self.match_regex or '',
|
||||
name=self.name,
|
||||
url=self.url or '',
|
||||
generate_posters=self.generate_posters,
|
||||
calculate_duration=self.calculate_duration,
|
||||
skip_download_videos=self.skip_download_videos or [],
|
||||
download_frequency=self.download_frequency,
|
||||
next_download=self.next_download,
|
||||
favorites={},
|
||||
source_id=self.source_id,
|
||||
url=self.url or '',
|
||||
)
|
||||
|
||||
session = Session.object_session(self)
|
||||
favorites = session.query(Video).filter(Video.favorite != None, Video.channel_id == self.id).all()
|
||||
favorites = session.query(Video).filter(Video.favorite != None, Video.channel_id == self.id).all() # noqa
|
||||
favorites: List[Video]
|
||||
if favorites:
|
||||
config['favorites'] = {i.source_id: {'favorite': i.favorite} for i in favorites}
|
||||
config['favorites'] = {i.video_path.path.name: {'favorite': i.favorite} for i in favorites}
|
||||
|
||||
return config
|
||||
|
||||
def get_directory(self):
|
||||
if self._directory:
|
||||
return self._directory
|
||||
|
||||
self._directory = get_absolute_media_path(self.directory)
|
||||
return self._directory
|
||||
|
||||
def get_relative_path(self, path: Path, exists: bool = True):
|
||||
channel_dir = self._directory or self.get_directory()
|
||||
path = channel_dir / path
|
||||
path = self.directory / path
|
||||
if exists and not path.exists():
|
||||
raise FileNotFoundError(f'Channel path {path} does not exist!')
|
||||
raise FileNotFoundError(f'{path} does not exist!')
|
||||
return path
|
||||
|
||||
def get_download(self) -> Optional[Download]:
|
||||
"""
|
||||
Get the Download row for this Channel. If there isn't a Download, return None.
|
||||
"""
|
||||
if not self.url:
|
||||
raise ValueError(f'Channel {self.name} does not have a URL to download!')
|
||||
|
||||
session = Session.object_session(self)
|
||||
download = session.query(Download).filter_by(url=self.url).one_or_none()
|
||||
return download
|
||||
|
||||
def __json__(self):
|
||||
d = dict(
|
||||
id=self.id,
|
||||
name=self.name,
|
||||
directory=self.directory,
|
||||
url=self.url,
|
||||
link=self.link,
|
||||
)
|
||||
return d
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ class ChannelPostRequest:
|
|||
mkdir = doc.Boolean()
|
||||
generate_posters = doc.Boolean()
|
||||
calculate_duration = doc.Boolean()
|
||||
download_frequency = doc.Integer()
|
||||
download_frequency = doc.String()
|
||||
|
||||
|
||||
class ChannelPutRequest:
|
||||
|
|
@ -23,7 +23,7 @@ class ChannelPutRequest:
|
|||
generate_posters = doc.Boolean()
|
||||
calculate_duration = doc.Boolean()
|
||||
mkdir = doc.Boolean()
|
||||
download_frequency = doc.Integer()
|
||||
download_frequency = doc.String()
|
||||
|
||||
|
||||
class RefreshRequest:
|
||||
|
|
@ -170,13 +170,3 @@ class ChannelStatistics:
|
|||
class VideosStatisticsResponse:
|
||||
videos = VideoStatistics
|
||||
channels = ChannelStatistics
|
||||
|
||||
|
||||
class DownloadHistory:
|
||||
link = doc.String()
|
||||
name = doc.String()
|
||||
info_date = doc.Date()
|
||||
|
||||
|
||||
class DownloadHistoryResponse:
|
||||
history = doc.List(doc.Object(DownloadHistory))
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
import pathlib
|
||||
import tempfile
|
||||
from functools import wraps
|
||||
from queue import Queue
|
||||
|
||||
from modules.videos.lib import refresh_channel_videos
|
||||
from modules.videos.models import Channel
|
||||
from wrolpi.common import ProgressReporter, insert_parameter
|
||||
from wrolpi.common import insert_parameter, set_test_media_directory
|
||||
from wrolpi.db import get_db_session
|
||||
from wrolpi.test.common import wrap_test_db, build_test_directories
|
||||
|
||||
|
|
@ -37,9 +38,11 @@ def create_channel_structure(structure):
|
|||
@wraps(func)
|
||||
@wrap_test_db
|
||||
def wrapped(*args, **kwargs):
|
||||
# Dummy queue and reporter to receive messages.
|
||||
q = Queue()
|
||||
reporter = ProgressReporter(q, 2)
|
||||
if args and (test := args[0]) and hasattr(test, 'tmp_dir'):
|
||||
tmp_dir = pathlib.Path(test.tmp_dir.name)
|
||||
else:
|
||||
tmp_dir = pathlib.Path(tempfile.TemporaryDirectory().name)
|
||||
set_test_media_directory(tmp_dir)
|
||||
|
||||
# Convert the channel/video structure to a file structure for the test.
|
||||
file_structure = []
|
||||
|
|
@ -48,7 +51,7 @@ def create_channel_structure(structure):
|
|||
file_structure.append(f'{channel}/{path}')
|
||||
file_structure.append(f'{channel}/')
|
||||
|
||||
with build_test_directories(file_structure) as tempdir:
|
||||
with build_test_directories(file_structure, tmp_dir) as tempdir:
|
||||
args, kwargs = insert_parameter(func, 'tempdir', tempdir, args, kwargs)
|
||||
|
||||
with get_db_session(commit=True) as session:
|
||||
|
|
@ -60,9 +63,12 @@ def create_channel_structure(structure):
|
|||
|
||||
with get_db_session(commit=True) as session:
|
||||
for channel_ in session.query(Channel).all():
|
||||
refresh_channel_videos(channel_, reporter)
|
||||
refresh_channel_videos(channel_)
|
||||
|
||||
return func(*args, **kwargs)
|
||||
try:
|
||||
return func(*args, **kwargs)
|
||||
finally:
|
||||
set_test_media_directory(None)
|
||||
|
||||
return wrapped
|
||||
|
||||
|
|
|
|||
|
|
@ -1,15 +1,22 @@
|
|||
import mock
|
||||
from pathlib import Path
|
||||
|
||||
from modules.videos import captions
|
||||
from modules.videos.models import Video
|
||||
from wrolpi.db import get_db_session, get_db_curs
|
||||
from wrolpi.db import get_db_curs, get_db_context
|
||||
from wrolpi.test.common import wrap_test_db, TestAPI
|
||||
from wrolpi.vars import PROJECT_DIR
|
||||
|
||||
|
||||
class TestCaption(TestAPI):
|
||||
vtt_path1 = (PROJECT_DIR / 'test/example1.en.vtt').absolute()
|
||||
vtt_path2 = (PROJECT_DIR / 'test/example2.en.vtt').absolute()
|
||||
|
||||
def setUp(self) -> None:
|
||||
super().setUp()
|
||||
self.vtt_path1 = Path(self.tmp_dir.name) / 'example1.en.vtt'
|
||||
self.vtt_path1.touch()
|
||||
self.vtt_path1.write_text((PROJECT_DIR / 'test/example1.en.vtt').read_text())
|
||||
self.vtt_path2 = Path(self.tmp_dir.name) / 'example2.en.vtt'
|
||||
self.vtt_path2.touch()
|
||||
self.vtt_path2.write_text((PROJECT_DIR / 'test/example2.en.vtt').read_text())
|
||||
|
||||
def test_example_en_vtt(self):
|
||||
expected = ['okay welcome to this session this is',
|
||||
|
|
@ -35,25 +42,26 @@ class TestCaption(TestAPI):
|
|||
|
||||
@wrap_test_db
|
||||
def test_process_captions(self):
|
||||
with get_db_session(commit=True) as session:
|
||||
video1 = Video(title='scream', caption_path=str(self.vtt_path1))
|
||||
session.add(video1)
|
||||
with mock.patch('modules.videos.captions.get_absolute_video_caption', lambda *a: self.vtt_path1):
|
||||
captions.process_captions(video1)
|
||||
video2 = Video(title='bar', caption_path=str(self.vtt_path2))
|
||||
session.add(video2)
|
||||
with mock.patch('modules.videos.captions.get_absolute_video_caption', lambda *a: self.vtt_path2):
|
||||
captions.process_captions(video2)
|
||||
_, session = get_db_context()
|
||||
video1 = Video(title='scream', caption_path=str(self.vtt_path1))
|
||||
session.add(video1)
|
||||
session.commit()
|
||||
captions.process_captions(video1)
|
||||
video2 = Video(title='bar', caption_path=str(self.vtt_path2))
|
||||
session.add(video2)
|
||||
session.commit()
|
||||
captions.process_captions(video2)
|
||||
|
||||
session.flush()
|
||||
session.refresh(video1)
|
||||
session.refresh(video2)
|
||||
session.commit()
|
||||
session.flush()
|
||||
session.refresh(video1)
|
||||
session.refresh(video2)
|
||||
|
||||
# Get the video from the DB
|
||||
video1 = session.query(Video).filter_by(id=video1.id).one()
|
||||
self.assertIsNotNone(video1.caption)
|
||||
video2 = session.query(Video).filter_by(id=video2.id).one()
|
||||
self.assertIsNotNone(video2.caption)
|
||||
# Get the video from the DB
|
||||
video1 = session.query(Video).filter_by(id=video1.id).one()
|
||||
self.assertIsNotNone(video1.caption)
|
||||
video2 = session.query(Video).filter_by(id=video2.id).one()
|
||||
self.assertIsNotNone(video2.caption)
|
||||
|
||||
# Search using the tsvector, "sessions" never actually appears in the text, but "session" does
|
||||
with get_db_curs() as curs:
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import pathlib
|
|||
import subprocess
|
||||
import tempfile
|
||||
from unittest import mock
|
||||
from unittest.mock import Mock
|
||||
from unittest.mock import Mock, MagicMock
|
||||
|
||||
import pytest
|
||||
from PIL import Image
|
||||
|
|
@ -11,17 +11,17 @@ from modules.videos.models import Channel, Video
|
|||
from modules.videos.test.common import create_channel_structure
|
||||
from wrolpi.common import get_absolute_media_path
|
||||
from wrolpi.db import get_db_session
|
||||
from wrolpi.test.common import ExtendedTestCase, build_test_directories, wrap_test_db
|
||||
from wrolpi.test.common import build_test_directories, wrap_test_db, TestAPI
|
||||
from wrolpi.vars import PROJECT_DIR
|
||||
from ..common import get_matching_directories, convert_image, bulk_validate_posters, remove_duplicate_video_paths, \
|
||||
update_view_count, get_video_duration
|
||||
|
||||
|
||||
class TestCommon(ExtendedTestCase):
|
||||
class TestCommon(TestAPI):
|
||||
|
||||
def test_get_absolute_media_path(self):
|
||||
wrolpi = get_absolute_media_path('videos/wrolpi')
|
||||
assert str(wrolpi).endswith('wrolpi')
|
||||
path = get_absolute_media_path('videos')
|
||||
self.assertTrue(str(path).endswith('videos'))
|
||||
|
||||
def test_matching_directories(self):
|
||||
structure = [
|
||||
|
|
@ -59,13 +59,13 @@ class TestCommon(ExtendedTestCase):
|
|||
"""
|
||||
Video duration can be retrieved from the video file.
|
||||
"""
|
||||
video_path = PROJECT_DIR / 'test/videos/wrolpi/big_buck_bunny_720p_1mb.mp4'
|
||||
video_path = PROJECT_DIR / 'test/big_buck_bunny_720p_1mb.mp4'
|
||||
self.assertEqual(get_video_duration(video_path), 5)
|
||||
|
||||
video_path = PROJECT_DIR / 'test/videos/wrolpi/does not exist.mp4'
|
||||
video_path = PROJECT_DIR / 'test/does not exist.mp4'
|
||||
self.assertRaises(FileNotFoundError, get_video_duration, video_path)
|
||||
|
||||
video_path = str(PROJECT_DIR / 'test/videos/wrolpi/does not exist.mp4')
|
||||
video_path = str(PROJECT_DIR / 'test/does not exist.mp4')
|
||||
self.assertRaises(FileNotFoundError, get_video_duration, video_path)
|
||||
|
||||
with tempfile.NamedTemporaryFile() as fh:
|
||||
|
|
@ -139,7 +139,7 @@ def test_convert_image():
|
|||
)
|
||||
def test_create_db_structure(_structure, paths):
|
||||
@create_channel_structure(_structure)
|
||||
def test_func(tempdir):
|
||||
def test_func(_, tempdir):
|
||||
assert isinstance(tempdir, pathlib.Path)
|
||||
for path in paths:
|
||||
path = (tempdir / path)
|
||||
|
|
@ -154,7 +154,12 @@ def test_create_db_structure(_structure, paths):
|
|||
assert channel.directory == tempdir / channel_name
|
||||
assert len(channel.videos) == len([i for i in _structure[channel_name] if i.endswith('mp4')])
|
||||
|
||||
test_func()
|
||||
with tempfile.TemporaryDirectory() as tmp_dir, \
|
||||
mock.patch('wrolpi.media_path.get_media_directory') as mock_get_directory:
|
||||
mock_get_directory.return_value = pathlib.Path(tmp_dir)
|
||||
test_ = MagicMock()
|
||||
test_.tmp_dir.name = tmp_dir
|
||||
test_func(test_)
|
||||
|
||||
|
||||
@wrap_test_db
|
||||
|
|
@ -184,10 +189,10 @@ def test_bulk_replace_invalid_posters(tempdir: pathlib.Path):
|
|||
assert Image.open(webp_fh).format == 'WEBP'
|
||||
|
||||
with get_db_session() as session:
|
||||
vid1 = session.query(Video).filter_by(poster_path='vid1.jpg').one()
|
||||
vid1 = session.query(Video).filter_by(poster_path=f'{tempdir}/channel1/vid1.jpg').one()
|
||||
assert vid1.validated_poster is False
|
||||
|
||||
vid2 = session.query(Video).filter_by(poster_path='vid2.webp').one()
|
||||
vid2 = session.query(Video).filter_by(poster_path=f'{tempdir}/channel2/vid2.webp').one()
|
||||
assert vid2.validated_poster is False
|
||||
|
||||
# Convert the WEBP image. convert_image() should only be called once.
|
||||
|
|
@ -201,13 +206,13 @@ def test_bulk_replace_invalid_posters(tempdir: pathlib.Path):
|
|||
with get_db_session() as session:
|
||||
# Get the video by ID because it's poster is now a JPEG.
|
||||
vid2 = session.query(Video).filter_by(id=vid2.id).one()
|
||||
assert str(vid2.poster_path) == 'vid2.jpg'
|
||||
assert all('webp' not in str(i.poster_path) for i in session.query(Video).all())
|
||||
assert str(vid2.poster_path.path).split('/')[-1] == 'vid2.jpg'
|
||||
assert all('webp' not in str(i.poster_path.path) for i in session.query(Video).all())
|
||||
assert vid2.validated_poster is True
|
||||
|
||||
# Vid1's image was validated, but not converted.
|
||||
vid1 = session.query(Video).filter_by(id=vid1.id).one()
|
||||
assert str(vid1.poster_path) == 'vid1.jpg'
|
||||
assert str(vid1.poster_path.path).split('/')[-1] == 'vid1.jpg'
|
||||
assert vid1.validated_poster is True
|
||||
|
||||
# Old webp was removed
|
||||
|
|
@ -240,9 +245,9 @@ def test_bulk_replace_invalid_posters(tempdir: pathlib.Path):
|
|||
)
|
||||
def test_update_view_count(tempdir: pathlib.Path):
|
||||
def check_view_counts(view_counts):
|
||||
with get_db_session(commit=True) as session:
|
||||
with get_db_session() as session_:
|
||||
for source_id, view_count in view_counts.items():
|
||||
vid = session.query(Video).filter_by(source_id=source_id).one()
|
||||
vid = session_.query(Video).filter_by(source_id=source_id).one()
|
||||
assert vid.view_count == view_count
|
||||
|
||||
with get_db_session(commit=True) as session:
|
||||
|
|
@ -251,9 +256,9 @@ def test_update_view_count(tempdir: pathlib.Path):
|
|||
channel2.info_json = {'entries': [{'id': 'vid2.mp4', 'view_count': 11}, {'id': 'bad_id', 'view_count': 12}]}
|
||||
channel3.info_json = {'entries': [{'id': 'vid3.mp4', 'view_count': 13}, {'id': 'vid4.mp4', 'view_count': 14}]}
|
||||
|
||||
# Use the video_path as a unique source_id
|
||||
# Use the video file name as a unique source_id
|
||||
for v in session.query(Video).all():
|
||||
v.source_id = str(v.video_path)
|
||||
v.source_id = str(v.video_path.path).split('/')[-1]
|
||||
|
||||
# Check all videos are empty.
|
||||
check_view_counts({'vid1.mp4': None, 'vid2.mp4': None, 'vid3.mp4': None, 'vid4.mp4': None})
|
||||
|
|
|
|||
|
|
@ -1,17 +1,18 @@
|
|||
import time
|
||||
from datetime import timedelta
|
||||
from queue import Queue
|
||||
import pathlib
|
||||
from copy import copy
|
||||
from datetime import datetime
|
||||
from unittest import mock
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
from yt_dlp.utils import UnsupportedError
|
||||
|
||||
from modules.videos.downloader import update_channels, find_all_missing_videos, download_video
|
||||
from modules.videos.channel.lib import spread_channel_downloads
|
||||
from modules.videos.downloader import find_all_missing_videos, VideoDownloader, \
|
||||
ChannelDownloader, get_or_create_channel
|
||||
from modules.videos.models import Channel, Video
|
||||
from modules.videos.test.common import create_channel_structure
|
||||
from wrolpi.common import ProgressReporter
|
||||
from wrolpi.dates import today
|
||||
from wrolpi.db import get_db_session
|
||||
from wrolpi.dates import local_timezone
|
||||
from wrolpi.db import get_db_session, get_db_context
|
||||
from wrolpi.downloader import DownloadManager, Download, DownloadFrequency
|
||||
from wrolpi.test.common import wrap_test_db, TestAPI
|
||||
|
||||
|
||||
|
|
@ -26,67 +27,13 @@ class FakeYDL:
|
|||
}
|
||||
|
||||
|
||||
@wrap_test_db
|
||||
@create_channel_structure(
|
||||
{
|
||||
'channel1': ['vid1.mp4'],
|
||||
'channel2': ['vid2.mp4']
|
||||
},
|
||||
)
|
||||
def test_update_channels(tempdir):
|
||||
"""
|
||||
Channels are only updated if their "next_download" has expired, or if they have never been updated.
|
||||
"""
|
||||
q = Queue()
|
||||
reporter = ProgressReporter(q, 2)
|
||||
|
||||
with get_db_session(commit=True) as session:
|
||||
channel1, channel2 = session.query(Channel).order_by(Channel.id).all()
|
||||
channel1.url = channel2.url = 'some url'
|
||||
|
||||
with get_db_session() as session:
|
||||
channel1, channel2 = session.query(Channel).order_by(Channel.id).all()
|
||||
with mock.patch('modules.videos.downloader.update_channel') as update_channel:
|
||||
update_channel: MagicMock
|
||||
update_channels(reporter)
|
||||
|
||||
# Both channels would have been updated.
|
||||
assert update_channel.call_count == 2
|
||||
update_channel.assert_any_call(channel1)
|
||||
update_channel.assert_any_call(channel2)
|
||||
|
||||
with get_db_session() as session:
|
||||
channel1, channel2 = session.query(Channel).order_by(Channel.id).all()
|
||||
channel1.next_download = today() + timedelta(days=1)
|
||||
session.commit()
|
||||
|
||||
with mock.patch('modules.videos.downloader.update_channel') as update_channel:
|
||||
update_channel: MagicMock
|
||||
update_channels(reporter)
|
||||
|
||||
# Channel1 is not ready for an update
|
||||
update_channel.assert_called_once()
|
||||
update_channel.assert_called_with(channel2)
|
||||
|
||||
channel1.next_download = today() + timedelta(days=1)
|
||||
channel2.next_download = today() + timedelta(days=1)
|
||||
session.commit()
|
||||
|
||||
with mock.patch('modules.videos.downloader.update_channel') as update_channel:
|
||||
update_channel: MagicMock
|
||||
update_channels(reporter)
|
||||
|
||||
# No channels needed to be updated
|
||||
assert update_channel.call_count == 0
|
||||
|
||||
|
||||
class TestDownloader(TestAPI):
|
||||
@wrap_test_db
|
||||
@create_channel_structure(
|
||||
{
|
||||
'channel1': ['vid1.mp4'],
|
||||
'channel2': ['vid2.mp4']
|
||||
},
|
||||
}
|
||||
)
|
||||
def test_find_all_missing_videos(self, tempdir):
|
||||
with get_db_session(commit=True) as session:
|
||||
|
|
@ -106,103 +53,310 @@ class TestDownloader(TestAPI):
|
|||
self.assertEqual(len(missing), 1)
|
||||
|
||||
# The video returned is the one we faked.
|
||||
channel, id_, entry = missing[0]
|
||||
self.assertEqual(channel, channel1)
|
||||
id_, source_id, entry = missing[0]
|
||||
# Two videos were created for this test already.
|
||||
self.assertEqual(id_, 3)
|
||||
# The fake entry we added is regurgitated back.
|
||||
self.assertEqual(entry, channel1.info_json['entries'][0])
|
||||
|
||||
@pytest.mark.skip
|
||||
def test_download_timeout(self):
|
||||
class FakeYoutubeDL:
|
||||
def __init__(self, *a, **kw):
|
||||
pass
|
||||
|
||||
def add_default_info_extractors(self):
|
||||
pass
|
||||
example_video_json = {
|
||||
'age_limit': 0,
|
||||
'average_rating': 4.6923075,
|
||||
'categories': ['Howto & Style'],
|
||||
'channel': 'channel name',
|
||||
'channel_id': 'channel id',
|
||||
'channel_url': 'channel url',
|
||||
'duration': 635,
|
||||
'extractor': 'youtube',
|
||||
'extractor_key': 'Youtube',
|
||||
'id': 'some long id',
|
||||
'is_live': None,
|
||||
'like_count': 24,
|
||||
'subtitles': {},
|
||||
'title': "The video's title",
|
||||
'upload_date': '20190707',
|
||||
'uploader': 'uploader name',
|
||||
'uploader_id': 'uploader id',
|
||||
'uploader_url': 'uploader url',
|
||||
'view_count': 406,
|
||||
'webpage_url': 'webpage url',
|
||||
'webpage_url_basename': 'watch',
|
||||
}
|
||||
|
||||
def extract_info(self, *a, **kw):
|
||||
# Sleep longer than the 1 second timeout.
|
||||
for i in range(10):
|
||||
time.sleep(1)
|
||||
example_channel_json = {
|
||||
'_type': 'playlist',
|
||||
'entries': [
|
||||
{'_type': 'url',
|
||||
'description': None,
|
||||
'duration': None,
|
||||
'id': 'video 1 id',
|
||||
'ie_key': 'Youtube',
|
||||
'title': 'video 1 title',
|
||||
'uploader': None,
|
||||
'url': 'video 1 url',
|
||||
'view_count': 58504},
|
||||
{'_type': 'url',
|
||||
'description': None,
|
||||
'duration': None,
|
||||
'id': 'video 2 id',
|
||||
'ie_key': 'Youtube',
|
||||
'title': 'video 2 title',
|
||||
'uploader': None,
|
||||
'url': 'video 2 url',
|
||||
'view_count': 1413},
|
||||
],
|
||||
'extractor': 'youtube:tab',
|
||||
'extractor_key': 'YoutubeTab',
|
||||
'id': 'some id',
|
||||
'title': 'channel title',
|
||||
'webpage_url': 'channel url',
|
||||
'webpage_url_basename': 'videos',
|
||||
}
|
||||
|
||||
with mock.patch('api.timeout.TEST_TIMEOUT', 1), \
|
||||
mock.patch('modules.videos.downloader.YoutubeDL', FakeYoutubeDL), \
|
||||
mock.patch('modules.videos.downloader.get_absolute_media_path'):
|
||||
channel = Channel()
|
||||
self.assertRaises(TimeoutError, download_video, channel, {'id': 1})
|
||||
|
||||
# {key: 'daily', text: 'Daily', value: 86400},
|
||||
# {key: 'weekly', text: 'Weekly', value: 604800},
|
||||
# {key: 'biweekly', text: 'Biweekly', value: 1209600},
|
||||
# {key: '30days', text: '30 Days', value: 2592000},
|
||||
# {key: '90days', text: '90 Days', value: 7776000},
|
||||
class TestVideosDownloaders(TestAPI):
|
||||
|
||||
def setUp(self) -> None:
|
||||
super().setUp()
|
||||
self.vd = VideoDownloader()
|
||||
self.cd = ChannelDownloader()
|
||||
self.mgr = DownloadManager()
|
||||
self.mgr.register_downloader(self.vd)
|
||||
self.mgr.register_downloader(self.cd)
|
||||
|
||||
self.videos_dir = pathlib.Path(self.tmp_dir.name) / 'videos'
|
||||
self.videos_dir.mkdir()
|
||||
|
||||
def test_video_valid_url(self):
|
||||
# A specific video can be downloaded.
|
||||
self.assertTrue(self.vd.valid_url('https://www.youtube.com/watch?v=31jPEBiAC3c'))
|
||||
|
||||
# A channel cannot be downloaded.
|
||||
self.assertFalse(self.vd.valid_url('https://www.youtube.com/c/LearningSelfReliance/videos'))
|
||||
self.assertFalse(self.vd.valid_url('https://www.youtube.com/c/LearningSelfReliance'))
|
||||
|
||||
def test_channel_valid_url(self):
|
||||
# An entire domain cannot be downloaded.
|
||||
self.assertFalse(self.cd.valid_url('https://example.com'))
|
||||
self.assertFalse(self.cd.valid_url('https://youtube.com'))
|
||||
|
||||
# Cannot download a single video.
|
||||
self.assertFalse(self.cd.valid_url('https://www.youtube.com/watch?v=31jPEBiAC3c'))
|
||||
|
||||
# Can download entire channels.
|
||||
self.assertTrue(self.cd.valid_url('https://www.youtube.com/c/LearningSelfReliance/videos'))
|
||||
self.assertTrue(self.cd.valid_url('https://www.youtube.com/c/LearningSelfReliance'))
|
||||
|
||||
# Can download entire playlists.
|
||||
self.assertTrue(self.cd.valid_url('https://www.youtube.com/playlist?list=PLCdlMQeP-TbG12nkBCt0E96yr3EfwcH4E'))
|
||||
|
||||
def test_get_downloader(self):
|
||||
# The correct Downloader is gotten.
|
||||
self.assertEqual(self.mgr.get_downloader('https://www.youtube.com/c/LearningSelfReliance/videos'), self.cd)
|
||||
self.assertEqual(self.mgr.get_downloader('https://www.youtube.com/c/LearningSelfReliance'), self.cd)
|
||||
self.assertEqual(self.mgr.get_downloader('https://www.youtube.com/watch?v=31jPEBiAC3c'), self.vd)
|
||||
|
||||
@staticmethod
|
||||
def _make_video_files(channel_dir):
|
||||
video_path = channel_dir / 'video.mp4'
|
||||
poster_path = channel_dir / 'video.jpg'
|
||||
description_path = channel_dir / 'video.description'
|
||||
info_json_path = channel_dir / 'video.info.json'
|
||||
video_path.touch()
|
||||
poster_path.touch()
|
||||
description_path.touch()
|
||||
info_json_path.touch()
|
||||
return video_path, poster_path, description_path, info_json_path
|
||||
|
||||
@wrap_test_db
|
||||
@create_channel_structure(
|
||||
{
|
||||
'channel1': [], # More channels than days in a week.
|
||||
'channel2': [],
|
||||
'channel3': [],
|
||||
'channel4': [],
|
||||
'channel5': [],
|
||||
'channel6': [],
|
||||
'channel7': [],
|
||||
'channel8': [],
|
||||
'channel9': [],
|
||||
},
|
||||
)
|
||||
@mock.patch('modules.videos.downloader.YDL', FakeYDL())
|
||||
def test_update_channel_order(self, tempdir):
|
||||
q = Queue()
|
||||
reporter = ProgressReporter(q, 2)
|
||||
def test_video_download(self):
|
||||
channel_dir = self.videos_dir / 'channel name'
|
||||
channel_dir.mkdir(parents=True)
|
||||
|
||||
# All channels are downloaded weekly.
|
||||
# Video paths
|
||||
video_path, poster_path, description_path, info_json_path = self._make_video_files(channel_dir)
|
||||
|
||||
with get_db_session() as session:
|
||||
url = 'https://www.youtube.com/watch?v=31jPEBiAC3c'
|
||||
with mock.patch('modules.videos.downloader.YDL.extract_info') as mock_extract_info, \
|
||||
mock.patch('modules.videos.downloader.VideoDownloader.prepare_filename') as mock_prepare_filename, \
|
||||
mock.patch('modules.videos.downloader.VideoDownloader.process_runner') as mock_process_runner:
|
||||
mock_extract_info.return_value = example_video_json
|
||||
mock_prepare_filename.return_value = (video_path, {'id': 'foo'})
|
||||
mock_process_runner.return_value = 0
|
||||
|
||||
self.mgr.create_download(url, session)
|
||||
|
||||
mock_process_runner.assert_called_once()
|
||||
video_url, _, out_dir = mock_process_runner.call_args[0]
|
||||
|
||||
download: Download = session.query(Download).one()
|
||||
self.assertEqual(video_url, download.url)
|
||||
channel: Channel = session.query(Channel).one()
|
||||
self.assertEqual(channel.directory, channel_dir)
|
||||
|
||||
video: Video = session.query(Video).one()
|
||||
self.assertEqual(video.video_path, video_path)
|
||||
self.assertEqual(video.description_path, description_path)
|
||||
self.assertEqual(video.poster_path, poster_path)
|
||||
self.assertEqual(video.info_json_path, info_json_path)
|
||||
self.assertTrue(video.video_path.path.is_absolute())
|
||||
self.assertTrue(video.poster_path.path.is_absolute())
|
||||
self.assertTrue(video.description_path.path.is_absolute())
|
||||
self.assertTrue(video.info_json_path.path.is_absolute())
|
||||
|
||||
@wrap_test_db
|
||||
def test_video_download_no_channel(self):
|
||||
_, session = get_db_context()
|
||||
|
||||
channel_dir = self.videos_dir / 'NO CHANNEL'
|
||||
channel_dir.mkdir(parents=True)
|
||||
|
||||
# Video paths
|
||||
video_path, _, _, _ = self._make_video_files(channel_dir)
|
||||
|
||||
# Video has no channel
|
||||
info_json = copy(example_video_json)
|
||||
del info_json['channel']
|
||||
del info_json['channel_id']
|
||||
del info_json['channel_url']
|
||||
|
||||
url = 'https://www.youtube.com/watch?v=31jPEBiAC3c'
|
||||
with mock.patch('modules.videos.downloader.YDL.extract_info') as mock_extract_info, \
|
||||
mock.patch('modules.videos.downloader.VideoDownloader.prepare_filename') as mock_prepare_filename, \
|
||||
mock.patch('modules.videos.downloader.VideoDownloader.process_runner') as mock_process_runner:
|
||||
mock_extract_info.return_value = info_json
|
||||
mock_prepare_filename.return_value = (video_path, {'id': 'foo'})
|
||||
mock_process_runner.return_value = 0
|
||||
|
||||
self.mgr.create_download(url, session)
|
||||
|
||||
mock_process_runner.assert_called_once()
|
||||
|
||||
video: Video = session.query(Video).one()
|
||||
self.assertEqual(str(video.video_path.path), f'{channel_dir}/video.mp4')
|
||||
|
||||
@wrap_test_db
|
||||
def test_channel_download(self):
|
||||
_, session = get_db_context()
|
||||
|
||||
url = 'https://www.youtube.com/c/LearningSelfReliance/videos'
|
||||
session.add(Channel(url=url, link='foo'))
|
||||
|
||||
channel_dir = self.videos_dir / 'NO CHANNEL'
|
||||
channel_dir.mkdir(parents=True)
|
||||
video_path, _, _, _ = self._make_video_files(channel_dir)
|
||||
|
||||
with mock.patch('modules.videos.downloader.YDL.extract_info') as mock_extract_info, \
|
||||
mock.patch('modules.videos.downloader.VideoDownloader.prepare_filename') as mock_prepare_filename, \
|
||||
mock.patch('modules.videos.downloader.VideoDownloader.process_runner') as mock_process_runner:
|
||||
mock_extract_info.return_value = example_channel_json
|
||||
mock_prepare_filename.return_value = (video_path, {'id': 'foo'})
|
||||
mock_process_runner.return_value = 0
|
||||
self.mgr.create_download(url, session)
|
||||
|
||||
# Two videos are in the example channel.
|
||||
downloads = self.mgr.get_new_downloads(session)
|
||||
expected = ['https://youtube.com/watch?v=HQ_62YwcA80', 'https://youtube.com/watch?v=4gSz6W4Gv-o']
|
||||
for download, expected in zip(downloads, expected):
|
||||
self.assertEqual(download.url, expected)
|
||||
# Download is skipped for test.
|
||||
self.assertEqual(download.status, 'new')
|
||||
|
||||
@wrap_test_db
|
||||
def test_invalid_download_url(self):
|
||||
_, session = get_db_context()
|
||||
with mock.patch('modules.videos.downloader.VideoDownloader.valid_url') as mock_valid_url, \
|
||||
mock.patch('modules.videos.downloader.YDL') as mock_ydl:
|
||||
mock_valid_url.return_value = True
|
||||
mock_ydl.extract_info.side_effect = UnsupportedError('oops')
|
||||
self.mgr.create_download('url', session)
|
||||
|
||||
@wrap_test_db
|
||||
@mock.patch('modules.videos.channel.lib.today', lambda: local_timezone(datetime(2020, 1, 1, 0, 0, 0)))
|
||||
def test_spread_channel_downloads(self):
|
||||
with get_db_session(commit=True) as session:
|
||||
channels = list(session.query(Channel).order_by(Channel.link).all())
|
||||
for channel in channels:
|
||||
channel.download_frequency = 604800 # one week
|
||||
channel.url = 'some url'
|
||||
self.assertEqual(channel.next_download, None)
|
||||
channel1 = Channel(name='channel1', link='channel1', url='https://example.com/1',
|
||||
download_frequency=DownloadFrequency.weekly)
|
||||
channel2 = Channel(name='channel2', link='channel2', url='https://example.com/2',
|
||||
download_frequency=DownloadFrequency.weekly)
|
||||
session.add_all([channel1, channel2])
|
||||
|
||||
# Channels will download on different days the next week. Except where there are not enough days.
|
||||
update_channels(reporter)
|
||||
days = [7, 8, 9, 10, 10, 11, 12, 13, 14]
|
||||
for days_, channel in zip(days, channels):
|
||||
nd, expected = channel.next_download, today() + timedelta(days=days_)
|
||||
self.assertEqual(nd, expected, f'Expected {(nd - expected).days} days for {channel}')
|
||||
def check_frequencies(expected):
|
||||
with get_db_session(commit=True) as session:
|
||||
downloads = list(session.query(Download).all())
|
||||
self.assertLength(downloads, expected)
|
||||
for download in downloads:
|
||||
next_download = expected[download.url]
|
||||
self.assertEqual(next_download, str(download.next_download),
|
||||
f'{download.url} frequency {next_download} != {download.next_download}')
|
||||
|
||||
# Even though all channels aren't updated, their order is the same.
|
||||
with get_db_session(commit=True):
|
||||
channels[0].next_download = None
|
||||
channels[1].next_download = None
|
||||
channels[4].next_download = None
|
||||
channels[8].next_download = None
|
||||
update_channels(reporter)
|
||||
for days_, channel in zip(days, channels):
|
||||
self.assertEqual(channel.next_download, today() + timedelta(days=days_))
|
||||
# Spread the downloads out.
|
||||
spread_channel_downloads()
|
||||
|
||||
expected = {
|
||||
'https://example.com/1': '2020-01-08 00:00:00-07:00',
|
||||
'https://example.com/2': '2020-01-11 12:00:00-07:00',
|
||||
}
|
||||
check_frequencies(expected)
|
||||
|
||||
# Multiple frequencies are supported.
|
||||
with get_db_session(commit=True) as session:
|
||||
frequency_map = {
|
||||
'channel1': 604800, # weekly
|
||||
'channel2': 604800,
|
||||
'channel3': 1209600, # bi-weekly
|
||||
'channel4': 1209600,
|
||||
'channel5': 2592000, # 30 days
|
||||
'channel6': 2592000,
|
||||
'channel7': 2592000,
|
||||
'channel8': 2592000,
|
||||
'channel9': 2592000,
|
||||
}
|
||||
for channel in channels:
|
||||
channel.download_frequency = frequency_map[channel.link]
|
||||
channel.next_download = None
|
||||
update_channels(reporter)
|
||||
days = [10, 14, 21, 28,
|
||||
36, 42, 48, 54, 60 # 6 days apart = 30 days / 5
|
||||
]
|
||||
for days_, channel in zip(days, channels):
|
||||
nd, expected = channel.next_download, today() + timedelta(days=days_)
|
||||
self.assertEqual(nd, expected, f'Expected {(nd - expected).days} days for {channel}')
|
||||
channel3 = Channel(name='channel3', link='channel3', url='https://example.com/3',
|
||||
download_frequency=DownloadFrequency.daily)
|
||||
channel4 = Channel(name='channel4', link='channel4', url='https://example.com/4',
|
||||
download_frequency=DownloadFrequency.weekly)
|
||||
# 5 and 6 should be ignored.
|
||||
channel5 = Channel(name='channel5', link='channel5', url='https://example.com/5')
|
||||
channel6 = Channel(name='channel6', link='channel6', download_frequency=DownloadFrequency.daily)
|
||||
session.add_all([channel3, channel4, channel5, channel6])
|
||||
|
||||
spread_channel_downloads()
|
||||
expected = {
|
||||
'https://example.com/1': '2020-01-08 00:00:00-07:00',
|
||||
'https://example.com/2': '2020-01-10 08:00:00-07:00',
|
||||
'https://example.com/3': '2020-01-02 00:00:00-07:00',
|
||||
'https://example.com/4': '2020-01-12 16:00:00-07:00',
|
||||
}
|
||||
check_frequencies(expected)
|
||||
|
||||
@wrap_test_db
|
||||
def test_get_or_create_channel(self):
|
||||
"""
|
||||
A Channel may need to be created for an arbitrary download. Attempt to use an existing Channel if we can
|
||||
match it.
|
||||
"""
|
||||
with get_db_session(commit=True) as session:
|
||||
c1 = Channel(name='foo', link='foo', source_id='foo', url='foo')
|
||||
c2 = Channel(name='bar', link='bar', source_id='bar')
|
||||
c3 = Channel(name='baz', link='baz', source_id='baz', url='baz')
|
||||
c4 = Channel(name='qux', link='qux')
|
||||
session.add_all([c1, c2, c3, c4])
|
||||
|
||||
# All existing channels should be used.
|
||||
tests = [
|
||||
(dict(source_id='foo'), c1),
|
||||
(dict(link='foo'), c1),
|
||||
(dict(url='foo'), c1),
|
||||
(dict(url='foo', source_id='bar'), c2), # source_id is preferred.
|
||||
(dict(name='foo', source_id='bar'), c2),
|
||||
(dict(source_id='bar'), c2),
|
||||
(dict(source_id='baz'), c3),
|
||||
(dict(name='qux'), c4),
|
||||
(dict(link='qux'), c4),
|
||||
]
|
||||
for kwargs, expected in tests:
|
||||
channel = get_or_create_channel(**kwargs)
|
||||
self.assertEqual(expected.id, channel.id, f'Expected {expected} got {channel}')
|
||||
|
||||
# A new channel is created. It will not be automatically downloaded.
|
||||
channel = get_or_create_channel(source_id='quux', name='quux', url='quux')
|
||||
self.assertEqual(channel.id, 5)
|
||||
self.assertEqual(channel.source_id, 'quux')
|
||||
self.assertEqual(channel.name, 'quux')
|
||||
self.assertEqual(channel.link, 'quux')
|
||||
self.assertEqual(channel.url, 'quux')
|
||||
self.assertIsNone(channel.download_frequency)
|
||||
|
||||
# New channel can be retrieved.
|
||||
self.assertEqual(get_or_create_channel(source_id='quux'), channel)
|
||||
|
|
|
|||
74
modules/videos/test/test_lib.py
Normal file
74
modules/videos/test/test_lib.py
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
from datetime import datetime
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
|
||||
from modules.videos.channel import lib
|
||||
from modules.videos.models import Channel
|
||||
from wrolpi.dates import local_timezone
|
||||
from wrolpi.downloader import DownloadFrequency
|
||||
|
||||
daily, weekly, days30 = DownloadFrequency.daily, DownloadFrequency.weekly, DownloadFrequency.days30
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'channels,expected',
|
||||
[
|
||||
([Channel(download_frequency=daily, url='https://example.com/1')],
|
||||
[('https://example.com/1', daily, '2000-01-02 00:00:00-07:00')]
|
||||
),
|
||||
# Several different download frequencies.
|
||||
([
|
||||
Channel(download_frequency=daily, url='https://example.com/1'),
|
||||
Channel(download_frequency=daily, url='https://example.com/2'),
|
||||
Channel(download_frequency=daily, url='https://example.com/3'),
|
||||
Channel(download_frequency=weekly, url='https://example.com/4'),
|
||||
Channel(download_frequency=weekly, url='https://example.com/5'),
|
||||
Channel(download_frequency=days30, url='https://example.com/6'),
|
||||
],
|
||||
[
|
||||
('https://example.com/1', daily, '2000-01-02 00:00:00-07:00'),
|
||||
('https://example.com/2', daily, '2000-01-02 08:00:00-07:00'),
|
||||
('https://example.com/3', daily, '2000-01-02 16:00:00-07:00'),
|
||||
('https://example.com/4', weekly, '2000-01-08 00:00:00-07:00'),
|
||||
('https://example.com/5', weekly, '2000-01-11 12:00:00-07:00'),
|
||||
('https://example.com/6', days30, '2000-01-31 00:00:00-07:00'),
|
||||
]
|
||||
),
|
||||
# Multiple downloads in a day.
|
||||
([Channel(download_frequency=daily, url=f'https://example.com/{i}') for i in range(1, 3)],
|
||||
[
|
||||
('https://example.com/1', daily, '2000-01-02 00:00:00-07:00'),
|
||||
('https://example.com/2', daily, '2000-01-02 12:00:00-07:00'),
|
||||
]
|
||||
),
|
||||
# More downloads in a week than days in a week.
|
||||
([Channel(download_frequency=weekly, url=f'https://example.com/{i}') for i in range(1, 9)],
|
||||
[
|
||||
('https://example.com/1', weekly, '2000-01-08 00:00:00-07:00'),
|
||||
('https://example.com/2', weekly, '2000-01-08 21:00:00-07:00'),
|
||||
('https://example.com/3', weekly, '2000-01-09 18:00:00-07:00'),
|
||||
('https://example.com/4', weekly, '2000-01-10 15:00:00-07:00'),
|
||||
('https://example.com/5', weekly, '2000-01-11 12:00:00-07:00'),
|
||||
('https://example.com/6', weekly, '2000-01-12 09:00:00-07:00'),
|
||||
('https://example.com/7', weekly, '2000-01-13 06:00:00-07:00'),
|
||||
('https://example.com/8', weekly, '2000-01-14 03:00:00-07:00'),
|
||||
]
|
||||
),
|
||||
# Any Channel without a URL AND frequency is ignored.
|
||||
([
|
||||
Channel(download_frequency=daily, url='https://example.com/1'),
|
||||
Channel(download_frequency=daily),
|
||||
Channel(url='this should be ignored'),
|
||||
],
|
||||
[('https://example.com/1', daily, '2000-01-02 00:00:00-07:00')]
|
||||
),
|
||||
]
|
||||
)
|
||||
@mock.patch('modules.videos.channel.lib.today', lambda: local_timezone(datetime(2000, 1, 1, 0, 0, 0)))
|
||||
def test_spread_by_frequency(channels, expected):
|
||||
result = lib._spread_by_frequency(channels)
|
||||
for result, (expected_url, expected_frequency, expected_dt) in zip(result, expected):
|
||||
assert result['url'] == expected_url
|
||||
assert result['frequency'] == expected_frequency
|
||||
assert str(result['next_download']) == expected_dt
|
||||
15
modules/videos/test/test_video_url_resolver.py
Normal file
15
modules/videos/test/test_video_url_resolver.py
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import pytest
|
||||
|
||||
from modules.videos.video_url_resolver import video_url_resolver
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'domain,entry,expected',
|
||||
[
|
||||
('youtube.com', {'url': 'foo'}, 'https://youtube.com/watch?v=foo'),
|
||||
('youtube.com', {'webpage_url': 'https://youtube.com/watch?v=foo'}, 'https://youtube.com/watch?v=foo'),
|
||||
('youtube.com', {'url': 'https://youtube.com/watch?v=foo'}, 'https://youtube.com/watch?v=foo'),
|
||||
]
|
||||
)
|
||||
def test_video_url_resolver(domain, entry, expected):
|
||||
assert video_url_resolver(domain, entry) == expected
|
||||
|
|
@ -5,12 +5,12 @@ from sanic.request import Request
|
|||
|
||||
from wrolpi.common import logger, wrol_mode_check, run_after, get_media_directory, \
|
||||
get_relative_to_media_directory
|
||||
from wrolpi.root_api import json_response
|
||||
from wrolpi.db import get_db_session
|
||||
from wrolpi.errors import ValidationError, InvalidOrderBy
|
||||
from wrolpi.root_api import json_response
|
||||
from wrolpi.schema import validate_doc, JSONErrorResponse
|
||||
from .lib import get_video, VIDEO_ORDERS, DEFAULT_VIDEO_ORDER, video_search, get_video_for_app
|
||||
from ..common import get_matching_directories, get_allowed_limit, minimize_video
|
||||
from ..common import get_matching_directories, get_allowed_limit
|
||||
from ..lib import save_channels_config
|
||||
from ..schema import VideoResponse, VideoSearchRequest, VideoSearchResponse, \
|
||||
DirectoriesResponse, DirectoriesRequest
|
||||
|
|
@ -55,7 +55,7 @@ async def search(_: Request, data: dict):
|
|||
|
||||
videos, videos_total = video_search(search_str, offset, limit, channel_link, order_by, favorites)
|
||||
|
||||
ret = {'videos': list(map(minimize_video, videos)), 'totals': {'videos': videos_total}}
|
||||
ret = {'videos': list(videos), 'totals': {'videos': videos_total}}
|
||||
return json_response(ret)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -31,9 +31,9 @@ def get_video_for_app(video_id: int) -> Tuple[dict, Optional[dict], Optional[dic
|
|||
video.set_viewed()
|
||||
previous_video, next_video = video.get_surrounding_videos()
|
||||
|
||||
video = video.get_minimize()
|
||||
previous_video = previous_video.get_minimize() if previous_video else None
|
||||
next_video = next_video.get_minimize() if next_video else None
|
||||
video = video.__json__()
|
||||
previous_video = previous_video.__json__() if previous_video else None
|
||||
next_video = next_video.__json__() if next_video else None
|
||||
|
||||
return video, previous_video, next_video
|
||||
|
||||
|
|
@ -139,7 +139,7 @@ def video_search(
|
|||
results = session.query(Video).filter(Video.id.in_(ranked_ids)).all()
|
||||
results = sorted(results, key=lambda r: ranked_ids.index(r.id))
|
||||
|
||||
results = [i.get_minimize() for i in results]
|
||||
results = [i.__json__() for i in results]
|
||||
|
||||
return results, total
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ from datetime import timedelta
|
|||
from http import HTTPStatus
|
||||
from json import dumps
|
||||
from unittest import mock
|
||||
from unittest.mock import MagicMock
|
||||
from uuid import uuid4
|
||||
|
||||
from modules.videos.models import Channel, Video
|
||||
|
|
@ -102,13 +101,13 @@ class TestVideoFunctions(TestAPI):
|
|||
self.assertError(resp, HTTPStatus.FORBIDDEN, API_ERRORS[WROLModeEnabled]['code'])
|
||||
|
||||
# Can't refresh or download
|
||||
_, resp = api_app.test_client.post('/api/videos:refresh')
|
||||
_, resp = api_app.test_client.post('/api/videos/refresh')
|
||||
self.assertError(resp, HTTPStatus.FORBIDDEN, API_ERRORS[WROLModeEnabled]['code'])
|
||||
_, resp = api_app.test_client.post('/api/videos:download')
|
||||
_, resp = api_app.test_client.post('/api/videos/download')
|
||||
self.assertError(resp, HTTPStatus.FORBIDDEN, API_ERRORS[WROLModeEnabled]['code'])
|
||||
|
||||
# THE REST OF THESE METHODS ARE ALLOWED
|
||||
_, resp = api_app.test_client.post('/api/videos:favorite', data=favorite)
|
||||
_, resp = api_app.test_client.post('/api/videos/favorite', data=favorite)
|
||||
self.assertEqual(resp.status_code, HTTPStatus.OK)
|
||||
|
||||
@wrap_test_db
|
||||
|
|
@ -164,33 +163,3 @@ class TestVideoFunctions(TestAPI):
|
|||
self.assertEqual(session.query(Video).count(), 2)
|
||||
self.assertFalse((tempdir / 'channel1/vid1.mp4').is_file())
|
||||
self.assertFalse((tempdir / 'channel2/vid2.mp4').is_file())
|
||||
|
||||
def test_events(self):
|
||||
request, response = api_app.test_client.get('/api/events')
|
||||
self.assertOK(response)
|
||||
self.assertGreater(len(response.json['events']), 1)
|
||||
self.assertFalse(any(i['is_set'] for i in response.json['events']))
|
||||
|
||||
calls = []
|
||||
|
||||
async def fake_refresh_videos(*a, **kw):
|
||||
calls.append((a, kw))
|
||||
|
||||
with mock.patch('modules.videos.api.refresh_videos', fake_refresh_videos), \
|
||||
mock.patch('modules.videos.api.refresh_event') as refresh_event:
|
||||
refresh_event: MagicMock
|
||||
|
||||
# Cannot start a second refresh while one is running.
|
||||
refresh_event.is_set.return_value = True
|
||||
request, response = api_app.test_client.post('/api/videos:refresh')
|
||||
self.assertCONFLICT(response)
|
||||
|
||||
# Refresh is started, a stream is created
|
||||
refresh_event.is_set.return_value = False
|
||||
request, response = api_app.test_client.post('/api/videos:refresh')
|
||||
self.assertOK(response)
|
||||
self.assertEqual(response.json['code'], 'stream-started')
|
||||
stream_url: str = response.json['stream_url']
|
||||
assert stream_url.startswith('ws://')
|
||||
assert calls == [((None,), {})]
|
||||
refresh_event.set.assert_called()
|
||||
|
|
|
|||
24
modules/videos/video_url_resolver.py
Normal file
24
modules/videos/video_url_resolver.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
formatters = {
|
||||
'youtube.com': lambda i: f'https://youtube.com/watch?v={i["url"]}',
|
||||
'www.youtube.com': lambda i: f'https://youtube.com/watch?v={i["url"]}',
|
||||
}
|
||||
|
||||
|
||||
def video_url_resolver(domain: str, entry: dict) -> str:
|
||||
"""
|
||||
Guess the full URL of a Youtube-DL entry.
|
||||
"""
|
||||
if 'webpage_url' in entry:
|
||||
return entry['webpage_url']
|
||||
|
||||
if entry.get('url', '').startswith('https://'):
|
||||
return entry['url']
|
||||
if entry.get('url', '').startswith('http://'):
|
||||
return entry['url']
|
||||
|
||||
try:
|
||||
formatter = formatters[domain]
|
||||
return formatter(entry)
|
||||
except KeyError:
|
||||
# Unable to resolve, try the URL as is.
|
||||
return entry['url']
|
||||
Loading…
Add table
Add a link
Reference in a new issue