From 281de05b83a3c3b75e138d9b62c3b699a9baf9a8 Mon Sep 17 00:00:00 2001 From: Roland Date: Mon, 14 Oct 2019 12:34:38 -0600 Subject: [PATCH] Adding download button. Adding endpoint to stream statuses during download, not yet complete. --- .gitignore | 3 ++ wrolpi/plugins/videos/api.py | 33 ++++++++++++++++--- wrolpi/plugins/videos/downloader.py | 10 +++--- .../videos/templates/channels_settings.html | 14 ++++++++ wrolpi/plugins/videos/test/test_api.py | 11 ++++--- 5 files changed, 57 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 544f4d33..043da7ce 100644 --- a/.gitignore +++ b/.gitignore @@ -105,3 +105,6 @@ venv.bak/ # intellij .idea/ + +# example videos +test/example_videos diff --git a/wrolpi/plugins/videos/api.py b/wrolpi/plugins/videos/api.py index 37014db6..7ab558d5 100644 --- a/wrolpi/plugins/videos/api.py +++ b/wrolpi/plugins/videos/api.py @@ -1,14 +1,12 @@ import json import pathlib -import time import cherrypy -import psycopg2 from dictorm import DictDB from wrolpi.common import sanitize_link, get_db_context from wrolpi.plugins.videos.common import get_conflicting_channels -from wrolpi.plugins.videos.downloader import insert_video +from wrolpi.plugins.videos.downloader import insert_video, update_channels, download_all_missing_videos from wrolpi.plugins.videos.main import logger from .common import generate_video_paths, save_settings_config, get_downloader_config, \ resolve_project_path, \ @@ -27,6 +25,7 @@ class SettingsAPI(object): def __init__(self): self.refresh = Refresh() + self.download = Download() def PUT(self, **form_data): downloader_config = get_downloader_config() @@ -36,15 +35,41 @@ class SettingsAPI(object): return json.dumps({'success': 'Settings saved'}) +def newline_streamer(func): + def add_newlines(*a, **kw): + yield from (f'{i}\n' for i in func(*a, **kw)) + return add_newlines + + @cherrypy.expose class Refresh(object): def POST(self): cherrypy.response.headers['Content-Type'] = 'text/event-stream' + @newline_streamer def streamer(): with get_db_context(commit=True) as (db_conn, db): yield from refresh_videos(db) + yield 'stream-complete' + + return streamer() + + POST._cp_config = {'response.stream': True} + + +@cherrypy.expose +class Download(object): + + def POST(self): + cherrypy.response.headers['Content-Type'] = 'text/event-stream' + + @newline_streamer + def streamer(): + with get_db_context(commit=True) as (db_conn, db): + yield from update_channels(db_conn, db) + yield from download_all_missing_videos(db_conn, db) + yield 'stream-complete' return streamer() @@ -214,6 +239,6 @@ def refresh_videos(db: DictDB): curs.execute('DELETE FROM video WHERE id = ANY(%s)', (to_delete,)) for channel in Channel.get_where(): - yield f'Checking {channel["name"]} for new videos' + yield f'Checking {channel["name"]} directory for new videos' with db.transaction(commit=True): yield from refresh_channel_videos(db, channel) diff --git a/wrolpi/plugins/videos/downloader.py b/wrolpi/plugins/videos/downloader.py index dfb5372c..db162f54 100755 --- a/wrolpi/plugins/videos/downloader.py +++ b/wrolpi/plugins/videos/downloader.py @@ -62,6 +62,7 @@ def update_channels(db_conn, db, oldest_date=None): remote_channels = list(remote_channels) logger.debug(f'Getting info for {len(remote_channels)} channels') for channel in remote_channels: + yield f'Getting video list for {channel["name"]}' info = get_channel_info(channel) channel['info_json'] = info channel['info_date'] = datetime.now() @@ -79,7 +80,7 @@ def find_matching_video_files(directory, search_str) -> str: def find_missing_channel_videos(channel: Dict) -> dict: - info_json = json.loads(channel['info_json']) + info_json = channel['info_json'] entries = info_json['entries'] for entry in entries: source_id = entry['id'] @@ -238,13 +239,12 @@ def download_all_missing_videos(db_conn, db, count_limit=0): try: video_path = download_video(channel, missing_video) except Exception as e: - logger.warning(f'Failed to download {missing_video} with exception: {e}') + logger.warning(f'Failed to download "{missing_video["title"]}" with exception: {e}') + yield f'Failed to download "{missing_video["title"]}", see logs for details...' continue insert_video(db, video_path, channel, None) + yield f'{channel["name"]}: Downloaded: {missing_video["title"]}' db_conn.commit() - else: - db_conn.rollback() - print('No videos need to be downloaded today!') def main(args=None): diff --git a/wrolpi/plugins/videos/templates/channels_settings.html b/wrolpi/plugins/videos/templates/channels_settings.html index 87d34d99..3ef17dbf 100644 --- a/wrolpi/plugins/videos/templates/channels_settings.html +++ b/wrolpi/plugins/videos/templates/channels_settings.html @@ -46,6 +46,20 @@

+ +
+
+
+ {{ post_form_button('download', 'dark', 'Download New Videos') }} +
+
+
+

+ Update video list for each channel, then download any missing videos. +

+
+
+ diff --git a/wrolpi/plugins/videos/test/test_api.py b/wrolpi/plugins/videos/test/test_api.py index c6855ddf..1362c5e4 100644 --- a/wrolpi/plugins/videos/test/test_api.py +++ b/wrolpi/plugins/videos/test/test_api.py @@ -55,17 +55,18 @@ class TestAPI(unittest.TestCase): bogus = Video(video_path='bar').flush() assert bogus['id'], 'Failed to insert a bogus video for removal' + # TODO this test is very fragile :( streamer = api.settings.refresh.POST() statuses = list(streamer) - self.assertIn('Verifying videos in DB exist in file system', statuses) - self.assertIn('Deleting video files no longer in file system', statuses) - self.assertIn('Checking Big Buck Bunny for new videos', statuses) - self.assertIn('Big Buck Bunny: Added 1 new videos, 0 already existed.', statuses) + self.assertIn('Verifying videos in DB exist in file system\n', statuses) + self.assertIn('Deleting video files no longer in file system\n', statuses) + self.assertIn('Checking Big Buck Bunny directory for new videos\n', statuses) + self.assertEqual('stream-complete\n', statuses[-1]) with get_db_context() as (db_conn, db): Video, Channel = db['video'], db['channel'] self.assertEqual(Channel.count(), 1) - self.assertEqual(Video.count(), 1) + self.assertGreater(Video.count(), 1) @test_db_wrapper def test_channel(self):