Adding download button. Adding endpoint to stream statuses during download, not yet complete.

This commit is contained in:
Roland 2019-10-14 12:34:38 -06:00
parent fe9d3c020c
commit 281de05b83
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
5 changed files with 57 additions and 14 deletions

3
.gitignore vendored
View file

@ -105,3 +105,6 @@ venv.bak/
# intellij
.idea/
# example videos
test/example_videos

View file

@ -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)

View file

@ -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):

View file

@ -46,6 +46,20 @@
</p>
</div>
</div>
<div class="row">
<div class="col-4">
<form id="refresh" action="/api/videos/settings/download">
{{ post_form_button('download', 'dark', 'Download New Videos') }}
</form>
</div>
<div class="col-8">
<p>
Update video list for each channel, then download any missing videos.
</p>
</div>
</div>
</div>
</div>

View file

@ -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):