Commit 07b1a4c2 authored by fred's avatar fred

add possibility to push diffusion to soma (draft)

parent 7d18c20c
{% load l10n %}
{% localize off %}
<Palinsesto>
<Description color="#FF0000">{{ episode.emission.title }} - {{ episode.title }}</Description>
<Priority>1</Priority>
<Start>{{ start|date:"Y-m-d x H:i" }}</Start>
<Stop>{{ end|date:"Y-m-d x H:i" }}</Stop>
<TimeContinued>0</TimeContinued>
<SpotController>0</SpotController>
<Type>files</Type>
<Jingle></Jingle>
<Module></Module>
<ModuleData></ModuleData>
<Stream></Stream>
<Random>0</Random>
<SoftStop>1</SoftStop>
<RatioSong>10</RatioSong>
<RatioSpot>2</RatioSpot>
<PathSong>
<item>:/nonstop/{{ diffusion_path }}/</item>
</PathSong>
<PathSpot>
</PathSpot>
</Palinsesto>
{% endlocalize %}
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)
from django.conf.urls import url 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 = [ urlpatterns = [
# Example: /2012/nov/10/ # Example: /2012/nov/10/
...@@ -22,4 +25,7 @@ urlpatterns = [ ...@@ -22,4 +25,7 @@ urlpatterns = [
url(r'^search/csv$', SearchCsvView.as_view(), name='nonstop-search-csv'), url(r'^search/csv$', SearchCsvView.as_view(), name='nonstop-search-csv'),
url(r'^quick-links/$', QuickLinksView.as_view(), name='nonstop-quick-links'), url(r'^quick-links/$', QuickLinksView.as_view(), name='nonstop-quick-links'),
url(r'^cleanup/$', CleanupView.as_view(), name='nonstop-cleanup'), url(r'^cleanup/$', CleanupView.as_view(), name='nonstop-cleanup'),
# soma mangement for episodes
url(r'^diffusion/(?P<pk>\d+)/add/$', AddDiffusionView.as_view(), name='nonstop-add-diffusion'),
] ]
import datetime 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(): def get_current_nonstop_track():
...@@ -29,3 +39,50 @@ def get_current_nonstop_track(): ...@@ -29,3 +39,50 @@ def get_current_nonstop_track():
if current_track.artist: if current_track.artist:
d['track_artist'] = current_track.artist.name d['track_artist'] = current_track.artist.name
return d 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
...@@ -22,7 +22,10 @@ from django.views.generic.list import ListView ...@@ -22,7 +22,10 @@ from django.views.generic.list import ListView
from .forms import UploadTracksForm, TrackMetaForm, TrackSearchForm, CleanupForm from .forms import UploadTracksForm, TrackMetaForm, TrackSearchForm, CleanupForm
from .models import SomaLogLine, Track, Artist, NonstopFile from .models import SomaLogLine, Track, Artist, NonstopFile
from emissions.models import Nonstop from emissions.models import Nonstop, Diffusion
from . import utils
class SomaDayArchiveView(DayArchiveView): class SomaDayArchiveView(DayArchiveView):
queryset = SomaLogLine.objects.all() queryset = SomaLogLine.objects.all()
...@@ -341,3 +344,16 @@ class CleanupView(TemplateView): ...@@ -341,3 +344,16 @@ class CleanupView(TemplateView):
if count: if count:
messages.info(self.request, 'Removed %d new track(s)' % count) messages.info(self.request, 'Removed %d new track(s)' % count)
return HttpResponseRedirect('.') 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}))
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