diff --git a/README.md b/README.md index 3f300d5..5bdec82 100644 --- a/README.md +++ b/README.md @@ -203,6 +203,8 @@ and less common features: ![Import existing media into TubeSync](https://github.com/meeb/tubesync/blob/main/docs/import-existing-media.md) +![Sync or create missing metadata files](https://github.com/meeb/tubesync/blob/main/docs/create-missing-metadata.md) + # Warnings diff --git a/docs/create-missing-metadata.md b/docs/create-missing-metadata.md new file mode 100644 index 0000000..2328de4 --- /dev/null +++ b/docs/create-missing-metadata.md @@ -0,0 +1,37 @@ +# TubeSync + +## Advanced usage guide - creating missing metadata + +This is a new feature in v0.9 of TubeSync and later. It allows you to create or +re-create missing metadata in your TubeSync download directories for missing `nfo` +files and thumbnails. + +If you add a source with "write NFO files" or "copy thumbnails" disabled, download +some media and then update the source to write NFO files or copy thumbnails then +TubeSync will not automatically retroactively attempt to copy or create your missing +metadata files. You can use a special one-off command to manually write missing +metadata files to the correct locations. + +## Requirements + +You have added a source without metadata writing enabled, downloaded some media, then +updated the source to enable metadata writing. + +## Steps + +### 1. Run the batch metadata sync command + +Execute the following Django command: + +`./manage.py sync-missing-metadata` + +When deploying TubeSync inside a container, you can execute this with: + +`docker exec -ti tubesync python3 /app/manage.py sync-missing-metadata` + +This command will log what its doing to the terminal when you run it. + +Internally, this command loops over all your sources which have been saved with +"write NFO files" or "copy thumbnails" enabled. Then, loops over all media saved to +that source and confirms that the appropriate thumbnail files have been copied over and +the NFO file has been written if enabled. diff --git a/tubesync/sync/management/commands/sync-missing-metadata.py b/tubesync/sync/management/commands/sync-missing-metadata.py new file mode 100644 index 0000000..21b25c5 --- /dev/null +++ b/tubesync/sync/management/commands/sync-missing-metadata.py @@ -0,0 +1,34 @@ +import os +from shutil import copyfile +from django.core.management.base import BaseCommand, CommandError +from django.db.models import Q +from common.logger import log +from sync.models import Source, Media +from sync.utils import write_text_file + + +class Command(BaseCommand): + + help = 'Syncs missing metadata (such as nfo files) if source settings are updated' + + def handle(self, *args, **options): + log.info('Syncing missing metadata...') + sources = Source.objects.filter(Q(copy_thumbnails=True) | Q(write_nfo=True)) + for source in sources.order_by('name'): + log.info(f'Finding media for source: {source}') + for item in Media.objects.filter(source=source, downloaded=True): + log.info(f'Checking media for missing metadata: {source} / {item}') + thumbpath = item.thumbpath + if not thumbpath.is_file(): + if item.thumb: + log.info(f'Copying missing thumbnail from: {item.thumb.path} ' + f'to: {thumbpath}') + copyfile(item.thumb.path, thumbpath) + else: + log.error(f'Tried to copy missing thumbnail for {item} but ' + f'the thumbnail has not been downloaded') + nfopath = item.nfopath + if not nfopath.is_file(): + log.info(f'Writing missing NFO file: {nfopath}') + write_text_file(nfopath, item.nfoxml) + log.info('Done') diff --git a/tubesync/sync/tasks.py b/tubesync/sync/tasks.py index 47bf3cf..5688fb6 100644 --- a/tubesync/sync/tasks.py +++ b/tubesync/sync/tasks.py @@ -238,7 +238,7 @@ def download_media_metadata(media_id): if media.published < max_cap_age: # Media was published after the cap date, skip it log.warn(f'Media: {source} / {media} is older than cap age ' - f'{max_cap_age}, skipping') + f'{max_cap_age}, skipping') media.skip = True # If the source has a cut-off check the upload date is within the allowed delta if source.delete_old_media and source.days_to_keep > 0: @@ -246,7 +246,7 @@ def download_media_metadata(media_id): if media.published < delta: # Media was published after the cutoff date, skip it log.warn(f'Media: {source} / {media} is older than ' - f'{source.days_to_keep} days, skipping') + f'{source.days_to_keep} days, skipping') media.skip = True # Check we can download the media item if not media.skip: