2023-10-08 11:06:24 +00:00
|
|
|
import json
|
2023-10-08 11:07:57 +00:00
|
|
|
import os
|
2023-10-08 11:06:24 +00:00
|
|
|
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
import item.models
|
|
|
|
import itemlist.models
|
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = 'generate symlinks to clips and clips.json'
|
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
2023-10-08 11:07:57 +00:00
|
|
|
parser.add_argument('--prefix', action='store', dest='prefix', default="/srv/t_for_time", help='prefix to build clips in')
|
2023-10-08 11:06:24 +00:00
|
|
|
|
|
|
|
def handle(self, **options):
|
|
|
|
prefix = options['prefix']
|
|
|
|
clips = []
|
|
|
|
for i in item.models.Item.objects.filter(data__type__contains="Original"):
|
|
|
|
qs = item.models.Item.objects.filter(data__title=i.data['title']).exclude(id=i.id)
|
|
|
|
if qs.count() == 2:
|
|
|
|
clip = {}
|
|
|
|
durations = []
|
|
|
|
for e in item.models.Item.objects.filter(data__title=i.data['title']):
|
|
|
|
source = i.files.all()[0].data.path
|
|
|
|
ext = os.path.splitext(source)[1]
|
|
|
|
type_ = e.data['type'][0].lower()
|
|
|
|
target = os.path.join(prefix, type_, i.data['title'] + ext)
|
|
|
|
os.makedirs(os.path.dirname(target), exist_ok=True)
|
|
|
|
if os.path.exists(target):
|
|
|
|
os.unlink(target)
|
|
|
|
os.symlink(source, target)
|
|
|
|
clip[type_] = target
|
|
|
|
durations.append(e.files.all()[0].duration)
|
|
|
|
clip["duration"] = min(durations)
|
|
|
|
clip['tags'] = i.data.get('tags', [])
|
|
|
|
clips.append(clip)
|
|
|
|
|
|
|
|
with open(os.path.join(prefix, 'clips.json'), 'w') as fd:
|
|
|
|
json.dump(clips, fd, indent=2, ensure_ascii=False)
|