Skip to content

Commit

Permalink
Merge pull request #21 from wuvt/underwriting_queue_resupport
Browse files Browse the repository at this point in the history
Underwriting queue resupport
  • Loading branch information
mutantmonkey authored Jun 14, 2022
2 parents 293f3c6 + b81d5b9 commit c9d32cc
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 14 deletions.
81 changes: 81 additions & 0 deletions queue_underwriting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env python3

import yaml
import datetime
import calendar
import socket
import random
import sys

underwritingSchedule = '/tmp/sample.yml'
telnetHost = '172.17.0.2'
telnetPort = 1234

underwriters = yaml.safe_load(open(underwritingSchedule))
thisHour = []


def push_command(url):
cmd = "underwriting.push {}\n".format(url).encode('utf-8')
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((telnetHost, telnetPort))
except OSError as e:
print("Cannot push to socket: {0}".format(e))
return False
# queue the first one immediately
sock.send(cmd)
if datetime.datetime.now().minute > 30:
# we must be queuing for the next hour, so add the second
sock.send(cmd)
sock.send(b'exit\n')
return True


# check which hour we're queuing for and return datetime object
def what_hour():
queue_hour = datetime.datetime.now()
if queue_hour.minute > 30:
# queuing for next hour
queue_hour = queue_hour + datetime.timedelta(hours=1)
return queue_hour


queue_hour = what_hour()

for underwriting in underwriters:
today = queue_hour.date()
weekday = calendar.day_name[queue_hour.weekday()].lower()
# we want to sanely handle do-not-kill situations
if ((not underwriting['startdate'] or
underwriting['startdate'] <= today) and
(not underwriting['enddate'] or
underwriting['enddate'] >= today))and\
weekday in underwriting['schedule'] and\
queue_hour.hour in underwriting['schedule'][weekday]:
if len(underwriting['url']) > 1:
# Then we probably have multiple recordings of the grant statement
playFile = random.choice(underwriting['url'])
else:
# we probably don't need this, since a random int between 0 and 0
# is 0
playFile = underwriting['url'][0]
# probably the safer way to handle things in the future, but would
# require more parsing
# thisHour[underwriting['name']] = playFile
thisHour.append(playFile)

# because automation currently only plays one piece of underwriting at a time,
# we need arbitrate which piece we push. We'll pick a random one!
if len(thisHour) is not 0:
if push_command(random.choice(thisHour)):
# ideally, we would list what underwriting was queued, but we're being
# kind of sloppy with cases of overlapping underwriting
if len(thisHour) > 1:
print("There are {} underwriters this hour!".format(len(thisHour)))
print("Underwriting queued for {}".format(queue_hour))
else:
print("Failed to queue underwriting for {}".format(queue_hour))
sys.exit(1)
else:
print("No underwriting to queue for {}".format(queue_hour))
31 changes: 17 additions & 14 deletions radio.liq
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

%include "config.liq"

def underwriting_request() =
result = list.hd(default="", get_process_lines(
"curl -fsL #{playlist_api_url}/underwriting"))
if result == "" then
[]
else
[request.create(result)]
end
end
# UNDERWRITING API not implemented
#def underwriting_request() =
# result = list.hd(default="", get_process_lines(
# "curl -fsL #{playlist_api_url}/underwriting"))
# if result == "" then
# []
# else
# [request.create(result)]
# end
#end

def next_track_request() =
result = list.hd(default="", get_process_lines(
Expand Down Expand Up @@ -114,11 +115,13 @@ def log_metadata_async(m) =
end

# create underwriting queue - if API is disabled, just leave it empty
underwriting = if playlist_api_url != "" then
eat_blank(audio_to_stereo(request.dynamic.list(id="underwriting", retry_delay=900., underwriting_request)))
else
empty(id="underwriting")
end
#underwriting = if playlist_api_url != "" then
# eat_blank(audio_to_stereo(request.dynamic.list(id="underwriting", retry_delay=900., underwriting_request)))
#else
# empty(id="underwriting")
#end

underwriting = request.queue(id="underwriting")

# build up the track queues; provide a local one and one that uses the API
tracks_local = request.dynamic.list(id="tracks_local", next_track_local_request)
Expand Down

0 comments on commit c9d32cc

Please sign in to comment.