pandora_ubu/data.py

109 lines
2.8 KiB
Python
Raw Normal View History

2015-03-15 13:04:15 +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, 'ubu.json')) as f:
data = json.load(f)
def get_item(ubu):
qs = item.models.Item.objects.filter(data__contains='"ubu": "%s"'%ubu)
if qs.count():
return qs[0], False
return item.models.Item(), True
def update_item(d, add_only=False):
info = {{
'artist': 'director',
'description': 'summary',
'id': 'ubu',
'mp4': 'stream',
}.get(k,k):d[k] for k in d}
for k in ('type', 'flv', 'video'):
if k in info:
del info[k]
if 'director' in info:
if len(info['director']) > 200:
info['case'] = info['director']
info['director'] = [info['director'].split('.')[0][:200]]
else:
info['director'] = [info['director']]
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()
i, created = get_item(info['ubu'])
if not created and add_only:
return
print info['ubu'], info.get('title', 'Untitled')
for k in info:
i.data[k] = info[k]
i.level = 0
i.save()
#i.make_poster(True)
def update_items(add_only=False):
for d in data:
update_item(d, add_only)
def update_item_stream(i, force=False):
if update_stream(i) or force:
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
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
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():
if i.public_id in ['HJ', 'ANV']:
continue
print i
if update_item_stream(i):
l.add(i)