-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move all url error checking to api_utils
- Loading branch information
Showing
3 changed files
with
22 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
|
@@ -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))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters