add sync-missing-metadata command with docs, resolves #25
This commit is contained in:
34
tubesync/sync/management/commands/sync-missing-metadata.py
Normal file
34
tubesync/sync/management/commands/sync-missing-metadata.py
Normal file
@@ -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')
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user