diff --git a/wrolpi/plugins/videos/api.py b/wrolpi/plugins/videos/api.py index 2400e22c..73144006 100644 --- a/wrolpi/plugins/videos/api.py +++ b/wrolpi/plugins/videos/api.py @@ -181,6 +181,15 @@ def refresh_videos(db: DictDB): logger.info('Refreshing video list') Channel = db['channel'] + # Remove any videos that don't exist + curs = db.get_cursor() + curs.execute('SELECT id, video_path FROM video') + existing_videos = curs.fetchall() + for video in existing_videos: + if not pathlib.Path(video['video_path']).is_file(): + curs.execute('DELETE FROM video WHERE id = %s', (video['id'],)) + return + for channel in Channel.get_where(): with db.transaction(commit=True): refresh_channel_videos(db, channel) diff --git a/wrolpi/plugins/videos/test/test_api.py b/wrolpi/plugins/videos/test/test_api.py index 401c1f35..8a190909 100644 --- a/wrolpi/plugins/videos/test/test_api.py +++ b/wrolpi/plugins/videos/test/test_api.py @@ -48,6 +48,11 @@ class TestAPI(unittest.TestCase): with get_db_context() as (db_conn, db): Video, Channel = db['video'], db['channel'] import_settings_config() + + # Insert a bogus video, it should be removed + bogus = Video(video_path='bar').flush() + assert bogus['id'], 'Failed to insert a bogus video for removal' + api.settings.refresh.GET(db) self.assertEqual(Channel.count(), 1) self.assertEqual(Video.count(), 1)