Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions python/lib/tmdbscraper/api_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from __future__ import absolute_import, unicode_literals

import json
import socket

try:
import xbmc
Expand All @@ -43,6 +44,7 @@
pass

HEADERS = {}
TIMEOUT = 15


def set_headers(headers):
Expand Down Expand Up @@ -70,7 +72,8 @@ def load_info(url, params=None, default=None, resp_type = 'json'):
xbmc.log(str(HEADERS), xbmc.LOGDEBUG)
req = Request(url, headers=HEADERS)
try:
response = urlopen(req)
response = urlopen(req, timeout=TIMEOUT)
body = response.read().decode('utf-8')
except URLError as e:
if hasattr(e, 'reason'):
theerror = {'error': 'failed to reach the remote site\nReason: {}'.format(e.reason)}
Expand All @@ -80,9 +83,15 @@ def load_info(url, params=None, default=None, resp_type = 'json'):
return default
else:
return theerror
except (socket.timeout, socket.error) as e:
theerror = {'error': 'failed to read from the remote site\nReason: {}'.format(e)}
if default is not None:
return default
else:
return theerror
if resp_type.lower() == 'json':
resp = json.loads(response.read().decode('utf-8'))
resp = json.loads(body)
else:
resp = response.read().decode('utf-8')
resp = body
# xbmc.log('the api response:\n{}'.format(pformat(resp)), xbmc.LOGDEBUG)
return resp
Loading