refactoring, test suite for sync app

This commit is contained in:
meeb 2020-12-09 01:31:45 +11:00
parent 195a1bef4d
commit 60a726bb7c
7 changed files with 1756 additions and 19 deletions

View File

@ -51,7 +51,7 @@ def get_best_audio_format(media):
audio_formats = [] audio_formats = []
for fmt in media.iter_formats(): for fmt in media.iter_formats():
# If the format has a video stream, skip it # If the format has a video stream, skip it
if fmt['vcodec']: if fmt['vcodec'] is not None:
continue continue
audio_formats.append(fmt) audio_formats.append(fmt)
audio_formats = list(reversed(sorted(audio_formats, key=lambda k: k['abr']))) audio_formats = list(reversed(sorted(audio_formats, key=lambda k: k['abr'])))
@ -86,7 +86,7 @@ def get_best_video_format(media):
video_formats = [] video_formats = []
for fmt in media.iter_formats(): for fmt in media.iter_formats():
# If the format has an audio stream, skip it # If the format has an audio stream, skip it
if fmt['acodec']: if fmt['acodec'] is not None:
continue continue
if media.source.source_resolution.strip().upper() == fmt['format']: if media.source.source_resolution.strip().upper() == fmt['format']:
video_formats.append(fmt) video_formats.append(fmt)
@ -97,7 +97,7 @@ def get_best_video_format(media):
# Find the next-best format matches by height # Find the next-best format matches by height
for fmt in media.iter_formats(): for fmt in media.iter_formats():
# If the format has an audio stream, skip it # If the format has an audio stream, skip it
if fmt['acodec']: if fmt['acodec'] is not None:
continue continue
if (fmt['height'] <= media.source.source_resolution_height and if (fmt['height'] <= media.source.source_resolution_height and
fmt['height'] >= min_height): fmt['height'] >= min_height):
@ -106,8 +106,6 @@ def get_best_video_format(media):
# Can't fallback # Can't fallback
return False, False return False, False
video_formats = list(reversed(sorted(video_formats, key=lambda k: k['height']))) video_formats = list(reversed(sorted(video_formats, key=lambda k: k['height'])))
print('height', media.source.source_resolution_height)
print('video_formats', video_formats)
if not video_formats: if not video_formats:
# Still no matches # Still no matches
return False, False return False, False

View File

