From 110debe26d50c64b5dbd6198c332c93a87650882 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Tue, 20 Dec 2011 01:11:37 +0530 Subject: [PATCH] tv --- pandora/settings.py | 1 + pandora/tv/__init__.py | 0 pandora/tv/models.py | 76 ++++++++++++++++++++++++++++++++++++++++++ pandora/tv/views.py | 21 ++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 pandora/tv/__init__.py create mode 100644 pandora/tv/models.py create mode 100644 pandora/tv/views.py diff --git a/pandora/settings.py b/pandora/settings.py index b1108d6bf..3554312f3 100644 --- a/pandora/settings.py +++ b/pandora/settings.py @@ -110,6 +110,7 @@ INSTALLED_APPS = ( 'user', 'api', 'urlalias', + 'tv', ) # Log errors into db diff --git a/pandora/tv/__init__.py b/pandora/tv/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/pandora/tv/models.py b/pandora/tv/models.py new file mode 100644 index 000000000..b2ba0d896 --- /dev/null +++ b/pandora/tv/models.py @@ -0,0 +1,76 @@ +# -*- coding: utf-8 -*- +# vi:si:et:sw=4:sts=4:ts=4 +from __future__ import division, with_statement +from datetime import datetime, timedelta +from random import randint + +from django.db import models + + +class Channel(models.Model): + created = models.DateTimeField(auto_now_add=True) + modified = models.DateTimeField(auto_now=True) + run = models.IntegerField(default=0) + + list = models.ForeignKey('itemlist.List', related_name='channel') + + def json(self, user): + now = datetime.now() + items = self.list.get_items().filter(rendered=True) + if items.count() == 0: + return + + program = self.program.order_by('-start') + while program.count() < 2 or program[0].end < now: + 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 + self.save() + not_played = items + not_played_count = not_played.count() + item = not_played[randint(0, not_played_count-1)] + if program.count() > 0: + start = program[0].end + else: + start = now + end = start + timedelta(seconds=item.get_json()['duration']) + p = Program() + p.item=item + p.run=self.run + p.start=start + p.end=end + p.channel = self + p.save() + program = self.program.order_by('-start') + print program.count(), now, p.start, p.end + current = program[1] + return { + 'current': current.json(user, now), + 'next': program[0].json(user) + } + +class Program(models.Model): + created = models.DateTimeField(auto_now_add=True) + modified = models.DateTimeField(auto_now=True) + run = models.IntegerField(default=0) + start = models.DateTimeField() + end = models.DateTimeField() + item = models.ForeignKey('item.Item', related_name='program') + channel = models.ForeignKey(Channel, related_name='program') + + def __unicode__(self): + return u"%s %s" % (self.item, self.start) + + def json(self, user, current=False): + item_json = self.item.get_json() + r = { + 'item': self.item.itemId, + 'durations': item_json['durations'], + 'parts': item_json['parts'], + } + r['layers'] = self.item.get_layers(user) + if current: + r['currentTime'] = (current - self.start).total_seconds() + + return r diff --git a/pandora/tv/views.py b/pandora/tv/views.py new file mode 100644 index 000000000..055de83f3 --- /dev/null +++ b/pandora/tv/views.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# vi:si:et:sw=4:sts=4:ts=4 +from __future__ import division + +import models +from ox.utils import json +from ox.django.decorators import login_required_json +from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response + +from itemlist.views import get_list_or_404_json +from api.actions import actions + +@login_required_json +def tv(request): + data = json.loads(request.POST['data']) + list = get_list_or_404_json(data['list']) + channel, created = models.Channel.objects.get_or_create(list=list) + response = json_response(status=200, text='created') + response['data'] = channel.json(request.user) + return render_to_json_response(response) +actions.register(tv, cache=False)