matches. load subtitle annotations into database

This commit is contained in:
j 2011-06-01 18:40:39 +02:00
parent 81cc331aa9
commit e4519b41a4
4 changed files with 32 additions and 18 deletions

View file

@ -43,7 +43,7 @@ class File(models.Model):
episode = models.IntegerField(default=-1)
size = models.BigIntegerField(default=0)
duration = models.BigIntegerField(null=True)
duration = models.FloatField(null=True)
info = fields.DictField(default={})
@ -143,8 +143,8 @@ class File(models.Model):
if not self.is_audio and not self.is_video and self.name.endswith('.srt'):
self.is_subtitle = True
self.part = self.get_part()
self.type = self.get_type()
self.part = self.get_part()
if self.type not in ('audio', 'video'):
self.duration = None
@ -166,7 +166,7 @@ class File(models.Model):
return self.data.read()
return None
def srt(self):
def srt(self, offset=0):
def _detectEncoding(fp):
bomDict={ # bytepattern : name
@ -203,7 +203,7 @@ class File(models.Model):
return encoding
def parseTime(t):
return ox.time2ms(t.replace(',', '.')) / 1000
return offset + ox.time2ms(t.replace(',', '.')) / 1000
srt = []

View file

@ -89,15 +89,3 @@ def update_files(user, volume, files):
#FIXME: can this have any bad consequences? i.e. on the selction of used item files.
models.Instance.objects.filter(volume=volume).exclude(file__oshash__in=all_files).delete()
def import_subtitles(id):
f = models.File.objects.get(pk=id)
layer = models.Layer.objects.get(name='subtitles')
for data in f.srt():
annotation = models.Annotation(
item=f.item,
layer=layer,
start=data['in'],
end=data['out'],
value=data['value']
)
annotation.save()

View file

@ -3,6 +3,7 @@
from datetime import timedelta
from celery.decorators import task, periodic_task
from django.db.models import Q
import models
@ -34,3 +35,24 @@ def update_streams(itemId):
if item.files.filter(is_main=True, is_video=True, available=False).count() == 0:
item.update_streams()
return True
def load_subtitles(itemId):
item = models.Item.objects.get(itemId=itemId)
layer = models.Layer.objects.get(name='subtitles')
models.Annotation.objects.filter(layer=layer,item=item).delete()
offset = 0
for f in item.files.filter(is_main=True, is_subtitle=True, available=True).order_by('part'):
user = f.instances.all()[0].volume.user
for data in f.srt(offset):
annotation = models.Annotation(
item=f.item,
layer=layer,
start=data['in'],
end=data['out'],
value=data['value'],
user=user
)
annotation.save()
duration = item.files.filter(Q(is_audio=True)|Q(is_video=True)) \
.filter(is_main=True, available=True, part=f.part)[0].duration
offset += duration

View file

@ -6,8 +6,10 @@ from django.db import models
import ox
from ox.django import fields
from django.contrib.auth.models import User, Group
from django.db.models import Q
import managers
from annotation.models import Annotation
class Place(models.Model):
'''
@ -69,8 +71,10 @@ class Place(models.Model):
return j
def update_matches(self):
import random
self.matches = random.randInt(0, 100)
q = Q(value__icontains=self.name)
for name in self.alternativeNames:
q = q|Q(value__icontains=name)
self.matches = Annotation.objects.filter(q).count()
self.save()
def save(self, *args, **kwargs):