@ -6,8 +6,8 @@ from background_task.signals import task_failed
from background_task.models import Task from background_task.models import Task
from common.logger import log from common.logger import log
from .models import Source, Media from .models import Source, Media
from .tasks import (delete_index_source_task, index_source_task, from .tasks import (delete_task, index_source_task, download_media_thumbnail,
download_media_thumbnail, map_task_to_instance) map_task_to_instance)
from .utils import delete_file from .utils import delete_file
@ -22,7 +22,7 @@ def source_pre_save(sender, instance, **kwargs):
return return
if existing_source.index_schedule != instance.index_schedule: if existing_source.index_schedule != instance.index_schedule:
# Indexing schedule has changed, recreate the indexing task # Indexing schedule has changed, recreate the indexing task
delete_index_source_task(str(instance.pk)) delete_task('sync.tasks.index_source_task', instance.pk)
verbose_name = _('Index media from source "{}"') verbose_name = _('Index media from source "{}"')
index_source_task( index_source_task(
str(instance.pk), str(instance.pk),
@ -37,7 +37,7 @@ def source_post_save(sender, instance, created, **kwargs):
# Triggered after a source is saved # Triggered after a source is saved
if created: if created:
# Create a new indexing task for newly created sources # Create a new indexing task for newly created sources
delete_index_source_task(str(instance.pk)) delete_task('sync.tasks.index_source_task', instance.pk)
log.info(f'Scheduling media indexing for source: {instance.name}') log.info(f'Scheduling media indexing for source: {instance.name}')
verbose_name = _('Index media from source "{}"') verbose_name = _('Index media from source "{}"')
index_source_task( index_source_task(
@ -61,7 +61,7 @@ def source_pre_delete(sender, instance, **kwargs):
def source_post_delete(sender, instance, **kwargs): def source_post_delete(sender, instance, **kwargs):
# Triggered after a source is deleted # Triggered after a source is deleted
log.info(f'Deleting tasks for source: {instance.name}') log.info(f'Deleting tasks for source: {instance.name}')
delete_index_source_task(str(instance.pk)) delete_task('sync.tasks.index_source_task', instance.pk)
@receiver(task_failed, sender=Task) @receiver(task_failed, sender=Task)
@ -93,9 +93,12 @@ def media_post_save(sender, instance, created, **kwargs):
) )
@receiver(post_delete, sender=Media) @receiver(pre_delete, sender=Media)
def media_post_delete(sender, instance, **kwargs): def media_pre_delete(sender, instance, **kwargs):
# Triggered after media is deleted, delete media thumbnail # Triggered before media is deleted, delete any scheduled tasks
log.info(f'Deleting tasks for media: {instance.name}')
delete_task('sync.tasks.download_media_thumbnail', instance.source.pk)
# Delete media thumbnail if it exists
if instance.thumb: if instance.thumb:
log.info(f'Deleting thumbnail for: {instance} path: {instance.thumb.path}') log.info(f'Deleting thumbnail for: {instance} path: {instance.thumb.path}')
delete_file(instance.thumb.path) delete_file(instance.thumb.path)

View File

@ -110,8 +110,8 @@ def get_source_completed_tasks(source_id, only_errors=False):
return CompletedTask.objects.filter(**q).order_by('-failed_at') return CompletedTask.objects.filter(**q).order_by('-failed_at')
def delete_index_source_task(source_id): def delete_task(task_name, source_id):
Task.objects.drop_task('sync.tasks.index_source_task', args=(source_id,)) return Task.objects.filter(task_name=task_name, queue=str(source_id)).delete()
def cleanup_completed_tasks(): def cleanup_completed_tasks():

File diff suppressed because it is too large Load Diff

View File

@ -51,7 +51,7 @@ urlpatterns = [
MediaThumbView.as_view(), MediaThumbView.as_view(),
name='media-thumb'), name='media-thumb'),
path('media-item/<uuid:pk>', path('media/<uuid:pk>',
MediaItemView.as_view(), MediaItemView.as_view(),
name='media-item'), name='media-item'),

View File

@ -153,6 +153,11 @@ def parse_media_format(format_dict):
fps = 0 fps = 0
format_full = format_dict.get('format_note', '').strip().upper() format_full = format_dict.get('format_note', '').strip().upper()
format_str = format_full[:-2] if format_full.endswith('60') else format_full format_str = format_full[:-2] if format_full.endswith('60') else format_full
format_str = format_str.strip()
format_str = format_full[:-3] if format_full.endswith('HDR') else format_full
format_str = format_str.strip()
format_str = format_full[:-2] if format_full.endswith('60') else format_full
format_str = format_str.strip()
return { return {
'id': format_dict.get('format_id', ''), 'id': format_dict.get('format_id', ''),
'format': format_str, 'format': format_str,

View File

@ -109,7 +109,7 @@ class ValidateSourceView(FormView):
'scheme': 'https', 'scheme': 'https',
'domain': 'www.youtube.com', 'domain': 'www.youtube.com',
'path_regex': '^\/(c\/)?([^\/]+)$', 'path_regex': '^\/(c\/)?([^\/]+)$',
'path_must_not_match': ('/playlist',), 'path_must_not_match': ('/playlist', '/c/playlist'),
'qs_args': [], 'qs_args': [],
'extract_key': ('path_regex', 1), 'extract_key': ('path_regex', 1),
'example': 'https://www.youtube.com/SOMECHANNEL' 'example': 'https://www.youtube.com/SOMECHANNEL'