phantasma/app/video/management/commands/load_titles.py

76 lines
2.9 KiB
Python
Raw Normal View History

2021-09-28 13:10:22 +00:00
from django.core.management.base import BaseCommand
from django.conf import settings
2021-09-28 13:10:22 +00:00
import ox
from ... import models
2021-11-12 10:21:00 +00:00
def escape(key):
return key.replace('%', '%25').replace('&', '%26').replace('_', '%09').replace(' ', '_').replace('<', '%0E').replace('>', '%0F')
def escape_slug(key):
2021-11-24 10:47:25 +00:00
key = ox.decode_html(key).replace('%', '').replace('&', '-').replace('_', '-').replace(' ', '-').replace('<', '').replace('>', '').lower()
key = key.replace("'", '').replace('"', '').strip()
return key
2021-11-12 10:21:00 +00:00
2021-09-28 13:10:22 +00:00
class Command(BaseCommand):
help = 'import titles from pan.do/ra'
def add_arguments(self, parser):
parser.add_argument("--api", dest="api", type=str, default=settings.DEFAULT_PANDORA_API),
2021-09-28 13:10:22 +00:00
parser.add_argument("--group", dest="group", type=str, default='Asian Art Biennial 2021'),
def handle(self, *args, **options):
api = ox.api.signin(options['api'])
2021-11-12 10:21:00 +00:00
2021-09-28 13:10:22 +00:00
query = {
'query': {
},
2021-10-11 14:33:24 +00:00
'keys': [
'id', 'title', 'director', 'summary', 'source', 'sourcedescription', 'date', 'location',
2021-11-22 09:20:10 +00:00
'country', 'type',
2021-10-13 15:07:12 +00:00
'duration', 'featuring', 'cinematographer',
'hue', 'saturation', 'lightness',
2021-11-22 09:20:10 +00:00
'folder', 'folderdescription', 'rightslevel'
2021-10-11 14:33:24 +00:00
],
2021-11-22 09:20:10 +00:00
'sort': [{'key': 'duration', 'operator': '-'}],
2021-09-28 13:10:22 +00:00
'range': [0, 1000]
}
2021-11-12 10:21:00 +00:00
folders = {}
2021-09-28 13:10:22 +00:00
for item in api.find(**query)['data']['items']:
2021-11-22 09:20:10 +00:00
if item['rightslevel'] > 0:
continue
if isinstance(item['folder'], list):
print(item['id'])
2021-11-24 15:40:49 +00:00
2021-11-12 10:21:00 +00:00
if item['folder'] not in folders:
2021-11-12 13:02:02 +00:00
description = item['folderdescription'] or item['summary']
2021-11-12 10:21:00 +00:00
folders[item['folder']] = {
'title': item['folder'],
2021-11-22 09:20:10 +00:00
'date': item['date'],
'country': item['country'],
'featuring': item['featuring'],
'type': item['type'],
2021-11-12 13:02:02 +00:00
'description': description,
2021-11-24 15:43:14 +00:00
'url': api.url.replace('/api/', '/grid/folder==' + escape(ox.decode_html(item['folder'])),
2021-11-12 10:21:00 +00:00
'items': [],
}
del item['folderdescription']
if item['summary'] == folders[item['folder']]['description']:
item['summary'] = ''
folders[item['folder']]['items'].append(item)
2021-11-23 16:28:32 +00:00
slugs = []
2021-11-12 10:21:00 +00:00
for item in folders.values():
2021-11-23 16:25:39 +00:00
slug = escape_slug(item['title'].split(' / ')[0])
f, c = models.Film.objects.get_or_create(slug=slug)
2021-11-24 15:43:14 +00:00
f.pandora_url = item['url']
2021-09-28 13:10:22 +00:00
for key, value in item.items():
2021-11-12 10:21:00 +00:00
if key != 'url':
2021-10-11 13:44:02 +00:00
f.data[{
}.get(key, key)] = value
2021-10-22 16:36:55 +00:00
if c:
f.public = True
2021-09-28 13:10:22 +00:00
f.save()
2021-11-23 16:28:32 +00:00
slugs.append(slug)
models.Film.objects.exclude(slug__in=slugs).delete()