Format code using black
This commit is contained in:
parent
efe4fa027e
commit
c27f020d73
85 changed files with 3238 additions and 2243 deletions
|
@ -24,6 +24,7 @@ import mutagen
|
|||
from api_clients import version1 as v1_api_client
|
||||
from api_clients import version2 as api_client
|
||||
|
||||
|
||||
def api_client(logger):
|
||||
"""
|
||||
api_client returns the correct instance of AirtimeApiClient. Although there is only one
|
||||
|
@ -31,15 +32,17 @@ def api_client(logger):
|
|||
"""
|
||||
return v1_api_client.AirtimeApiClient(logger)
|
||||
|
||||
|
||||
# loading config file
|
||||
try:
|
||||
config = ConfigObj('/etc/airtime/airtime.conf')
|
||||
config = ConfigObj("/etc/airtime/airtime.conf")
|
||||
except Exception as e:
|
||||
print("Error loading config file: {}".format(e))
|
||||
sys.exit()
|
||||
|
||||
# TODO : add docstrings everywhere in this module
|
||||
|
||||
|
||||
def getDateTimeObj(time):
|
||||
# TODO : clean up for this function later.
|
||||
# - use tuples to parse result from split (instead of indices)
|
||||
|
@ -49,17 +52,20 @@ def getDateTimeObj(time):
|
|||
# shadowed
|
||||
# - add docstring to document all behaviour of this function
|
||||
timeinfo = time.split(" ")
|
||||
date = [ int(x) for x in timeinfo[0].split("-") ]
|
||||
my_time = [ int(x) for x in timeinfo[1].split(":") ]
|
||||
return datetime.datetime(date[0], date[1], date[2], my_time[0], my_time[1], my_time[2], 0, None)
|
||||
date = [int(x) for x in timeinfo[0].split("-")]
|
||||
my_time = [int(x) for x in timeinfo[1].split(":")]
|
||||
return datetime.datetime(
|
||||
date[0], date[1], date[2], my_time[0], my_time[1], my_time[2], 0, None
|
||||
)
|
||||
|
||||
|
||||
PUSH_INTERVAL = 2
|
||||
|
||||
class ShowRecorder(Thread):
|
||||
|
||||
def __init__ (self, show_instance, show_name, filelength, start_time):
|
||||
class ShowRecorder(Thread):
|
||||
def __init__(self, show_instance, show_name, filelength, start_time):
|
||||
Thread.__init__(self)
|
||||
self.logger = logging.getLogger('recorder')
|
||||
self.logger = logging.getLogger("recorder")
|
||||
self.api_client = api_client(self.logger)
|
||||
self.filelength = filelength
|
||||
self.start_time = start_time
|
||||
|
@ -75,35 +81,41 @@ class ShowRecorder(Thread):
|
|||
if config["pypo"]["record_file_type"] in ["mp3", "ogg"]:
|
||||
filetype = config["pypo"]["record_file_type"]
|
||||
else:
|
||||
filetype = "ogg";
|
||||
filetype = "ogg"
|
||||
|
||||
joined_path = os.path.join(config["pypo"]["base_recorded_files"], filename)
|
||||
filepath = "%s.%s" % (joined_path, filetype)
|
||||
|
||||
br = config["pypo"]["record_bitrate"]
|
||||
sr = config["pypo"]["record_samplerate"]
|
||||
c = config["pypo"]["record_channels"]
|
||||
c = config["pypo"]["record_channels"]
|
||||
ss = config["pypo"]["record_sample_size"]
|
||||
|
||||
#-f:16,2,44100
|
||||
#-b:256
|
||||
command = "ecasound -f:%s,%s,%s -i alsa -o %s,%s000 -t:%s" % \
|
||||
(ss, c, sr, filepath, br, length)
|
||||
# -f:16,2,44100
|
||||
# -b:256
|
||||
command = "ecasound -f:%s,%s,%s -i alsa -o %s,%s000 -t:%s" % (
|
||||
ss,
|
||||
c,
|
||||
sr,
|
||||
filepath,
|
||||
br,
|
||||
length,
|
||||
)
|
||||
args = command.split(" ")
|
||||
|
||||
self.logger.info("starting record")
|
||||
self.logger.info("command " + command)
|
||||
|
||||
self.p = Popen(args,stdout=PIPE,stderr=PIPE)
|
||||
self.p = Popen(args, stdout=PIPE, stderr=PIPE)
|
||||
|
||||
#blocks at the following line until the child process
|
||||
#quits
|
||||
# blocks at the following line until the child process
|
||||
# quits
|
||||
self.p.wait()
|
||||
outmsgs = self.p.stdout.readlines()
|
||||
for msg in outmsgs:
|
||||
m = re.search('^ERROR',msg)
|
||||
m = re.search("^ERROR", msg)
|
||||
if not m == None:
|
||||
self.logger.info('Recording error is found: %s', outmsgs)
|
||||
self.logger.info("Recording error is found: %s", outmsgs)
|
||||
self.logger.info("finishing record, return code %s", self.p.returncode)
|
||||
code = self.p.returncode
|
||||
|
||||
|
@ -112,21 +124,25 @@ class ShowRecorder(Thread):
|
|||
return code, filepath
|
||||
|
||||
def cancel_recording(self):
|
||||
#send signal interrupt (2)
|
||||
# send signal interrupt (2)
|
||||
self.logger.info("Show manually cancelled!")
|
||||
if (self.p is not None):
|
||||
if self.p is not None:
|
||||
self.p.send_signal(signal.SIGINT)
|
||||
|
||||
#if self.p is defined, then the child process ecasound is recording
|
||||
# if self.p is defined, then the child process ecasound is recording
|
||||
def is_recording(self):
|
||||
return (self.p is not None)
|
||||
return self.p is not None
|
||||
|
||||
def upload_file(self, filepath):
|
||||
|
||||
filename = os.path.split(filepath)[1]
|
||||
|
||||
# files is what requests actually expects
|
||||
files = {'file': open(filepath, "rb"), 'name': filename, 'show_instance': self.show_instance}
|
||||
files = {
|
||||
"file": open(filepath, "rb"),
|
||||
"name": filename,
|
||||
"show_instance": self.show_instance,
|
||||
}
|
||||
|
||||
self.api_client.upload_recorded_show(files, self.show_instance)
|
||||
|
||||
|
@ -136,27 +152,25 @@ class ShowRecorder(Thread):
|
|||
self.start_time, self.show_name, self.show_instance
|
||||
"""
|
||||
try:
|
||||
full_date, full_time = self.start_time.split(" ",1)
|
||||
full_date, full_time = self.start_time.split(" ", 1)
|
||||
# No idea why we translated - to : before
|
||||
#full_time = full_time.replace(":","-")
|
||||
# full_time = full_time.replace(":","-")
|
||||
self.logger.info("time: %s" % full_time)
|
||||
artist = "Airtime Show Recorder"
|
||||
#set some metadata for our file daemon
|
||||
recorded_file = mutagen.File(filepath, easy = True)
|
||||
recorded_file['artist'] = artist
|
||||
recorded_file['date'] = full_date
|
||||
recorded_file['title'] = "%s-%s-%s" % (self.show_name,
|
||||
full_date, full_time)
|
||||
#You cannot pass ints into the metadata of a file. Even tracknumber needs to be a string
|
||||
recorded_file['tracknumber'] = self.show_instance
|
||||
# set some metadata for our file daemon
|
||||
recorded_file = mutagen.File(filepath, easy=True)
|
||||
recorded_file["artist"] = artist
|
||||
recorded_file["date"] = full_date
|
||||
recorded_file["title"] = "%s-%s-%s" % (self.show_name, full_date, full_time)
|
||||
# You cannot pass ints into the metadata of a file. Even tracknumber needs to be a string
|
||||
recorded_file["tracknumber"] = self.show_instance
|
||||
recorded_file.save()
|
||||
|
||||
except Exception as e:
|
||||
top = traceback.format_exc()
|
||||
self.logger.error('Exception: %s', e)
|
||||
self.logger.error("Exception: %s", e)
|
||||
self.logger.error("traceback: %s", top)
|
||||
|
||||
|
||||
def run(self):
|
||||
code, filepath = self.record_show()
|
||||
|
||||
|
@ -174,14 +188,15 @@ class ShowRecorder(Thread):
|
|||
self.logger.info("problem recording show")
|
||||
os.remove(filepath)
|
||||
|
||||
|
||||
class Recorder(Thread):
|
||||
def __init__(self, q):
|
||||
Thread.__init__(self)
|
||||
self.logger = logging.getLogger('recorder')
|
||||
self.logger = logging.getLogger("recorder")
|
||||
self.api_client = api_client(self.logger)
|
||||
self.sr = None
|
||||
self.shows_to_record = {}
|
||||
self.server_timezone = ''
|
||||
self.server_timezone = ""
|
||||
self.queue = q
|
||||
self.loops = 0
|
||||
self.logger.info("RecorderFetch: init complete")
|
||||
|
@ -189,7 +204,7 @@ class Recorder(Thread):
|
|||
success = False
|
||||
while not success:
|
||||
try:
|
||||
self.api_client.register_component('show-recorder')
|
||||
self.api_client.register_component("show-recorder")
|
||||
success = True
|
||||
except Exception as e:
|
||||
self.logger.error(str(e))
|
||||
|
@ -205,7 +220,7 @@ class Recorder(Thread):
|
|||
msg = json.loads(message)
|
||||
command = msg["event_type"]
|
||||
self.logger.info("Received msg from Pypo Message Handler: %s", msg)
|
||||
if command == 'cancel_recording':
|
||||
if command == "cancel_recording":
|
||||
if self.currently_recording():
|
||||
self.cancel_recording()
|
||||
else:
|
||||
|
@ -218,14 +233,18 @@ class Recorder(Thread):
|
|||
def process_recorder_schedule(self, m):
|
||||
self.logger.info("Parsing recording show schedules...")
|
||||
temp_shows_to_record = {}
|
||||
shows = m['shows']
|
||||
shows = m["shows"]
|
||||
for show in shows:
|
||||
show_starts = getDateTimeObj(show['starts'])
|
||||
show_end = getDateTimeObj(show['ends'])
|
||||
show_starts = getDateTimeObj(show["starts"])
|
||||
show_end = getDateTimeObj(show["ends"])
|
||||
time_delta = show_end - show_starts
|
||||
|
||||
temp_shows_to_record[show['starts']] = [time_delta,
|
||||
show['instance_id'], show['name'], m['server_timezone']]
|
||||
temp_shows_to_record[show["starts"]] = [
|
||||
time_delta,
|
||||
show["instance_id"],
|
||||
show["name"],
|
||||
m["server_timezone"],
|
||||
]
|
||||
self.shows_to_record = temp_shows_to_record
|
||||
|
||||
def get_time_till_next_show(self):
|
||||
|
@ -237,7 +256,7 @@ class Recorder(Thread):
|
|||
next_show = getDateTimeObj(start_time)
|
||||
|
||||
delta = next_show - tnow
|
||||
s = '%s.%s' % (delta.seconds, delta.microseconds)
|
||||
s = "%s.%s" % (delta.seconds, delta.microseconds)
|
||||
out = float(s)
|
||||
|
||||
if out < 5:
|
||||
|
@ -257,7 +276,8 @@ class Recorder(Thread):
|
|||
return False
|
||||
|
||||
def start_record(self):
|
||||
if len(self.shows_to_record) == 0: return None
|
||||
if len(self.shows_to_record) == 0:
|
||||
return None
|
||||
try:
|
||||
delta = self.get_time_till_next_show()
|
||||
if delta < 5:
|
||||
|
@ -273,16 +293,25 @@ class Recorder(Thread):
|
|||
|
||||
T = pytz.timezone(server_timezone)
|
||||
start_time_on_UTC = getDateTimeObj(start_time)
|
||||
start_time_on_server = start_time_on_UTC.replace(tzinfo=pytz.utc).astimezone(T)
|
||||
start_time_formatted = '%(year)d-%(month)02d-%(day)02d %(hour)02d:%(min)02d:%(sec)02d' % \
|
||||
{'year': start_time_on_server.year, 'month': start_time_on_server.month, 'day': start_time_on_server.day, \
|
||||
'hour': start_time_on_server.hour, 'min': start_time_on_server.minute, 'sec': start_time_on_server.second}
|
||||
|
||||
start_time_on_server = start_time_on_UTC.replace(
|
||||
tzinfo=pytz.utc
|
||||
).astimezone(T)
|
||||
start_time_formatted = (
|
||||
"%(year)d-%(month)02d-%(day)02d %(hour)02d:%(min)02d:%(sec)02d"
|
||||
% {
|
||||
"year": start_time_on_server.year,
|
||||
"month": start_time_on_server.month,
|
||||
"day": start_time_on_server.day,
|
||||
"hour": start_time_on_server.hour,
|
||||
"min": start_time_on_server.minute,
|
||||
"sec": start_time_on_server.second,
|
||||
}
|
||||
)
|
||||
|
||||
seconds_waiting = 0
|
||||
|
||||
#avoiding CC-5299
|
||||
while(True):
|
||||
# avoiding CC-5299
|
||||
while True:
|
||||
if self.currently_recording():
|
||||
self.logger.info("Previous record not finished, sleeping 100ms")
|
||||
seconds_waiting = seconds_waiting + 0.1
|
||||
|
@ -290,16 +319,21 @@ class Recorder(Thread):
|
|||
else:
|
||||
show_length_seconds = show_length.seconds - seconds_waiting
|
||||
|
||||
self.sr = ShowRecorder(show_instance, show_name, show_length_seconds, start_time_formatted)
|
||||
self.sr = ShowRecorder(
|
||||
show_instance,
|
||||
show_name,
|
||||
show_length_seconds,
|
||||
start_time_formatted,
|
||||
)
|
||||
self.sr.start()
|
||||
break
|
||||
|
||||
#remove show from shows to record.
|
||||
# remove show from shows to record.
|
||||
del self.shows_to_record[start_time]
|
||||
#self.time_till_next_show = self.get_time_till_next_show()
|
||||
except Exception as e :
|
||||
# self.time_till_next_show = self.get_time_till_next_show()
|
||||
except Exception as e:
|
||||
top = traceback.format_exc()
|
||||
self.logger.error('Exception: %s', e)
|
||||
self.logger.error("Exception: %s", e)
|
||||
self.logger.error("traceback: %s", top)
|
||||
|
||||
def run(self):
|
||||
|
@ -318,7 +352,7 @@ class Recorder(Thread):
|
|||
self.process_recorder_schedule(temp)
|
||||
self.logger.info("Bootstrap recorder schedule received: %s", temp)
|
||||
except Exception as e:
|
||||
self.logger.error( traceback.format_exc() )
|
||||
self.logger.error(traceback.format_exc())
|
||||
self.logger.error(e)
|
||||
|
||||
self.logger.info("Bootstrap complete: got initial copy of the schedule")
|
||||
|
@ -338,16 +372,16 @@ class Recorder(Thread):
|
|||
self.process_recorder_schedule(temp)
|
||||
self.logger.info("updated recorder schedule received: %s", temp)
|
||||
except Exception as e:
|
||||
self.logger.error( traceback.format_exc() )
|
||||
self.logger.error(traceback.format_exc())
|
||||
self.logger.error(e)
|
||||
try: self.handle_message()
|
||||
try:
|
||||
self.handle_message()
|
||||
except Exception as e:
|
||||
self.logger.error( traceback.format_exc() )
|
||||
self.logger.error('Pypo Recorder Exception: %s', e)
|
||||
self.logger.error(traceback.format_exc())
|
||||
self.logger.error("Pypo Recorder Exception: %s", e)
|
||||
time.sleep(PUSH_INTERVAL)
|
||||
self.loops += 1
|
||||
except Exception as e :
|
||||
except Exception as e:
|
||||
top = traceback.format_exc()
|
||||
self.logger.error('Exception: %s', e)
|
||||
self.logger.error("Exception: %s", e)
|
||||
self.logger.error("traceback: %s", top)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue