def get_event_at_time(sid, epoch_time, queued_events=False): if queued_events: in_queue = db.c.fetch_row( "SELECT sched_id, sched_type FROM r4_schedule WHERE sid = %s AND sched_start = 0 ORDER BY sched_id LIMIT 1", (sid, )) if in_queue: return event.load_by_id_and_type(in_queue['sched_id'], in_queue['sched_type']) at_time = db.c.fetch_row( "SELECT sched_id, sched_type FROM r4_schedule WHERE sid = %s AND sched_start <= %s AND sched_end > %s ORDER BY (%s - sched_start) LIMIT 1", (sid, epoch_time + 5, epoch_time, epoch_time)) if at_time: return event.load_by_id_and_type(at_time['sched_id'], at_time['sched_type']) elif epoch_time >= time.time(): return None else: # We add 5 seconds here in order to make up for any crossfading and buffering times that can screw up the radio timing elec_id = db.c.fetch_var( "SELECT elec_id FROM r4_elections WHERE r4_elections.sid = %s AND elec_start_actual <= %s ORDER BY elec_start_actual DESC LIMIT 1", (sid, epoch_time - 5)) if elec_id: return event.Election.load_by_id(elec_id) else: return None
def get_event_at_time(sid, epoch_time, queued_events = False): if queued_events: in_queue = db.c.fetch_row("SELECT sched_id, sched_type FROM r4_schedule WHERE sid = %s AND sched_start = 0 ORDER BY sched_id LIMIT 1", (sid,)) if in_queue: return event.load_by_id_and_type(in_queue['sched_id'], in_queue['sched_type']) at_time = db.c.fetch_row("SELECT sched_id, sched_type FROM r4_schedule WHERE sid = %s AND sched_start <= %s AND sched_end > %s ORDER BY (%s - sched_start) LIMIT 1", (sid, epoch_time + 5, epoch_time, epoch_time)) if at_time: return event.load_by_id_and_type(at_time['sched_id'], at_time['sched_type']) elif epoch_time >= time.time(): return None else: # We add 5 seconds here in order to make up for any crossfading and buffering times that can screw up the radio timing elec_id = db.c.fetch_var("SELECT elec_id FROM r4_elections WHERE r4_elections.sid = %s AND elec_start_actual <= %s ORDER BY elec_start_actual DESC LIMIT 1", (sid, epoch_time - 5)) if elec_id: return event.Election.load_by_id(elec_id) else: return None
def get_event_in_progress(sid): in_progress = db.c.fetch_row( "SELECT sched_id, sched_type FROM r4_schedule WHERE sid = %s AND sched_in_progress = TRUE ORDER BY sched_start DESC LIMIT 1", (sid, )) if in_progress: return event.load_by_id_and_type(in_progress['sched_id'], in_progress['sched_type']) else: return get_event_at_time(sid, int(time.time()), queued_events=True)
def get_event_in_progress(sid): in_progress = db.c.fetch_row("SELECT sched_id, sched_type FROM r4_schedule WHERE sid = %s AND sched_in_progress = TRUE ORDER BY sched_start DESC LIMIT 1", (sid,)) if in_progress: return event.load_by_id_and_type(in_progress['sched_id'], in_progress['sched_type']) else: return get_event_at_time(sid, int(time.time()), queued_events=True)