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

58 lines
2.2 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:
description = item['folderdescription'] or item['summary']
folders[item['folder']] = {
'title': item['folder'],
'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)
for item in folders.values():
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()