From 2a067d6101d92e7ec3cef6bf3d6d35334b7536fc Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Sat, 12 Nov 2022 19:26:54 +0100 Subject: [PATCH] add new command to rsync sounds to website --- .../management/commands/upload-sounds.py | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 emissions/management/commands/upload-sounds.py diff --git a/emissions/management/commands/upload-sounds.py b/emissions/management/commands/upload-sounds.py new file mode 100644 index 0000000..80a7436 --- /dev/null +++ b/emissions/management/commands/upload-sounds.py @@ -0,0 +1,66 @@ +import os +import subprocess + +from django.conf import settings +from django.core.management.base import BaseCommand, CommandError + +from ...models import SoundFile + + +class Command(BaseCommand): + def add_arguments(self, parser): + parser.add_argument( + '--emission', + dest='emission', + metavar='EMISSION', + default=None, + help='Limit to soundfiles belonging to emission (slug)', + ) + parser.add_argument( + '--episode', + dest='episode', + metavar='EPISODE', + default=None, + help='Limit to sounfiles belonging to episode (slug)', + ) + parser.add_argument( + '--newer', + dest='newer', + help='Limit to soundfiles newer than (either a number of a second, ' + 'or a number of days when suffixed by "d"', + ) + + def handle(self, emission, episode, newer, verbosity, **kwargs): + self.verbose = verbosity > 1 + + qs = SoundFile.objects.exclude(podcastable=False) + + if emission: + qs = qs.filter(episode__emission__slug=emission) + + if episode: + qs = qs.filter(episode__slug=episode) + + if newer: + if newer.endswith('d'): + newer = int(newer.strip('d')) * 86400 + else: + newer = int(newer) + qs = qs.filter(creation_timestamp__gt=timezone.now() - datetime.timedelta(seconds=newer)) + + for soundfile in qs.select_related(): + try: + if soundfile.file is None or not os.path.exists(soundfile.file.path): + continue + except ValueError: # no file associated with it + continue + + for file_format in ('ogg', 'mp3'): + sound_filename = soundfile.get_format_path(file_format) + assert sound_filename.startswith(settings.MEDIA_ROOT) + dest = sound_filename[len(settings.MEDIA_ROOT) :] + cmd = ['rsync', '-a', sound_filename, settings.WEBSITE_MEDIA_SOUNDS_SYNC_BASE + dest] + if self.verbose: + cmd.insert(1, '-v') + print(dest) + subprocess.run(cmd) -- 2.39.2