From 07b1a4c28a6b2dffd1799e4dac31a0f2c5d0259d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Sat, 31 Mar 2018 14:26:27 +0200 Subject: [PATCH] add possibility to push diffusion to soma (draft) --- nonstop/templates/nonstop/soma_palinsesti.xml | 34 +++++++++++ nonstop/templatetags/__init__.py | 0 nonstop/templatetags/nonstop.py | 10 ++++ nonstop/urls.py | 8 ++- nonstop/utils.py | 59 ++++++++++++++++++- nonstop/views.py | 18 +++++- 6 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 nonstop/templates/nonstop/soma_palinsesti.xml create mode 100644 nonstop/templatetags/__init__.py create mode 100644 nonstop/templatetags/nonstop.py diff --git a/nonstop/templates/nonstop/soma_palinsesti.xml b/nonstop/templates/nonstop/soma_palinsesti.xml new file mode 100644 index 0000000..87d493b --- /dev/null +++ b/nonstop/templates/nonstop/soma_palinsesti.xml @@ -0,0 +1,34 @@ +{% load l10n %} +{% localize off %} + + {{ episode.emission.title }} - {{ episode.title }} + + 1 + {{ start|date:"Y-m-d x H:i" }} + {{ end|date:"Y-m-d x H:i" }} + 0 + 0 + + files + + + + + + + + 0 + 1 + + 10 + 2 + + + :/nonstop/{{ diffusion_path }}/ + + + + + + +{% endlocalize %} diff --git a/nonstop/templatetags/__init__.py b/nonstop/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/nonstop/templatetags/nonstop.py b/nonstop/templatetags/nonstop.py new file mode 100644 index 0000000..e529846 --- /dev/null +++ b/nonstop/templatetags/nonstop.py @@ -0,0 +1,10 @@ +from django.template import Library + +from .. import utils + +register = Library() + + +@register.filter +def is_already_in_soma(diffusion): + return utils.is_already_in_soma(diffusion) diff --git a/nonstop/urls.py b/nonstop/urls.py index a17d571..e6c0774 100644 --- a/nonstop/urls.py +++ b/nonstop/urls.py @@ -1,6 +1,9 @@ from django.conf.urls import url -from .views import SomaDayArchiveView, SomaDayArchiveCsvView, RedirectTodayView, TrackDetailView, ArtistDetailView, ArtistListView, StatisticsView, UploadTracksView, RecentTracksView, QuickLinksView, SearchView, CleanupView, SearchCsvView +from .views import (SomaDayArchiveView, SomaDayArchiveCsvView, RedirectTodayView, + TrackDetailView, ArtistDetailView, ArtistListView, StatisticsView, + UploadTracksView, RecentTracksView, QuickLinksView, SearchView, CleanupView, + SearchCsvView, AddDiffusionView) urlpatterns = [ # Example: /2012/nov/10/ @@ -22,4 +25,7 @@ urlpatterns = [ url(r'^search/csv$', SearchCsvView.as_view(), name='nonstop-search-csv'), url(r'^quick-links/$', QuickLinksView.as_view(), name='nonstop-quick-links'), url(r'^cleanup/$', CleanupView.as_view(), name='nonstop-cleanup'), + + # soma mangement for episodes + url(r'^diffusion/(?P\d+)/add/$', AddDiffusionView.as_view(), name='nonstop-add-diffusion'), ] diff --git a/nonstop/utils.py b/nonstop/utils.py index f44895b..722b6b6 100644 --- a/nonstop/utils.py +++ b/nonstop/utils.py @@ -1,6 +1,16 @@ import datetime +import os +import shutil -from .models import Track, SomaLogLine +from django.template import loader, Context +import xml.etree.ElementTree as ET + +try: + import pysoma +except ImportError: + pysoma = None + +from .models import Track, SomaLogLine, LOCAL_BASE_PATH def get_current_nonstop_track(): @@ -29,3 +39,50 @@ def get_current_nonstop_track(): if current_track.artist: d['track_artist'] = current_track.artist.name return d + + +def get_diffusion_file_path(diffusion): + return u'diffusions-auto/%s--%s' % ( + diffusion.datetime.strftime('%Y%m%d-%H%M'), + diffusion.episode.emission.slug) + +def is_already_in_soma(diffusion): + if not pysoma: + # can't tell but as we couldn't add it anyway, let's lie. + return True + return os.path.exists(os.path.join(LOCAL_BASE_PATH, get_diffusion_file_path(diffusion))) + +def add_diffusion(diffusion): + soundfile = diffusion.episode.soundfile_set.filter(fragment=False)[0] + diffusion_path = get_diffusion_file_path(diffusion) + + # copy file + if os.path.exists(LOCAL_BASE_PATH): + os.mkdir(os.path.join(LOCAL_BASE_PATH, diffusion_path)) + shutil.copyfile(soundfile.file.path, + os.path.join(LOCAL_BASE_PATH, diffusion_path, os.path.basename(soundfile.file.path))) + + # create palinsesti + palinsesti_template = loader.get_template('nonstop/soma_palinsesti.xml') + context = Context() + context['diffusion_path'] = diffusion_path + context['episode'] = diffusion.episode + context['start'] = diffusion.datetime + # end should be a bit before the real end of file so the same file doesn't + # get repeated. + context['end'] = diffusion.datetime + datetime.timedelta(seconds=(soundfile.duration or 300) - 180) + + palinsesti = palinsesti_template.render(context) + palinsesti_xml = ET.fromstring(palinsesti.encode('utf-8')) + + # append to soma + if pysoma: + connection = pysoma.open_tcp('soma.panik', 12521, '', 0) + palinsesto_xml = ET.fromstring(connection.get_palinsesto()) + palinsesto_xml.append(palinsesti_xml) + with open('/tmp/soma.pl', 'w') as fd: + fd.write(ET.tostring(palinsesto_xml)) + + connection.new_palinsesto('/tmp/soma.pl') + connection.set_default_palinsesto() + del connection diff --git a/nonstop/views.py b/nonstop/views.py index 16cab27..b156987 100644 --- a/nonstop/views.py +++ b/nonstop/views.py @@ -22,7 +22,10 @@ from django.views.generic.list import ListView from .forms import UploadTracksForm, TrackMetaForm, TrackSearchForm, CleanupForm from .models import SomaLogLine, Track, Artist, NonstopFile -from emissions.models import Nonstop +from emissions.models import Nonstop, Diffusion + +from . import utils + class SomaDayArchiveView(DayArchiveView): queryset = SomaLogLine.objects.all() @@ -341,3 +344,16 @@ class CleanupView(TemplateView): if count: messages.info(self.request, 'Removed %d new track(s)' % count) return HttpResponseRedirect('.') + + +class AddDiffusionView(DetailView): + model = Diffusion + + def get(self, request, *args, **kwargs): + diffusion = self.get_object() + utils.add_diffusion(diffusion) + episode = diffusion.episode + messages.info(self.request, _('%s added to soma') % episode.emission.title) + return HttpResponseRedirect(reverse('episode-view', kwargs={ + 'emission_slug': episode.emission.slug, + 'slug': episode.slug})) -- 2.39.2