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

80 lines
3 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):
2021-11-24 10:47:25 +00:00
key = ox.decode_html(key).replace('%', '').replace('&', '-').replace('_', '-').replace(' ', '-').replace('<', '').replace('>', '').lower()
key = key.replace("'", '').replace('"', '').strip()
return key
2021-11-12 10:21:00 +00:00
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
keys = [
'id', 'title', 'director', 'summary', 'source', 'sourcedescription', 'date', 'location',
'country', 'type',
'duration', 'featuring', 'cinematographer',
'hue', 'saturation', 'lightness',
'folder', 'folderdescription', 'rightslevel'
]
2021-09-28 13:10:22 +00:00
query = {
'query': {
},
'keys': ['id'],
2021-11-22 09:20:10 +00:00
'sort': [{'key': 'duration', 'operator': '-'}],
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']:
item = api.get(id=item['id'], keys=keys)['data']
2021-11-22 09:20:10 +00:00
if item['rightslevel'] > 0:
continue
if isinstance(item['folder'], list):
print(item['id'])
2021-11-24 15:40:49 +00:00
2021-11-12 10:21:00 +00:00
if item['folder'] not in folders:
description = item['folderdescription'] or item.get('summary', '')
2021-11-12 10:21:00 +00:00
folders[item['folder']] = {
'title': item['folder'],
'date': item.get('date', ''),
'country': item.get('country', []),
'featuring': item.get('featuring', []),
2021-11-22 09:20:10 +00:00
'type': item['type'],
2021-11-12 13:02:02 +00:00
'description': description,
2021-11-24 15:44:01 +00:00
'url': api.url.replace('/api/', '/grid/folder==' + escape(ox.decode_html(item['folder']))),
2021-11-12 10:21:00 +00:00
'items': [],
}
2021-11-12 10:21:00 +00:00
del item['folderdescription']
if 'summary' in item and item['summary'] == folders[item['folder']]['description']:
2021-11-12 10:21:00 +00:00
item['summary'] = ''
folders[item['folder']]['items'].append(item)
2021-11-23 16:28:32 +00:00
slugs = []
2021-11-12 10:21:00 +00:00
for item in folders.values():
2021-11-23 16:25:39 +00:00
slug = escape_slug(item['title'].split(' / ')[0])
f, c = models.Film.objects.get_or_create(slug=slug)
2021-11-24 15:43:14 +00:00
f.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-09-28 13:10:22 +00:00
f.save()
2021-11-23 16:28:32 +00:00
slugs.append(slug)
models.Film.objects.exclude(slug__in=slugs).delete()