include next item tv api

This commit is contained in:
j 2023-11-04 11:54:47 +01:00
parent 615860a7e8
commit a2b7cc9744

View file

@ -31,23 +31,28 @@ class Channel(models.Model):
items = Item.objects.filter(rendered=True, level__lte=cansee, sort__duration__gt=0) items = Item.objects.filter(rendered=True, level__lte=cansee, sort__duration__gt=0)
if items.count() == 0: if items.count() == 0:
return None return None
program = self.program.order_by('-start') program = self.program.order_by('-start')
changed = False changed = False
while program.count() < 1 or program[0].end < now: play_now = program.filter(start__lte=now, end__gt=now).first()
not_played = items.exclude(program__in=self.program.filter(run=self.run)) while not play_now or not play_now.next():
not_played_count = not_played.count() played = self.program.filter(run=self.run)
if not_played_count == 0: if played.exists():
self.run += 1 not_played = items.exclude(program__in=self.program.filter(run=self.run))
not_played_count = not_played.count()
if not_played_count == 0:
self.run += 1
changed = True
else:
changed = True changed = True
if changed:
not_played = items not_played = items
not_played_count = not_played.count() not_played_count = not_played.count()
if not_played_count > 1: if not_played_count > 1 and program.exists():
not_played = not_played.exclude(id=program[0].id) not_played = not_played.exclude(id=program[0].id)
not_played_count = not_played.count() not_played_count = not_played.count()
item = not_played[randint(0, not_played_count-1)] item = not_played[randint(0, not_played_count-1)]
if program.count() > 0: if program.exists():
start = program.aggregate(Max('end'))['end__max'] start = program.order_by('-end')[0].end
else: else:
start = now start = now
p = Program() p = Program()
@ -58,9 +63,10 @@ class Channel(models.Model):
p.channel = self p.channel = self
p.save() p.save()
program = self.program.order_by('-start') program = self.program.order_by('-start')
play_now = program.filter(start__lte=now, end__gt=now).first()
if changed: if changed:
self.save() self.save()
return program[0] return play_now
def json(self, user): def json(self, user):
now = datetime.now() now = datetime.now()
@ -82,6 +88,9 @@ class Program(models.Model):
def __str__(self): def __str__(self):
return "%s %s" % (self.item, self.start) return "%s %s" % (self.item, self.start)
def next(self):
return self.channel.program.filter(start__gte=self.end).order_by('start').first()
def json(self, user, current=False): def json(self, user, current=False):
item_json = self.item.json() item_json = self.item.json()
r = { r = {
@ -92,8 +101,6 @@ class Program(models.Model):
r['layers'] = self.item.get_layers(user) r['layers'] = self.item.get_layers(user)
r['streams'] = [s.file.oshash for s in self.item.streams()] r['streams'] = [s.file.oshash for s in self.item.streams()]
if current: if current:
#requires python2.7 r['position'] = (current - self.start).total_seconds()
#r['position'] = (current - self.start).total_seconds() r['next'] = self.next().json(user)
td = current - self.start
r['position'] = td.seconds + td.days * 24 * 3600 + float(td.microseconds)/10**6
return r return r