4 from django.core.management.base import BaseCommand
6 from emissions.utils import get_duration
7 from ...models import Track, Jingle
10 class Command(BaseCommand):
11 def add_arguments(self, parser):
13 '--recent', action='store_true', default=False,
14 help='Only do recent files')
16 '--jingles', action='store_true', default=False,
19 '--force', action='store_true', default=False,
20 help='Recompute existing durations')
22 def handle(self, verbosity, **kwargs):
23 if kwargs.get('jingles'):
24 qs = Jingle.objects.all()
26 qs = Track.objects.all()
27 if kwargs.get('recent'):
28 qs = qs.filter(creation_timestamp__gt=datetime.date.today() - datetime.timedelta(days=10))
29 if not kwargs.get('force'):
30 qs = qs.filter(duration__isnull=True)
33 for i, track in enumerate(qs):
35 print('[%s/%s] %s' % (i, total, track))
37 file_path = track.file_path()
38 except AttributeError:
40 if file_path is None or not os.path.exists(file_path):
42 duration = get_duration(file_path)
44 track.duration = datetime.timedelta(seconds=float(duration))