utils.py 1012 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
import datetime

from .models import Track, SomaLogLine


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
    if not 'Tranches/' in current_nonstop_file.filepath:
        # 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