pandora_prelinger/data.py

146 lines
4.1 KiB
Python
Raw Normal View History

2015-03-15 13:02:45 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
import json
import os
from os.path import join, normpath, dirname
import ox
from django.conf import settings
import item.models
import archive.models
base = normpath(dirname(__file__))
with open(join(base, 'prelinger.json')) as f:
data = json.load(f)
def get_item(archive):
qs = item.models.Item.objects.filter(data__contains='"archive": "%s"'%archive)
if qs.count():
return qs[0], False
return item.models.Item(), True
def update_item(d, add_only=False):
info = {{
'description': 'summary',
'id': 'archive',
'mp4': 'stream',
2015-04-02 09:02:27 +00:00
'subject': 'topic',
2015-03-15 13:02:45 +00:00
}.get(k,k):d[k] for k in d}
for k in ('type', 'flv', 'video'):
if k in info:
del info[k]
if 'year' in info:
if isinstance(info['year'], list):
info['year'] = info['year'][0]
if isinstance(info['year'], basestring):
info['year'] = info['year'][:4]
extra = []
for key in ('director', 'publisher', 'creator', 'sponsor'):
if key in info:
if len(info[key]) > 200:
extra.append(info[key])
info[key] = [info[key].split('.')[0][:200]]
else:
info[key] = [info[key]]
if extra:
info['summary'] = info.get('summary', '') + '\n\n' + '\n\n'.join(extra)
if 'summary' in info:
info['summary'] = ox.sanitize_html(info['summary'].replace('\n', '<br>\n'))
if 'title' in info:
info['title'] = info['title'].replace('(%s)' % info.get('year', ''), '').strip()
info['title'] = ox.normalize_title(info['title'])
if info.get('sound') in ('b&w', 'b/w'):
info['sound'], info['color'] = info['color'], info['sound']
if 'sound' in info:
info['sound'] = [{
'sound': 'Sound',
'sd': 'Sound',
'yes': 'Sound',
'no': 'Silent',
'si': 'Silent',
'silen': 'Silent',
'silent': 'Silent',
}.get(c.lower(), c) for c in info['sound'].split('/')]
if 'color' in info:
info['color'] = [{
'color': 'Color',
'c': 'Color',
2015-04-02 09:02:27 +00:00
'b': 'B&W',
'w': 'B&W',
2015-03-15 13:02:45 +00:00
'sepia': 'Sepia',
'tinted': 'Tinted',
2015-04-02 09:02:27 +00:00
'tinted B&W': 'B&W (tinted)',
2015-03-15 13:02:45 +00:00
'black and white': 'B&W',
2015-04-02 09:02:27 +00:00
'black & white': 'B&W',
2015-03-15 13:02:45 +00:00
'bw': 'B&W',
'b&w': 'B&W',
2015-04-02 09:02:27 +00:00
}.get(c.lower(), c) for c in info['color'].replace('b/w', 'b&w').replace('B/W', 'B&W').split('/')]
2015-03-15 13:02:45 +00:00
i, created = get_item(info['archive'])
if add_only and not created:
return
print info['archive'], info.get('title', 'Untitled')
for k in info:
i.data[k] = info[k]
i.level = 0
i.save()
if created:
i.save()
i.make_poster(True)
def update_items(add_only=False):
for d in data:
update_item(d, add_only)
2015-04-02 09:02:27 +00:00
def update_item_stream(i, force=False):
if update_stream(i) or force:
2015-03-15 13:02:45 +00:00
for s in i.streams():
s.make_timeline()
i.save()
i.update_timeline()
i.make_poster(True)
return True
return False
def update_stream(i):
stream_url = i.data.get('stream')
if not stream_url:
return False
for s in i.streams():
if s.info['url'] != stream_url:
s.delete()
else:
return False
oshash = ox.net.oshash(stream_url)
if oshash == 'IOError':
print 'invalid url', stream_url
return False
f, created = archive.models.File.objects.get_or_create(oshash=oshash)
f.item = i
f.selected = True
f.available = True
f.info['extension'] = 'mp4'
f.path = stream_url
f.save()
s, created = archive.models.Stream.objects.get_or_create(source=None, file=f)
if created:
s.info['url'] = stream_url
s.available = True
s.resolution = 480
s.format = 'mp4'
s.save()
return True
return False
def update_streams():
import itemlist.models
l = itemlist.models.List.get('j:with Video')
for i in item.models.Item.objects.all():
print i
if update_item_stream(i):
l.add(i)