pandora_prelinger/notes.py
2015-03-15 13:02:45 +00:00

44 lines
1.4 KiB
Python

from __future__ import division
import re
import ox
import item.models
import annotation.models
import user.models
def parse_notes(i, fps=29.97):
u = user.models.User.objects.all()[0]
text = i.data.get('summary', '')
parts = text.split('<br>\n<br>\n')
annotations = []
offset = None
for part in parts:
m = re.compile('^((\d{2}):(\d{2}):(\d{2}):(\d{2})) +(.*)').findall(part)
if m:
tc, h, m, s, f, txt = m[0]
seconds = int(h) * 60 * 60 + int(m) * 60 + int(s) + int(f)/fps
if offset == None:
offset = seconds - 60
timecode = ox.format_timecode(seconds - offset)
text = text.replace(tc, '<a href="/%s/%s">%s</a>' %(i.public_id, timecode, tc))
if annotations:
annotations[-1]['out'] = seconds - offset - 1/fps
annotations.append({
'in': seconds - offset,
'out': -1,
'value': txt
})
if annotations:
annotations[-1]['out'] = i.sort.duration
if text != i.data.get('summary', ''):
i.data['summary'] = text
i.save()
for d in annotations:
a = annotation.models.Annotation()
a.item = i
a.layer = 'publicnotes'
a.start = d['in']
a.end = d['out']
a.value = d['value']
a.user = u
a.save()
return text, annotations