Create a set of keys for cleanup_removed_media

This commit is contained in:
tcely 2025-05-19 08:54:21 -04:00 committed by GitHub
parent 8109d6a836
commit ed55710f26
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -255,7 +255,7 @@ def cleanup_old_media():
schedule_media_servers_update() schedule_media_servers_update()
def cleanup_removed_media(source, videos): def cleanup_removed_media(source, video_keys):
if not source.delete_removed_media: if not source.delete_removed_media:
return return
log.info(f'Cleaning up media no longer in source: {source}') log.info(f'Cleaning up media no longer in source: {source}')
@ -265,8 +265,7 @@ def cleanup_removed_media(source, videos):
source=source, source=source,
) )
for media in qs_gen(mqs): for media in qs_gen(mqs):
matching_source_item = [video['id'] for video in videos if video['id'] == media.key] if media.key not in video_keys:
if not matching_source_item:
log.info(f'{media.name} is no longer in source, removing') log.info(f'{media.name} is no longer in source, removing')
with atomic(): with atomic():
media.delete() media.delete()
@ -317,6 +316,7 @@ def index_source_task(source_id):
) )
tvn_format = '{:,}' + f'/{num_videos:,}' tvn_format = '{:,}' + f'/{num_videos:,}'
vn = 0 vn = 0
video_keys = set()
while len(videos) > 0: while len(videos) > 0:
vn += 1 vn += 1
video = videos.popleft() video = videos.popleft()
@ -325,6 +325,7 @@ def index_source_task(source_id):
if not key: if not key:
# Video has no unique key (ID), it can't be indexed # Video has no unique key (ID), it can't be indexed
continue continue
video_keys.add(key)
update_task_status(task, tvn_format.format(vn)) update_task_status(task, tvn_format.format(vn))
# media, new_media = Media.objects.get_or_create(key=key, source=source) # media, new_media = Media.objects.get_or_create(key=key, source=source)
try: try:
@ -379,7 +380,7 @@ def index_source_task(source_id):
# Reset task.verbose_name to the saved value # Reset task.verbose_name to the saved value
update_task_status(task, None) update_task_status(task, None)
# Cleanup of media no longer available from the source # Cleanup of media no longer available from the source
cleanup_removed_media(source, videos) cleanup_removed_media(source, video_keys)
videos = video = None videos = video = None