<commit_before>from django.core.context_processors import csrf
from django.shortcuts import render_to_response
from django.http import HttpResponse
from django import forms
from models import Master, Standby, State
from program.models import TimeSlot

import json
import time
from datetime import datetime

DB = 'nop'
MUSIKPROG_ID = 1 # unmodieriertes musikprogramm

class NopForm(forms.Form):
    date = forms.DateField(
            required=True,
            initial=datetime.date(datetime.now()),
            widget=forms.DateInput(
                format='%Y-%m-%d',
                attrs={'id':'nop_date', 'class':'date'})
            )
    time = forms.TimeField(
            required=True,
            initial=datetime.time(datetime.now()),
            widget=forms.TimeInput(
                format='%H:%M',
                attrs={'id':'nop_time', 'class':'date'})
            )

def _dtstring(dt):
    return time.strftime('%Y-%m-%d %H:%M', dt)

def _which(timestamp=None):
    if timestamp:
        res = State.objects.using(DB).filter(timestamp__lt=timestamp)[0]
    else:
        res = State.objects.using(DB).all()[0]
    if not res or res.state == 'master':
        return Master
    else:
        return Standby

def _get_show(datetime = None):
    try:
        if datetime:
            timeslot = TimeSlot.objects.get(start__lte=datetime, end__gt=datetime)
        else:
            timeslot = TimeSlot.objects.get_or_create_current()
        return {'start': _dtstring(timeslot.start.timetuple()),
                'id': timeslot.show.id,
                'name': timeslot.show.name}
    except: # e.g. DoesNotExist
        return {'start': None, 'id': None, 'name': None}


def _current():
    #current = int(time.time())*1000000
    #time.gmtime(_which().objects.using(DB).all()[6000].timestamp//1000000)
    # select all where timestamp < givenTS, get most recent one -> order DESC

    artist = None
    title = None
    album = None
    show = _get_show()
    if show['id'] == MUSIKPROG_ID:
        # reverse sorted. get the first object = last played
        result = _which().objects.using(DB).all()[0]
        artist = result.artist
        title = result.title
        album = result.album
    return {'show': show['name'],
            'start': show['start'],
            'artist': artist,
            'title': title,
            'album': album}

def _bydate(year=None, month=None, day=None, hour=None, minute=None):
    #try:
        #import pdb;pdb.set_trace()
        show = _get_show(datetime(year, month, day, hour, minute))
        if show['id'] and show['id'] != MUSIKPROG_ID:
            return [{'show': show['name'],
                     'start': show['start'],
                     'artist': None,
                     'title': None,
                     'album': None}]<commit_msg>extend trackinfo retrieving for more shows<commit_after>from django.core.context_processors import csrf
from django.shortcuts import render_to_response
from django.http import HttpResponse
from django import forms
from models import Master, Standby, State
from program.models import TimeSlot

import json
import time
from datetime import datetime

DB = 'nop'
MUSIKPROG_IDS = (1,17,81)
        # unmodieriertes musikprogramm
        # bumbumtschak
        # selchfleisch

class NopForm(forms.Form):
    date = forms.DateField(
            required=True,
            initial=datetime.date(datetime.now()),
            widget=forms.DateInput(
                format='%Y-%m-%d',
                attrs={'id':'nop_date', 'class':'date'})
            )
    time = forms.TimeField(
            required=True,
            initial=datetime.time(datetime.now()),
            widget=forms.TimeInput(
                format='%H:%M',
                attrs={'id':'nop_time', 'class':'date'})
            )

def _dtstring(dt):
    return time.strftime('%Y-%m-%d %H:%M', dt)

def _which(timestamp=None):
    if timestamp:
        res = State.objects.using(DB).filter(timestamp__lt=timestamp)[0]
    else:
        res = State.objects.using(DB).all()[0]
    if not res or res.state == 'master':
        return Master
    else:
        return Standby

def _get_show(datetime = None):
    try:
        if datetime:
            timeslot = TimeSlot.objects.get(start__lte=datetime, end__gt=datetime)
        else:
            timeslot = TimeSlot.objects.get_or_create_current()
        return {'start': _dtstring(timeslot.start.timetuple()),
                'id': timeslot.show.id,
                'name': timeslot.show.name}
    except: # e.g. DoesNotExist
        return {'start': None, 'id': None, 'name': None}


def _current():
    #current = int(time.time())*1000000
    #time.gmtime(_which().objects.using(DB).all()[6000].timestamp//1000000)
    # select all where timestamp < givenTS, get most recent one -> order DESC

    artist = None
    title = None
    album = None
    show = _get_show()
    if show['id'] in MUSIKPROG_IDS:
        # reverse sorted. get the first object = last played
        result = _which().objects.using(DB).all()[0]
        artist = result.artist
        title = result.title
        album = result.album
    return {'show': show['name'],
            'start': show['start'],
            'artist': artist,
            'title': title,
            'album': album}

def _bydate(year=None, month=None, day=None, hour=None, minute=None):
    #try:
        #import pdb;pdb.set_trace()
        show = _get_show(datetime(year, month, day, hour, minute))
        if show['id'] and show['id'] not in MUSIKPROG_IDS:
            return [{'show': show['name'],
                     'start': show['start'],
                     'artist': None,
                     'title': None,
                     'album': None}]
{'fraction_non_alphanumeric': 0.13189113747383113, 'fraction_numerical': 0.00837404047452896, 'mean_word_length': 2.2778730703259007, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2038250', 'n_tokens_mistral': 1761, 'n_tokens_neox': 1691, 'n_words': 405}