From c0c3c9dfafd55a9167b4391cfc2b8820f24d4b26 Mon Sep 17 00:00:00 2001 From: l3uddz Date: Wed, 18 Apr 2018 14:29:14 +0100 Subject: [PATCH] should solve issue where a list of dicts is returned by sonarr/radarr instead of a singular dict --- media/radarr.py | 13 +++++++++---- media/sonarr.py | 15 ++++++++++----- misc/helpers.py | 17 ++++++++++++++++- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/media/radarr.py b/media/radarr.py index 3f517cc..298b758 100644 --- a/media/radarr.py +++ b/media/radarr.py @@ -4,6 +4,7 @@ import backoff import requests from misc.log import logger +from misc import helpers log = logger.get_logger(__name__) @@ -96,13 +97,17 @@ class Radarr: log.debug("Request Response Code: %d", req.status_code) log.debug("Request Response Text:\n%s", req.text) - if (req.status_code == 201 or req.status_code == 200) and 'json' in req.headers['Content-Type'].lower() \ - and req.json()['tmdbId'] == movie_tmdbid: + response_json = None + if 'json' in req.headers['Content-Type'].lower(): + response_json = helpers.get_response_dict(req.json()) + + if (req.status_code == 201 or req.status_code == 200) and (response_json and 'tmdbId' in response_json) \ + and response_json['tmdbId'] == movie_tmdbid: log.debug("Successfully added %s (%d)", movie_title, movie_tmdbid) return True - elif 'json' in req.headers['Content-Type'].lower() and 'message' in req.text: + elif response_json and 'message' in response_json: log.error("Failed to add %s (%d) - status_code: %d, reason: %s", movie_title, movie_tmdbid, - req.status_code, req.json()['message']) + req.status_code, response_json['message']) return False else: log.error("Failed to add %s (%d), unexpected response:\n%s", movie_title, movie_tmdbid, req.text) diff --git a/media/sonarr.py b/media/sonarr.py index 1d10f16..bd5ce7f 100644 --- a/media/sonarr.py +++ b/media/sonarr.py @@ -3,6 +3,7 @@ from urllib.parse import urljoin import backoff import requests +from misc import helpers from misc.log import logger log = logger.get_logger(__name__) @@ -141,14 +142,18 @@ class Sonarr: log.debug("Request Payload: %s", payload) log.debug("Request Response Code: %d", req.status_code) log.debug("Request Response Text:\n%s", req.text) - - if (req.status_code == 201 or req.status_code == 200) and 'json' in req.headers['Content-Type'].lower() \ - and req.json()['tvdbId'] == series_tvdbid: + + response_json = None + if 'json' in req.headers['Content-Type'].lower(): + response_json = helpers.get_response_dict(req.json()) + + if (req.status_code == 201 or req.status_code == 200) and (response_json and 'tvdbId' in response_json) \ + and response_json['tvdbId'] == series_tvdbid: log.debug("Successfully added %s (%d)", series_title, series_tvdbid) return True - elif 'json' in req.headers['Content-Type'].lower() and 'errorMessage' in req.text: + elif response_json and 'errorMessage' in response_json: log.error("Failed to add %s (%d) - status_code: %d, reason: %s", series_title, series_tvdbid, - req.status_code, req.json()['errorMessage']) + req.status_code, response_json['errorMessage']) return False else: log.error("Failed to add %s (%d), unexpected response:\n%s", series_title, series_tvdbid, req.text) diff --git a/misc/helpers.py b/misc/helpers.py index c5c9f6f..5cca113 100644 --- a/misc/helpers.py +++ b/misc/helpers.py @@ -27,7 +27,7 @@ def sonarr_readable_tag_from_ids(profile_tag_ids, chosen_tag_ids): try: if not chosen_tag_ids: return None - + tags = [] for tag_name, tag_id in profile_tag_ids.items(): if tag_id in chosen_tag_ids: @@ -380,3 +380,18 @@ def trakt_is_movie_blacklisted(movie, blacklist_settings): except Exception: log.exception("Exception determining if movie was blacklisted %s: ", movie) return blacklisted + + +def get_response_dict(response): + found_response = None + try: + if isinstance(response, list): + found_response = response[0] + elif isinstance(response, dict): + found_response = response + else: + log.error("Unexpected response instance type of %s for %s", type(response).__name__, response) + + except Exception: + log.exception("Exception determining response for %s: ", response) + return found_response