utils.py 3.19 KB
Newer Older
1
import datetime
2 3
import os
import shutil
fred's avatar
fred committed
4
import time
5

6 7 8 9 10 11 12 13 14
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
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30


def get_current_nonstop_track():
    try:
        soma_log_line = SomaLogLine.objects.select_related().order_by('-play_timestamp')[0]
    except IndexError:
        # nothing yet
        return {}
    if soma_log_line.play_timestamp < (datetime.datetime.now() - datetime.timedelta(hours=1)):
        # last known line is way too old
        return {}
    if not soma_log_line.on_air:
        # nonstop should be on air but it's not :/
        return {}
    d = {}
    current_nonstop_file = soma_log_line.filepath
31
    if not 'Tranches/' in current_nonstop_file.filepath and (
32
            not 'tracks/' in current_nonstop_file.filepath):
33 34 35 36 37 38 39 40 41 42
        # nonstop is playing but it's not a nonstop track :/
        return {}
    current_track = soma_log_line.filepath.track
    if current_track is None:
        # nonstop is playing a nonstop track, but it's unknown :/
        return {}
    d = {'track_title': current_track.title}
    if current_track.artist:
        d['track_artist'] = current_track.artist.name
    return d
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87


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')
fred's avatar
fred committed
88 89 90 91 92
        del connection

        # give it some time (...)
        time.sleep(3)
        connection = pysoma.open_tcp('soma.panik', 12521, '', 0)
93 94
        connection.set_default_palinsesto()
        del connection