njpma/app/video/management/commands/load_titles.py
2021-11-12 10:21:00 +00:00

60 lines
2.4 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):
return key.replace('%', '').replace('&', '-').replace('_', '-').replace(' ', '-').replace('<', '').replace('>', '').lower()
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',
'duration', 'featuring', 'cinematographer',
'hue', 'saturation', 'lightness',
'folder', 'folderdescription'
],
'range': [0, 1000]
}
folders = {}
for item in api.find(**query)['data']['items']:
if item['folder'] not in folders:
folders[item['folder']] = {
'title': item['folder'],
'description': item['folderdescription'],
'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)
for item in folders.values():
if not item['description'] and len(item['items']) == 1:
item['description'] = item['items'][0]['summary']
item['items'][0]['summary'] = ''
f, c = models.Film.objects.get_or_create(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.slug = escape_slug(item['title'])
f.save()