From 4d7e9133e0973fa190f71a997226971c9634963c Mon Sep 17 00:00:00 2001 From: Thibault Nocchi <1619359+ThibaultNocchi@users.noreply.github.com> Date: Tue, 1 Feb 2022 20:26:57 +0100 Subject: [PATCH] add possibilty to use yt dlp write_info_json flag --- .../migrations/0011_auto_20220201_1654.py | 21 +++++++++++++++++++ tubesync/sync/models.py | 17 ++++++++++++++- tubesync/sync/templates/sync/source.html | 4 ++++ tubesync/sync/views.py | 6 ++++-- tubesync/sync/youtube.py | 3 ++- 5 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 tubesync/sync/migrations/0011_auto_20220201_1654.py diff --git a/tubesync/sync/migrations/0011_auto_20220201_1654.py b/tubesync/sync/migrations/0011_auto_20220201_1654.py new file mode 100644 index 0000000..96d9f4a --- /dev/null +++ b/tubesync/sync/migrations/0011_auto_20220201_1654.py @@ -0,0 +1,21 @@ +# Generated by Django 3.2.11 on 2022-02-01 16:54 + +import django.core.files.storage +from django.db import migrations, models +import sync.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('sync', '0010_auto_20210924_0554'), + ] + + operations = [ + migrations.AddField( + model_name='source', + name='write_json', + field=models.BooleanField( + default=False, help_text='Write a JSON file with the media info, these may be detected and used by some media servers', verbose_name='write json'), + ), + ] diff --git a/tubesync/sync/models.py b/tubesync/sync/models.py index 4c99f71..3ac58a4 100644 --- a/tubesync/sync/models.py +++ b/tubesync/sync/models.py @@ -298,6 +298,11 @@ class Source(models.Model): default=False, help_text=_('Write an NFO file in XML with the media info, these may be detected and used by some media servers') ) + write_json = models.BooleanField( + _('write json'), + default=False, + help_text=_('Write a JSON file with the media info, these may be detected and used by some media servers') + ) has_failed = models.BooleanField( _('has failed'), default=False, @@ -1072,6 +1077,16 @@ class Media(models.Model): def nfopath(self): return self.source.directory_path / self.nfoname + @property + def jsonname(self): + filename = os.path.basename(self.media_file.path) + prefix, ext = os.path.splitext(filename) + return f'{prefix}.info.json' + + @property + def jsonpath(self): + return self.source.directory_path / self.jsonname + @property def directory_path(self): # Otherwise, create a suitable filename from the source media_format @@ -1220,7 +1235,7 @@ class Media(models.Model): f'no valid format available') # Download the media with youtube-dl download_youtube_media(self.url, format_str, self.source.extension, - str(self.filepath)) + str(self.filepath), self.source.write_json) # Return the download paramaters return format_str, self.source.extension diff --git a/tubesync/sync/templates/sync/source.html b/tubesync/sync/templates/sync/source.html index f8b1e39..1668e3b 100644 --- a/tubesync/sync/templates/sync/source.html +++ b/tubesync/sync/templates/sync/source.html @@ -111,6 +111,10 @@ Write NFO? Write NFO?
{% if source.write_nfo %}{% else %}{% endif %} + + Write JSON? + Write JSON?
{% if source.write_json %}{% else %}{% endif %} + {% if source.delete_old_media and source.days_to_keep > 0 %} Delete old media diff --git a/tubesync/sync/views.py b/tubesync/sync/views.py index a45ed70..1eb9f43 100644 --- a/tubesync/sync/views.py +++ b/tubesync/sync/views.py @@ -280,7 +280,7 @@ class AddSourceView(CreateView): fields = ('source_type', 'key', 'name', 'directory', 'media_format', 'index_schedule', 'download_media', 'download_cap', 'delete_old_media', 'days_to_keep', 'source_resolution', 'source_vcodec', 'source_acodec', - 'prefer_60fps', 'prefer_hdr', 'fallback', 'copy_thumbnails', 'write_nfo') + 'prefer_60fps', 'prefer_hdr', 'fallback', 'copy_thumbnails', 'write_nfo', 'write_json') errors = { 'invalid_media_format': _('Invalid media format, the media format contains ' 'errors or is empty. Check the table at the end of ' @@ -371,7 +371,7 @@ class UpdateSourceView(UpdateView): fields = ('source_type', 'key', 'name', 'directory', 'media_format', 'index_schedule', 'download_media', 'download_cap', 'delete_old_media', 'days_to_keep', 'source_resolution', 'source_vcodec', 'source_acodec', - 'prefer_60fps', 'prefer_hdr', 'fallback', 'copy_thumbnails', 'write_nfo') + 'prefer_60fps', 'prefer_hdr', 'fallback', 'copy_thumbnails', 'write_nfo', 'write_json') errors = { 'invalid_media_format': _('Invalid media format, the media format contains ' 'errors or is empty. Check the table at the end of ' @@ -421,6 +421,8 @@ class DeleteSourceView(DeleteView, FormMixin): delete_file(media.thumbpath) # Delete NFO file if it exists delete_file(media.nfopath) + # Delete JSON file if it exists + delete_file(media.jsonpath) return super().post(request, *args, **kwargs) def get_success_url(self): diff --git a/tubesync/sync/youtube.py b/tubesync/sync/youtube.py index f6a959b..6827749 100644 --- a/tubesync/sync/youtube.py +++ b/tubesync/sync/youtube.py @@ -64,7 +64,7 @@ def get_media_info(url): return response -def download_media(url, media_format, extension, output_file): +def download_media(url, media_format, extension, output_file, info_json): ''' Downloads a YouTube URL to a file on disk. ''' @@ -108,6 +108,7 @@ def download_media(url, media_format, extension, output_file): 'outtmpl': output_file, 'quiet': True, 'progress_hooks': [hook], + 'writeinfojson': info_json }) with yt_dlp.YoutubeDL(opts) as y: try: