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

59 lines
2.2 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):
return key.replace('%', '').replace('&', '-').replace('_', '-').replace(' ', '-').replace('<', '').replace('>', '').lower()
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-10-13 15:07:12 +00:00
'duration', 'featuring', 'cinematographer',
'hue', 'saturation', 'lightness',
2021-11-12 10:21:00 +00:00
'folder', 'folderdescription'
2021-10-11 14:33:24 +00:00
],
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-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-12 13:02:02 +00:00
'description': description,
2021-11-12 10:21:00 +00:00
'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'])
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-11-12 13:02:02 +00:00
f.slug = escape_slug(item['title'])
2021-09-28 13:10:22 +00:00
f.save()