Commit 0c87d29c authored by fred's avatar fred

add view to remove a stream from soma

parent 57a6acd6
......@@ -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),
]
......@@ -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
......@@ -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'))
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