Commit 01acdee0 authored by fred's avatar fred
Browse files

move whatsonair code to a utility module

parent 5dfb4923
from datetime import datetime, timedelta
import xml.etree.ElementTree as ET
from django.core.management.base import BaseCommand, CommandError
from ...models import Emission, Episode, Diffusion, SoundFile, Schedule
from ...utils import whatsonair
class Command(BaseCommand):
def handle(self, *args, **options):
now = datetime.now()
date_start = now - timedelta(days=1)
date_end = now + timedelta(days=1)
diffusions = Diffusion.objects.filter(
datetime__range=(date_start, date_end)).order_by('datetime')
diffusions = [x for x in diffusions if x.datetime <= now and
now <= x.datetime + timedelta(minutes=x.episode.emission.duration)]
episode = None
emission = None
if diffusions: # there should be only one
diffusion = diffusions[0]
episode = diffusion.episode
emission = episode.emission
else:
for schedule in Schedule.objects.all():
if not schedule.matches(now):
continue
emission = schedule.emission
break
print 'found episode:', episode
print 'found emission:', emission
onair = whatsonair()
print 'found episode:', onair.get('episode')
print 'found emission:', onair.get('emission')
from datetime import datetime, timedelta
from PIL import Image
def maybe_resize(image_path):
......@@ -11,3 +12,29 @@ def maybe_resize(image_path):
image.save(image_path)
def whatsonair():
from models import Diffusion, Schedule
now = datetime.now()
date_start = now - timedelta(days=1)
date_end = now + timedelta(days=1)
diffusions = Diffusion.objects.filter(
datetime__range=(date_start, date_end)).order_by('datetime')
diffusions = [x for x in diffusions if x.datetime <= now and
now <= x.datetime + timedelta(minutes=x.episode.emission.duration)]
episode = None
emission = None
if diffusions: # there should be only one
diffusion = diffusions[0]
episode = diffusion.episode
emission = episode.emission
else:
for schedule in Schedule.objects.all():
if not schedule.matches(now):
continue
emission = schedule.emission
break
return {'emission': emission,
'episode': episode}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment