Skip to content

Commit

Permalink
move all url error checking to api_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
pkscout committed Oct 1, 2020
1 parent c030960 commit 55709e6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 41 deletions.
19 changes: 11 additions & 8 deletions libs/api_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
pass

HEADERS = (
('User-Agent', 'Kodi scraper for themoviedb.org by pkscout; [email protected]'),
('User-Agent', 'Kodi TV Show scraper by Team Kodi/; contact [email protected]'),
('Accept', 'application/json'),
)
SESSION = requests.Session()
Expand All @@ -43,20 +43,23 @@ def set_headers(headers):
SESSION.headers.update(headers)


def load_info(url, params=None):
def load_info(url, params=None, default=None):
# type: (Text, Optional[Dict[Text, Union[Text, List[Text]]]]) -> Union[dict, list]
"""
Load info from themoviedb
Load info from external api
:param url: API endpoint URL
:param params: URL query params
:return: API response
:raises requests.exceptions.HTTPError: if any error happens
:return: API response or default on error
"""
logger.debug('Calling URL "{}" with params {}'.format(url, params))
response = SESSION.get(url, params=params)
if not response.ok:
response.raise_for_status()
try:
response = SESSION.get(url, params=params)
except HTTPError as exc:
logger.error('themoviedb returned an error: {}'.format(exc))
response = None
if response is None:
return default
json_response = response.json()
if settings.VERBOSELOG:
logger.debug('the api response:\n{}'.format(pformat(json_response)))
Expand Down
37 changes: 10 additions & 27 deletions libs/tmdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

from __future__ import absolute_import, unicode_literals

from requests.exceptions import HTTPError
from math import floor
from pprint import pformat
from . import cache, data_utils, api_utils, settings, imdbratings, traktratings
Expand Down Expand Up @@ -69,8 +68,8 @@ def search_show(title, year=None):
params = {'query': title}
if year:
params.update({'first_air_date_year': str(year)})
try:
resp = api_utils.load_info(search_url, params)
resp = api_utils.load_info(search_url, params)
if resp is not None:
if ext_media_id:
if ext_media_id['type'] == 'tmdb_id':
if resp.get('success') == 'false':
Expand All @@ -81,8 +80,6 @@ def search_show(title, year=None):
results = resp.get('tv_results', [])
else:
results = resp.get('results', [])
except HTTPError as exc:
logger.error('themoviedb returned an error: {}'.format(exc))
return results


Expand All @@ -93,11 +90,7 @@ def load_episode_list(show_info, season_map, ep_grouping):
if ep_grouping is not None:
logger.debug('Getting episodes with episode grouping of ' + ep_grouping)
episode_group_url = EPISODE_GROUP_URL.format(ep_grouping)
try:
custom_order = api_utils.load_info(episode_group_url)
except HTTPError as exc:
logger.error('themoviedb returned an error: {}'.format(exc))
custom_order = None
custom_order = api_utils.load_info(episode_group_url)
if custom_order is not None:
show_info['seasons'] = []
season_num = 1
Expand Down Expand Up @@ -147,22 +140,16 @@ def load_show_info(show_id, ep_grouping=None):
params = {}
params['append_to_response'] = 'credits,content_ratings,external_ids,images'
params['include_image_language'] = '%s,en,null' % settings.LANG[0:2]
try:
show_info = api_utils.load_info(show_url, params)
except HTTPError as exc:
logger.error('themoviedb returned an error: {}'.format(exc))
show_info = api_utils.load_info(show_url, params)
if show_info is None:
return None
season_map = {}
for season in show_info.get('seasons', []):
season_url = SEASON_URL.format(show_id, season['season_number'])
params = {}
params['append_to_response'] = 'credits,images'
params['include_image_language'] = '%s,en,null' % settings.LANG[0:2]
try:
season_info = api_utils.load_info(season_url, params)
except HTTPError as exc:
logger.error('themoviedb returned an error: {}'.format(exc))
season_info = {}
season_info = api_utils.load_info(season_url, params, default={})
season_info['images'] = _sort_image_types(season_info.get('images', {}))
season_map[str(season['season_number'])] = season_info
show_info = load_episode_list(show_info, season_map, ep_grouping)
Expand Down Expand Up @@ -207,10 +194,8 @@ def load_episode_info(show_id, episode_id):
params = {}
params['append_to_response'] = 'credits,external_ids,images'
params['include_image_language'] = '%s,en,null' % settings.LANG[0:2]
try:
ep_return = api_utils.load_info(ep_url, params)
except HTTPError as exc:
logger.error('themoviedb returned an error: {}'.format(exc))
ep_return = api_utils.load_info(ep_url, params)
if ep_return is None:
return None
ep_return['images'] = _sort_image_types(ep_return.get('images', {}))
ep_return['season_number'] = episode_info['season_number']
Expand Down Expand Up @@ -265,10 +250,8 @@ def load_fanarttv_art(show_info):
break
if tvdb_id and artwork_enabled:
fanarttv_url = FANARTTV_URL.format(tvdb_id)
try:
artwork = api_utils.load_info(fanarttv_url)
except HTTPError as exc:
logger.error('fanart.tv returned an error: {}'.format(exc))
artwork = api_utils.load_info(fanarttv_url)
if artwork is None:
return show_info
for fanarttv_type, tmdb_type in settings.FANARTTV_MAPPING.items():
if settings.FANARTTV_ART[tmdb_type]:
Expand Down
7 changes: 1 addition & 6 deletions libs/traktratings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

from __future__ import absolute_import, unicode_literals

from requests.exceptions import HTTPError
from . import api_utils, settings
from .utils import logger
try:
Expand All @@ -46,11 +45,7 @@ def get_details(imdb_id, season=None, episode=None):
url = EP_URL.format(imdb_id, season, episode)
else:
url = SHOW_URL.format(imdb_id)
try:
resp = api_utils.load_info(url)
except HTTPError as exc:
logger.error('trakt returned an error: {}'.format(exc))
resp = {}
resp = api_utils.load_info(url, default={})
rating =resp.get('rating')
votes = resp.get('votes')
if votes and rating:
Expand Down

0 comments on commit 55709e6

Please sign in to comment.