From e0e4d4c87fe4b03978b02b2039b30fe4b1670025 Mon Sep 17 00:00:00 2001 From: Kyle Robbertze Date: Mon, 4 May 2020 13:24:57 +0200 Subject: [PATCH] Ensure all json loads calls use strings --- python_apps/airtime-celery/airtime-celery/tasks.py | 12 ++++++++++-- .../airtime_analyzer/cuepoint_analyzer.py | 4 ++++ .../airtime_analyzer/message_listener.py | 4 ++++ python_apps/api_clients/api_clients/api_client.py | 6 +++++- python_apps/pypo/pypo/pypofetch.py | 4 ++++ python_apps/pypo/pypo/pypomessagehandler.py | 4 ++++ python_apps/pypo/pypo/recorder.py | 6 +++++- 7 files changed, 36 insertions(+), 4 deletions(-) diff --git a/python_apps/airtime-celery/airtime-celery/tasks.py b/python_apps/airtime-celery/airtime-celery/tasks.py index 2ef843b89..097d072ec 100644 --- a/python_apps/airtime-celery/airtime-celery/tasks.py +++ b/python_apps/airtime-celery/airtime-celery/tasks.py @@ -86,8 +86,12 @@ def soundcloud_download(token, callback_url, api_key, track_id): auth=requests.auth.HTTPBasicAuth(api_key, ""), ) re.raise_for_status() + try: + response = re.content.decode() + except (UnicodeDecodeError, AttributeError): + response = re.content f = json.loads( - re.content + response ) # Read the response from the media API to get the file id obj["fileid"] = f["id"] else: @@ -203,8 +207,12 @@ def podcast_download( auth=requests.auth.HTTPBasicAuth(api_key, ""), ) re.raise_for_status() + try: + response = re.content.decode() + except (UnicodeDecodeError, AttributeError): + response = re.content f = json.loads( - re.content + response ) # Read the response from the media API to get the file id obj["fileid"] = f["id"] obj["status"] = 1 diff --git a/python_apps/airtime_analyzer/airtime_analyzer/cuepoint_analyzer.py b/python_apps/airtime_analyzer/airtime_analyzer/cuepoint_analyzer.py index 49e90bce6..739df2478 100644 --- a/python_apps/airtime_analyzer/airtime_analyzer/cuepoint_analyzer.py +++ b/python_apps/airtime_analyzer/airtime_analyzer/cuepoint_analyzer.py @@ -27,6 +27,10 @@ class CuePointAnalyzer(Analyzer): command = [CuePointAnalyzer.SILAN_EXECUTABLE, '-b', '-F', '0.99', '-f', 'JSON', '-t', '1.0', filename] try: results_json = subprocess.check_output(command, stderr=subprocess.STDOUT, close_fds=True) + try: + results_json = results_json.decode() + except (UnicodeDecodeError, AttributeError): + pass silan_results = json.loads(results_json) # Defensive coding against Silan wildly miscalculating the cue in and out times: diff --git a/python_apps/airtime_analyzer/airtime_analyzer/message_listener.py b/python_apps/airtime_analyzer/airtime_analyzer/message_listener.py index 120fc3585..5d28cc3f2 100644 --- a/python_apps/airtime_analyzer/airtime_analyzer/message_listener.py +++ b/python_apps/airtime_analyzer/airtime_analyzer/message_listener.py @@ -157,6 +157,10 @@ class MessageListener: We avoid cascading failure this way. ''' try: + try: + body = body.decode() + except (UnicodeDecodeError, AttributeError): + pass msg_dict = json.loads(body) api_key = msg_dict["api_key"] callback_url = msg_dict["callback_url"] diff --git a/python_apps/api_clients/api_clients/api_client.py b/python_apps/api_clients/api_clients/api_client.py index fb9cad33e..f89a1a9b3 100644 --- a/python_apps/api_clients/api_clients/api_client.py +++ b/python_apps/api_clients/api_clients/api_client.py @@ -137,7 +137,11 @@ class ApiRequest(object): try: if content_type == 'application/json': - data = json.loads(response) + try: + response = response.decode() + except (UnicodeDecodeError, AttributeError): + pass + data = json.loads(response.decode) return data else: raise InvalidContentType() diff --git a/python_apps/pypo/pypo/pypofetch.py b/python_apps/pypo/pypo/pypofetch.py index 4824cdab7..b6f53b15a 100644 --- a/python_apps/pypo/pypo/pypofetch.py +++ b/python_apps/pypo/pypo/pypofetch.py @@ -78,6 +78,10 @@ class PypoFetch(Thread): try: self.logger.info("Received event from Pypo Message Handler: %s" % message) + try: + message = message.decode() + except (UnicodeDecodeError, AttributeError): + pass m = json.loads(message) command = m['event_type'] self.logger.info("Handling command: " + command) diff --git a/python_apps/pypo/pypo/pypomessagehandler.py b/python_apps/pypo/pypo/pypomessagehandler.py index f5d1a9b51..3d4b37589 100644 --- a/python_apps/pypo/pypo/pypomessagehandler.py +++ b/python_apps/pypo/pypo/pypomessagehandler.py @@ -64,6 +64,10 @@ class PypoMessageHandler(Thread): try: self.logger.info("Received event from RabbitMQ: %s" % message) + try: + message = message.decode() + except (UnicodeDecodeError, AttributeError): + pass m = json.loads(message) command = m['event_type'] self.logger.info("Handling command: " + command) diff --git a/python_apps/pypo/pypo/recorder.py b/python_apps/pypo/pypo/recorder.py index ce2d3a4e0..d5ba3cec7 100644 --- a/python_apps/pypo/pypo/recorder.py +++ b/python_apps/pypo/pypo/recorder.py @@ -197,7 +197,11 @@ class Recorder(Thread): def handle_message(self): if not self.queue.empty(): message = self.queue.get() - msg = json.loads(message) + try: + message = message.decode() + except (UnicodeDecodeError, AttributeError): + pass + msg = json.loads(message) command = msg["event_type"] self.logger.info("Received msg from Pypo Message Handler: %s", msg) if command == 'cancel_recording':