njpma/app/video/management/commands/load_titles.py
2021-11-24 11:47:25 +01:00

75 lines
2.9 KiB
Python

from django.core.management.base import BaseCommand
from django.conf import settings
import ox
from ... import models
def escape(key):
return key.replace('%', '%25').replace('&', '%26').replace('_', '%09').replace(' ', '_').replace('<', '%0E').replace('>', '%0F')
def escape_slug(key):
key = ox.decode_html(key).replace('%', '').replace('&', '-').replace('_', '-').replace(' ', '-').replace('<', '').replace('>', '').lower()
key = key.replace("'", '').replace('"', '').strip()
return key
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),
parser.add_argument("--group", dest="group", type=str, default='Asian Art Biennial 2021'),
def handle(self, *args, **options):
api = ox.api.signin(options['api'])
query = {
'query': {
},
'keys': [
'id', 'title', 'director', 'summary', 'source', 'sourcedescription', 'date', 'location',
'country', 'type',
'duration', 'featuring', 'cinematographer',
'hue', 'saturation', 'lightness',
'folder', 'folderdescription', 'rightslevel'
],
'sort': [{'key': 'duration', 'operator': '-'}],
'range': [0, 1000]
}
folders = {}
for item in api.find(**query)['data']['items']:
if item['rightslevel'] > 0:
continue
if isinstance(item['folder'], list):
print(item['id'])
if item['folder'] not in folders:
description = item['folderdescription'] or item['summary']
folders[item['folder']] = {
'title': item['folder'],
'date': item['date'],
'country': item['country'],
'featuring': item['featuring'],
'type': item['type'],
'description': description,
'url': api.url.replace('/api/', '/grid/folder==' + escape(item['folder'])),
'items': [],
}
del item['folderdescription']
if item['summary'] == folders[item['folder']]['description']:
item['summary'] = ''
folders[item['folder']]['items'].append(item)
slugs = []
for item in folders.values():
slug = escape_slug(item['title'].split(' / ')[0])
f, c = models.Film.objects.get_or_create(slug=slug)
f.pandora_url = item['url']
for key, value in item.items():
if key != 'url':
f.data[{
}.get(key, key)] = value
if c:
f.public = True
f.save()
slugs.append(slug)
models.Film.objects.exclude(slug__in=slugs).delete()