]> git.0d.be Git - django-panik-nonstop.git/commitdiff
add view to remove a stream from soma
authorFrédéric Péters <fpeters@0d.be>
Tue, 11 Feb 2020 19:33:56 +0000 (20:33 +0100)
committerFrédéric Péters <fpeters@0d.be>
Tue, 11 Feb 2020 19:33:56 +0000 (20:33 +0100)
nonstop/urls.py
nonstop/utils.py
nonstop/views.py

index 96d70362ba3eb2306b31b0cb1e329aa40e5f029c..4439c7d1bef1d2878b594e7614e5ac82a06a4820 100644 (file)
@@ -4,6 +4,7 @@ from .views import (SomaDayArchiveView, SomaDayArchiveCsvView, RedirectTodayView
         TrackDetailView, ArtistDetailView, ArtistListView, StatisticsView,
         UploadTracksView, RecentTracksView, QuickLinksView, SearchView, CleanupView,
         SearchCsvView, AddDiffusionView, AddStreamedDiffusionView,
+        DelStreamedDiffusionView,
         jingle_audio_view)
 
 urlpatterns = [
@@ -30,5 +31,6 @@ 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),
 ]
index 806fb12467c03422c16965e762fd825d0c6fe5fb..304ebba9ffd87e072edb79fc54c7d8125223fe49 100644 (file)
@@ -55,6 +55,19 @@ def is_already_in_soma(diffusion):
 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()
@@ -102,32 +115,21 @@ def add_diffusion(diffusion, **kwargs):
     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':
@@ -140,3 +142,19 @@ def add_diffusion(diffusion, **kwargs):
         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
index 937a8cb08ebc260cb4496867dc0f6fe3ff52fd05..f6da185b21bef0b6be324e1947c24199b2b90163 100644 (file)
@@ -414,6 +414,18 @@ class AddStreamedDiffusionView(CreateView):
             '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'))