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

79 lines
3 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'])
keys = [
'id', 'title', 'director', 'summary', 'source', 'sourcedescription', 'date', 'location',
'country', 'type',
'duration', 'featuring', 'cinematographer',
'hue', 'saturation', 'lightness',
'folder', 'folderdescription', 'rightslevel'
]
query = {
'query': {
},
'keys': ['id'],
'sort': [{'key': 'duration', 'operator': '-'}],
'range': [0, 1000]
}
folders = {}
for item in api.find(**query)['data']['items']:
item = api.get(id=item['id'], keys=keys)['data']
if item['rightslevel'] > 0:
continue
if isinstance(item['folder'], list):
print(item['id'])
if item['folder'] not in folders:
description = item['folderdescription'] or item.get('summary', '')
folders[item['folder']] = {
'title': item['folder'],
'date': item.get('date', ''),
'country': item.get('country', []),
'featuring': item.get('featuring', []),
'type': item['type'],
'description': description,
'url': api.url.replace('/api/', '/grid/folder==' + escape(ox.decode_html(item['folder']))),
'items': [],
}
del item['folderdescription']
if 'summary' in item and 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()