Skip to content

Commit

Permalink
add sync-missing-metadata command with docs, resolves #25
Browse files Browse the repository at this point in the history
  • Loading branch information
meeb committed Feb 18, 2021
1 parent 895bfe6 commit 2f324f2
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
37 changes: 37 additions & 0 deletions docs/create-missing-metadata.md
Original file line number Diff line number Diff line change
@@ -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.
34 changes: 34 additions & 0 deletions tubesync/sync/management/commands/sync-missing-metadata.py
Original file line number Diff line number Diff line change
@@ -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')
4 changes: 2 additions & 2 deletions tubesync/sync/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,15 @@ 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:
delta = timezone.now() - timedelta(days=source.days_to_keep)
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:
Expand Down

0 comments on commit 2f324f2

Please sign in to comment.