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