]> git.0d.be Git - django-panik-nonstop.git/blob - nonstop/utils.py
d3d71dbef1e30310777313eaaf678540411d545d
[django-panik-nonstop.git] / nonstop / utils.py
1 import datetime
2 import os
3 import shutil
4 import time
5
6 from django.template import loader
7 import xml.etree.ElementTree as ET
8
9 try:
10     import pysoma
11 except ImportError:
12     pysoma = None
13
14 from .models import Track, SomaLogLine, LOCAL_BASE_PATH
15
16
17 def get_current_nonstop_track():
18     try:
19         soma_log_line = SomaLogLine.objects.select_related().order_by('-play_timestamp')[0]
20     except IndexError:
21         # nothing yet
22         return {}
23     if soma_log_line.play_timestamp < (datetime.datetime.now() - datetime.timedelta(hours=1)):
24         # last known line is way too old
25         return {}
26     if not soma_log_line.on_air:
27         # nonstop should be on air but it's not :/
28         return {}
29     d = {}
30     current_nonstop_file = soma_log_line.filepath
31     if not 'Tranches/' in current_nonstop_file.filepath and (
32             not 'tracks/' in current_nonstop_file.filepath):
33         # nonstop is playing but it's not a nonstop track :/
34         return {}
35     current_track = soma_log_line.filepath.track
36     if current_track is None:
37         # nonstop is playing a nonstop track, but it's unknown :/
38         return {}
39     d = {'track_title': current_track.title}
40     if current_track.artist:
41         d['track_artist'] = current_track.artist.name
42     return d
43
44
45 def get_diffusion_file_path(diffusion):
46     return u'diffusions-auto/%s--%s' % (
47             diffusion.datetime.strftime('%Y%m%d-%H%M'),
48             diffusion.episode.emission.slug)
49
50 def is_already_in_soma(diffusion):
51     return os.path.exists(os.path.join(LOCAL_BASE_PATH, get_diffusion_file_path(diffusion)))
52
53 class DuplicateDiffusionSlot(Exception):
54     pass
55
56 def add_diffusion(diffusion):
57     soundfile = diffusion.episode.soundfile_set.filter(fragment=False)[0]
58     diffusion_path = get_diffusion_file_path(diffusion)
59
60     # copy file
61     if os.path.exists(LOCAL_BASE_PATH):
62         if os.path.exists(os.path.join(LOCAL_BASE_PATH, diffusion_path)):
63             raise DuplicateDiffusionSlot()
64         os.mkdir(os.path.join(LOCAL_BASE_PATH, diffusion_path))
65         shutil.copyfile(soundfile.file.path,
66             os.path.join(LOCAL_BASE_PATH, diffusion_path, os.path.basename(soundfile.file.path)))
67
68     # create palinsesti
69     palinsesti_template = loader.get_template('nonstop/soma_palinsesti.xml')
70     context = {}
71     context['diffusion_path'] = diffusion_path
72     context['episode'] = diffusion.episode
73     context['start'] = diffusion.datetime
74     # end should be a bit before the real end of file so the same file doesn't
75     # get repeated.
76     context['end'] = diffusion.datetime + datetime.timedelta(seconds=(soundfile.duration or 300) - 180)
77
78     palinsesti = palinsesti_template.render(context)
79     palinsesti_xml = ET.fromstring(palinsesti.encode('utf-8'))
80
81     # append to soma
82     if pysoma:
83         connection = pysoma.open_tcp('soma.panik', 12521, '', 0)
84         palinsesto_xml = ET.fromstring(connection.get_palinsesto())
85         palinsesto_xml.append(palinsesti_xml)
86         with open('/tmp/soma.pl', 'w') as fd:
87             fd.write(ET.tostring(palinsesto_xml))
88
89         connection.new_palinsesto('/tmp/soma.pl')
90         del connection
91
92         # give it some time (...)
93         time.sleep(3)
94         connection = pysoma.open_tcp('soma.panik', 12521, '', 0)
95         connection.set_default_palinsesto()
96         del connection