Skip to content

Commit

Permalink
change imdb calls to use api_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
pkscout committed Oct 1, 2020
1 parent 55709e6 commit 4f08246
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 30 deletions.
22 changes: 11 additions & 11 deletions libs/api_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,36 @@
except ImportError:
pass

HEADERS = (
('User-Agent', 'Kodi TV Show scraper by Team Kodi/; contact [email protected]'),
('Accept', 'application/json'),
)
SESSION = requests.Session()
SESSION.headers.update(dict(HEADERS))


def set_headers(headers):
SESSION.headers.update(headers)


def load_info(url, params=None, default=None):
def load_info(url, params=None, default=None, resp_type = 'json'):
# type: (Text, Optional[Dict[Text, Union[Text, List[Text]]]]) -> Union[dict, list]
"""
Load info from external api
:param url: API endpoint URL
:param params: URL query params
:default: object to return if there is an error
:resp_type: what to return to the calling function
:return: API response or default on error
"""
logger.debug('Calling URL "{}" with params {}'.format(url, params))
try:
response = SESSION.get(url, params=params)
except HTTPError as exc:
logger.error('themoviedb returned an error: {}'.format(exc))
logger.error('the site returned an error: {}'.format(exc))
response = None
if response is None:
return default
json_response = response.json()
resp = default
elif resp_type.lower() == 'json':
resp = response.json()
else:
resp = response.text
if settings.VERBOSELOG:
logger.debug('the api response:\n{}'.format(pformat(json_response)))
return json_response
logger.debug('the api response:\n{}'.format(pformat(resp)))
return resp
19 changes: 4 additions & 15 deletions libs/imdbratings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,22 @@


import re
import requests
from requests.exceptions import ConnectionError as RequestsConnectionError, Timeout, RequestException
from . import api_utils

IMDB_RATINGS_URL = 'https://www.imdb.com/title/{}/'

IMDB_RATING_REGEX = re.compile(r'itemprop="ratingValue".*?>.*?([\d.]+).*?<')
IMDB_VOTES_REGEX = re.compile(r'itemprop="ratingCount".*?>.*?([\d,]+).*?<')

# get the tv show info via imdb

def get_details(imdb_id):
if not imdb_id:
return {}
votes, rating = _get_ratinginfo(imdb_id)
return _assemble_imdb_result(votes, rating)

def _get_ratinginfo(imdb_id):
try:
response = requests.get(IMDB_RATINGS_URL.format(imdb_id))
except (Timeout, RequestsConnectionError, RequestException) as ex:
return _format_error_message(ex)
return _parse_imdb_result(response.text if response and response.status_code == 200 else '')
response = api_utils.load_info(IMDB_RATINGS_URL.format(imdb_id), default = '', resp_type='text')
return _parse_imdb_result(response)

def _assemble_imdb_result(votes, rating):
result = {}
Expand All @@ -64,9 +59,3 @@ def _parse_imdb_votes(input_html):
if (match):
return int(match.group(1).replace(',', ''))
return None

def _format_error_message(ex):
message = type(ex).__name__
if hasattr(ex, 'message'):
message += ": {0}".format(ex.message)
return {'error': message}
6 changes: 6 additions & 0 deletions libs/tmdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
except ImportError:
pass

HEADERS = (
('User-Agent', 'Kodi TV Show scraper by Team Kodi; contact [email protected]'),
('Accept', 'application/json'),
)
api_utils.set_headers(HEADERS)

BASE_URL = 'https://api.themoviedb.org/3/{}?api_key=%s&language=%s' % (settings.TMDB_CLOWNCAR, settings.LANG)
EPISODE_GROUP_URL = BASE_URL.format('tv/episode_group/{}')
SEARCH_URL = BASE_URL.format('search/tv')
Expand Down
12 changes: 8 additions & 4 deletions libs/traktratings.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,19 @@
except ImportError:
pass

BASE_URL = 'https://api.trakt.tv/shows/{}'
SHOW_URL = BASE_URL + '?extended=full'
EP_URL = BASE_URL + '/seasons/{}/episodes/{}/ratings'

HEADERS = (
('User-Agent', 'Kodi TV Show scraper by Team Kodi; contact [email protected]'),
('Accept', 'application/json'),
('trakt-api-key', settings.TRAKT_CLOWNCAR),
('trakt-api-version', '2'),
('Content-Type', 'application/json'),
)
api_utils.set_headers(dict(HEADERS))
api_utils.set_headers(HEADERS)

BASE_URL = 'https://api.trakt.tv/shows/{}'
SHOW_URL = BASE_URL + '?extended=full'
EP_URL = BASE_URL + '/seasons/{}/episodes/{}/ratings'


def get_details(imdb_id, season=None, episode=None):
Expand Down

0 comments on commit 4f08246

Please sign in to comment.