diff --git a/wrolpi/plugins/videos/api.py b/wrolpi/plugins/videos/api.py index 2b99975d..1a93d628 100644 --- a/wrolpi/plugins/videos/api.py +++ b/wrolpi/plugins/videos/api.py @@ -1,6 +1,6 @@ import json -import os import pathlib +from functools import wraps import cherrypy from dictorm import DictDB @@ -63,7 +63,7 @@ class Refresh(object): @json_statuses_streamer def streamer(): with get_db_context(commit=True) as (db_conn, db): - yield from refresh_videos(db) + yield from _refresh_videos(db) return streamer() @@ -204,16 +204,21 @@ def get_channel_form(form_data: dict): def refresh_channel_videos(db, channel): + # A set of paths relative to this channel's directory existing_paths = {i['video_path'] for i in channel['videos']} - directory = resolve_project_path(channel['directory']) + directory = resolve_project_path(channel['directory']).absolute() if not directory.is_dir(): logger.warn(f'Channel {channel["name"]} directory "{directory}" does not exist, skipping...') logger.warn(f'Have you downloaded any videos for channel {channel["name"]}?') return - channel_directory = resolve_project_path(channel['directory']).absolute() - possible_new_paths = {str(i) for i in generate_video_paths(directory, relative_to=channel_directory)} - new_videos = possible_new_paths.difference(existing_paths) + # A set of absolute paths that exist in the file system + possible_new_paths = list(generate_video_paths(directory)) + # make them relative to this channel + new_videos = [] + for possible_new_path in possible_new_paths: + if possible_new_path.relative_to(directory) not in existing_paths: + new_videos.append(possible_new_path) for video_path in new_videos: logger.debug(f'{channel["name"]}: Added {video_path}') @@ -224,7 +229,7 @@ def refresh_channel_videos(db, channel): yield final_status -def refresh_videos(db: DictDB): +def _refresh_videos(db: DictDB): """ Find any videos in the channel directories and add them to the DB. Delete DB records of any videos not in the file system. @@ -237,7 +242,7 @@ def refresh_videos(db: DictDB): logger.info('Refreshing video list') Channel = db['channel'] - # Remove any videos that don't exist + # Remove any duplicate videos and any videos that don't exist yield 'Verifying videos in DB exist in file system' curs = db.get_cursor() curs.execute('SELECT DISTINCT ON (video_path) video_path, video.id, directory AS channel_directory FROM ' @@ -265,3 +270,8 @@ def refresh_videos(db: DictDB): yield f'Checking {channel["name"]} directory for new videos' with db.transaction(commit=True): yield from refresh_channel_videos(db, channel) + + +@wraps(_refresh_videos) +def refresh_videos(db: DictDB): + return list(_refresh_videos(db)) diff --git a/wrolpi/plugins/videos/downloader.py b/wrolpi/plugins/videos/downloader.py index 5e6193b6..67e9549d 100755 --- a/wrolpi/plugins/videos/downloader.py +++ b/wrolpi/plugins/videos/downloader.py @@ -201,7 +201,8 @@ def insert_video(db: DictDB, video_path: pathlib.Path, channel: Table, idempoten 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 - video_path = video_path.relative_to(channel_dir) + if video_path.is_absolute(): + video_path = video_path.relative_to(channel_dir) name_match = NAME_PARSER.match(video_path.name) _ = upload_date = source_id = title = ext = None diff --git a/wrolpi/plugins/videos/test/test_api.py b/wrolpi/plugins/videos/test/test_api.py index ae2ec077..d5a95e5b 100644 --- a/wrolpi/plugins/videos/test/test_api.py +++ b/wrolpi/plugins/videos/test/test_api.py @@ -7,7 +7,7 @@ from shutil import copyfile import mock from wrolpi.common import get_db_context -from wrolpi.plugins.videos.api import APIRoot +from wrolpi.plugins.videos.api import APIRoot, refresh_videos from wrolpi.plugins.videos.common import import_settings_config, get_downloader_config, EXAMPLE_CONFIG_PATH from wrolpi.plugins.videos.downloader import insert_video from wrolpi.test.common import test_db_wrapper @@ -124,18 +124,52 @@ class TestAPI(unittest.TestCase): Video, Channel = db['video'], db['channel'] - vid1 = pathlib.Path(channel_path / 'channel name_20000101_title.mp4') + # These are the types of files that will be found first + vid1 = pathlib.Path(channel_path / 'channel name_20000101_abcdefghijk_title.mp4') vid1.touch() - jpg1 = pathlib.Path(channel_path / 'channel name_20000101_title.jpg') - jpg1.touch() - vid2 = pathlib.Path(channel_path / 'channel name_20000102_title.webm') + vid2 = pathlib.Path(channel_path / 'channel name_20000102_bcdefghijkl_title.webm') vid2.touch() - jpg2 = pathlib.Path(channel_path / 'channel name_20000102_title.jpg') - jpg2.touch() + # These files are associated with the video files above, and should be found second + poster1 = pathlib.Path(channel_path / 'channel name_20000101_abcdefghijk_title.jpg') + poster1.touch() + poster2 = pathlib.Path(channel_path / 'channel name_20000102_bcdefghijkl_title.jpg') + poster2.touch() + + # Create a channel, associate videos with it. channel = Channel(directory=channel_dir).flush() video1 = insert_video(db, vid1, channel) video2 = insert_video(db, vid2, channel) + self.assertEqual({i['video_path'] for i in channel['videos']}, {vid1.name, vid2.name}) - self.assertEqual(video1['poster_path'], jpg1.name) - self.assertEqual(video2['poster_path'], jpg2.name) + # Poster files were found + self.assertEqual(video1['poster_path'], poster1.name) + self.assertEqual(video2['poster_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']}) + Video(video_path='foo', channel_id=channel['id']).flush() + self.assertIn('foo', {i['video_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 + vid3 = pathlib.Path(channel_path / 'channel name_20000103_cdefghijklm_title.flv') + vid3.touch() + description3 = pathlib.Path(channel_path / 'channel name_20000103_cdefghijklm_title.description') + description3.touch() + + refresh_videos(db) + + # Bogus file was removed + self.assertNotIn('foo', {i['video_path'] for i in channel['videos']}) + + # Final structure we built + expected = { + (vid1.name, poster1.name, None), + (vid2.name, poster2.name, None), + (vid3.name, None, description3.name), + } + self.assertEqual( + {(i['video_path'], i['poster_path'], i['description_path']) for i in channel['videos']}, + expected + )