TrackDetailView, ArtistDetailView, ArtistListView, StatisticsView,
UploadTracksView, RecentTracksView, QuickLinksView, SearchView, CleanupView,
SearchCsvView, AddDiffusionView, AddStreamedDiffusionView,
+ DelStreamedDiffusionView,
jingle_audio_view)
urlpatterns = [
# soma management for episodes
url(r'^diffusion/(?P<pk>\d+)/add/$', AddDiffusionView.as_view(), name='nonstop-add-diffusion'),
url(r'^diffusion/(?P<pk>\d+)/add-stream/$', AddStreamedDiffusionView.as_view(), name='nonstop-add-streamed-diffusion'),
+ url(r'^diffusion/(?P<pk>\d+)/del-stream/$', DelStreamedDiffusionView.as_view(), name='nonstop-del-streamed-diffusion'),
url(r'^api/jingle/(?P<pk>\d+)/$', jingle_audio_view),
]
class DuplicateDiffusionSlot(Exception):
pass
+
+def soma_connection():
+ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ s.connect(('soma', 12521))
+ s.recv(1024) # -> b'soma 2.5 NO_SSL\n'
+ s.sendall(b'100 - Ok\n')
+ s.recv(1024) # -> b'100 - Welcome to soma daemon\n'
+ s.sendall(b'\n') # (empty password)
+ if s.recv(1024) != b'100 - Ok\n':
+ raise SomaException('failed to initialize soma connection')
+ return s
+
+
def add_diffusion(diffusion, **kwargs):
context = {}
streamed_diffusion = StreamedDiffusion.objects.filter(diffusion=diffusion).first()
palinsesti = palinsesti_template.render(context)
palinsesti_xml = ET.fromstring(palinsesti.encode('utf-8'))
- # add to soma
- def soma_connection():
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- s.connect(('soma', 12521))
- s.recv(1024) # -> b'soma 2.5 NO_SSL\n'
- s.sendall(b'100 - Ok\n')
- s.recv(1024) # -> b'100 - Welcome to soma daemon\n'
- s.sendall(b'\n') # (empty password)
- if s.recv(1024) != b'100 - Ok\n':
- raise SomaException('failed to initialize soma connection')
- return s
+ palinsesto_xml = get_palinsesto_xml()
+ palinsesto_xml.append(palinsesti_xml)
+ send_palinsesto_xml(palinsesto_xml)
- with soma_connection() as s:
- s.sendall(b'109 - Get the current palinsesto\n')
- palinsesto_bytes = b''
- while True:
- new_bytes = s.recv(200000)
- if not new_bytes:
- break
- palinsesto_bytes += new_bytes
- if not palinsesto_bytes.startswith(b'100 - Ok\n'):
- raise SomaException('failed to get palinsesto')
- palinsesto_bytes = palinsesto_bytes[9:]
- palinsesto_xml = ET.fromstring(palinsesto_bytes)
- palinsesto_xml.append(palinsesti_xml)
+def remove_streamed_diffusion(streamed_diffusion):
+ palinsesto_xml = get_palinsesto_xml()
+ stream_marker = '[stream:%s]' % streamed_diffusion.id
+ for palinsesto in palinsesto_xml.findall('Palinsesto'):
+ if palinsesto.findall('Description')[0].text.startswith(stream_marker):
+ palinsesto_xml.remove(palinsesto)
+ send_palinsesto_xml(palinsesto_xml)
+
+
+def send_palinsesto_xml(palinsesto_xml):
with soma_connection() as s:
s.sendall(b'106 - Switch to a New Palinsesto Request\n')
if s.recv(1024) != b'100 - Ok\n':
s.sendall(b'122 - Set the current Palinsesto as Default\n')
if s.recv(1024) != b'100 - Ok\n':
raise SomaException('failed to set current palinsesto as default')
+
+
+def get_palinsesto_xml():
+ with soma_connection() as s:
+ s.sendall(b'109 - Get the current palinsesto\n')
+ palinsesto_bytes = b''
+ while True:
+ new_bytes = s.recv(200000)
+ if not new_bytes:
+ break
+ palinsesto_bytes += new_bytes
+ if not palinsesto_bytes.startswith(b'100 - Ok\n'):
+ raise SomaException('failed to get palinsesto')
+ palinsesto_bytes = palinsesto_bytes[9:]
+ palinsesto_xml = ET.fromstring(palinsesto_bytes)
+ return palinsesto_xml
'slug': episode.slug})
+class DelStreamedDiffusionView(RedirectView):
+ def get_redirect_url(self):
+ diffusion = Diffusion.objects.get(id=self.kwargs['pk'])
+ streamed_diffusion = diffusion.streameddiffusion_set.first()
+ utils.remove_streamed_diffusion(streamed_diffusion)
+ streamed_diffusion.delete()
+ episode = diffusion.episode
+ return reverse('episode-view', kwargs={
+ 'emission_slug': episode.emission.slug,
+ 'slug': episode.slug})
+
+
def jingle_audio_view(request, *args, **kwargs):
jingle = Jingle.objects.get(id=kwargs['pk'])
return FileResponse(open(os.path.join(LOCAL_BASE_PATH, 'SPOTS', jingle.filepath), 'rb'))