<commit_before># -*- coding: utf-8 -*-

from datetime import datetime
from datetime import timedelta

import sys
import time
import logging
import logging.config
import telnetlib
import calendar
import json
import math
from pypofetch import PypoFetch

from Queue import Empty

from threading import Thread

from api_clients import api_client
from std_err_override import LogWriter
from configobj import ConfigObj


# configure logging
logging.config.fileConfig("logging.cfg")
logger = logging.getLogger()
LogWriter.override_std_err(logger)

#need to wait for Python 2.7 for this..
#logging.captureWarnings(True)

# loading config file
try:
    config = ConfigObj('/etc/airtime/pypo.cfg')
    LS_HOST = config['ls_host']
    LS_PORT = config['ls_port']
    PUSH_INTERVAL = 2
    MAX_LIQUIDSOAP_QUEUE_LENGTH = 2
except Exception, e:
    logger.error('Error loading config file %s', e)
    sys.exit()

class PypoPush(Thread):
    def __init__(self, q, telnet_lock):
        Thread.__init__(self)
        self.api_client = api_client.api_client_factory(config)
        self.queue = q

        self.telnet_lock = telnet_lock

        self.pushed_objects = {}
        self.logger = logging.getLogger('push')
        
    def main(self):
        loops = 0
        heartbeat_period = math.floor(30/PUSH_INTERVAL)
        
        next_media_item_chain = None
        media_schedule = None
        time_until_next_play = None
        chains = None
                
        while True:
            try:
                if time_until_next_play is None:
                    media_schedule = self.queue.get(block=True)
                else:
                    media_schedule = self.queue.get(block=True, timeout=time_until_next_play)
                                        
                chains = self.get_all_chains(media_schedule)                         
                
                #We get to the following lines only if a schedule was received.
                liquidsoap_queue_approx = self.get_queue_items_from_liquidsoap()

                current_event_chain = self.get_current_chain(chains)                
                if len(current_event_chain) > 0 and len(liquidsoap_queue_approx) == 0:
                    #Something is scheduled but Liquidsoap is not playing anything!
                    #Need to schedule it immediately..this might happen if Liquidsoap crashed.
                    chains.remove(current_event_chain)
                    
                    self.modify_cue_point(current_event_chain[0])
                    next_media_item_chain = current_event_chain
                    time_until_next_play = 0
                else:
                    media_chain = filter(lambda item: (item["type"] == "file"), current_event_chain)
                    self.handle_new_media_schedule(media_schedule, liquidsoap_queue_approx, media_chain)
                    
                    next_media_item_chain = self.get_next_schedule_chain(chains)
                    chains.remove(next_media_item_chain)
                    
                    self.logger.debug("Next schedule chain: %s", next_media_item_chain)                
                    if next_media_item_chain is not None:
                        tnow = datetime.utcnow()
                        chain_start = datetime.strptime(next_media_item_chain[0]['start'], "%Y-%m-%d-%H-%M-%S")
                        time_until_next_play = self.date_interval_to_seconds(chain_start - tnow)
                        self.logger.debug("Blocking %s seconds until show start", time_until_next_play)
                    else:
                        self.logger.debug("Blocking indefinitely since no show scheduled")
                        time_until_next_play = None
            except Empty, e:
                #We only get here when a new chain of tracks are ready to be played.
                self.push_to_liquidsoap(next_media_item_chain)
                                <commit_msg>fix bug with pypo crashing<commit_after># -*- coding: utf-8 -*-

from datetime import datetime
from datetime import timedelta

import sys
import time
import logging
import logging.config
import telnetlib
import calendar
import json
import math
from pypofetch import PypoFetch

from Queue import Empty

from threading import Thread

from api_clients import api_client
from std_err_override import LogWriter
from configobj import ConfigObj


# configure logging
logging.config.fileConfig("logging.cfg")
logger = logging.getLogger()
LogWriter.override_std_err(logger)

#need to wait for Python 2.7 for this..
#logging.captureWarnings(True)

# loading config file
try:
    config = ConfigObj('/etc/airtime/pypo.cfg')
    LS_HOST = config['ls_host']
    LS_PORT = config['ls_port']
    PUSH_INTERVAL = 2
    MAX_LIQUIDSOAP_QUEUE_LENGTH = 2
except Exception, e:
    logger.error('Error loading config file %s', e)
    sys.exit()

class PypoPush(Thread):
    def __init__(self, q, telnet_lock):
        Thread.__init__(self)
        self.api_client = api_client.api_client_factory(config)
        self.queue = q

        self.telnet_lock = telnet_lock

        self.pushed_objects = {}
        self.logger = logging.getLogger('push')
        
    def main(self):
        loops = 0
        heartbeat_period = math.floor(30/PUSH_INTERVAL)
        
        next_media_item_chain = None
        media_schedule = None
        time_until_next_play = None
        chains = None
                
        while True:
            try:
                if time_until_next_play is None:
                    media_schedule = self.queue.get(block=True)
                else:
                    media_schedule = self.queue.get(block=True, timeout=time_until_next_play)
                                        
                chains = self.get_all_chains(media_schedule)                         
                
                #We get to the following lines only if a schedule was received.
                liquidsoap_queue_approx = self.get_queue_items_from_liquidsoap()

                current_event_chain = self.get_current_chain(chains)                
                if len(current_event_chain) > 0 and len(liquidsoap_queue_approx) == 0:
                    #Something is scheduled but Liquidsoap is not playing anything!
                    #Need to schedule it immediately..this might happen if Liquidsoap crashed.
                    chains.remove(current_event_chain)
                    
                    self.modify_cue_point(current_event_chain[0])
                    next_media_item_chain = current_event_chain
                    time_until_next_play = 0
                else:
                    media_chain = filter(lambda item: (item["type"] == "file"), current_event_chain)
                    self.handle_new_media_schedule(media_schedule, liquidsoap_queue_approx, media_chain)
                    
                    next_media_item_chain = self.get_next_schedule_chain(chains)
                    
                    self.logger.debug("Next schedule chain: %s", next_media_item_chain)                
                    if next_media_item_chain is not None:
                        chains.remove(next_media_item_chain)
                        tnow = datetime.utcnow()
                        chain_start = datetime.strptime(next_media_item_chain[0]['start'], "%Y-%m-%d-%H-%M-%S")
                        time_until_next_play = self.date_interval_to_seconds(chain_start - tnow)
                        self.logger.debug("Blocking %s seconds until show start", time_until_next_play)
                    else:
                        self.logger.debug("Blocking indefinitely since no show scheduled")
                        time_until_next_play = None
            except Empty, e:
                #We only get here when a new chain of tracks are ready to be played.
                self.push_to_liquidsoap(next_media_item_chain)
                                
{'fraction_non_alphanumeric': 0.06691074359298069, 'fraction_numerical': 0.003282413836636788, 'mean_word_length': 1.8671733622873687, 'pattern_counts': {'":': 0, '<': 3, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3599989', 'n_tokens_mistral': 2179, 'n_tokens_neox': 2038, 'n_words': 500}