should solve issue where a list of dicts is returned by sonarr/radarr instead of a singular dict

pull/8/head
l3uddz 7 years ago
parent cd17eb5314
commit b2294c6b97

@ -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)

@ -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)

@ -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

Loading…
Cancel
Save