2014-11-28 17:07:26 -05:00
|
|
|
import requests
|
2015-01-25 15:14:57 -05:00
|
|
|
import json
|
|
|
|
from sickbeard import logger
|
2014-11-28 17:07:26 -05:00
|
|
|
|
2014-11-28 17:13:06 -05:00
|
|
|
from exceptions import traktException, traktAuthException, traktServerBusy
|
2014-11-28 17:07:26 -05:00
|
|
|
|
|
|
|
class TraktAPI():
|
2015-01-27 14:26:53 -05:00
|
|
|
def __init__(self, apikey, username=None, password=None, disable_ssl_verify=False, timeout=30):
|
2014-11-28 17:07:26 -05:00
|
|
|
self.username = username
|
|
|
|
self.password = password
|
2015-01-27 14:26:53 -05:00
|
|
|
self.verify = not disable_ssl_verify
|
2014-11-28 17:07:26 -05:00
|
|
|
self.timeout = timeout
|
2015-01-25 15:14:57 -05:00
|
|
|
self.api_url = 'https://api.trakt.tv/'
|
|
|
|
self.headers = {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'trakt-api-version': '2',
|
|
|
|
'trakt-api-key': apikey,
|
|
|
|
}
|
2014-11-28 17:07:26 -05:00
|
|
|
|
|
|
|
def validateAccount(self):
|
2015-01-25 15:14:57 -05:00
|
|
|
if hasattr(self, 'token'):
|
|
|
|
del(self.token)
|
|
|
|
data = {
|
|
|
|
'login': self.username,
|
|
|
|
'password': self.password
|
|
|
|
}
|
|
|
|
try:
|
2015-01-27 14:26:53 -05:00
|
|
|
resp = requests.request('POST', self.api_url+"auth/login", headers=self.headers,
|
|
|
|
data=json.dumps(data), timeout=self.timeout, verify=self.verify)
|
2015-01-25 15:14:57 -05:00
|
|
|
resp.raise_for_status()
|
|
|
|
resp = resp.json()
|
|
|
|
except (requests.HTTPError, requests.ConnectionError) as e:
|
2015-01-27 09:41:02 -05:00
|
|
|
code = getattr(e.response, 'status_code', None)
|
|
|
|
if not code:
|
|
|
|
# This is pretty much a fatal error if there is no status_code
|
|
|
|
# It means there basically was no response at all
|
|
|
|
raise traktException(e)
|
|
|
|
elif code == 502:
|
2015-01-25 21:33:57 -05:00
|
|
|
# Retry the request, cloudflare had a proxying issue
|
|
|
|
logger.log(u"Retrying trakt api request: auth/login", logger.WARNING)
|
|
|
|
return self.validateAccount()
|
2015-01-27 09:41:02 -05:00
|
|
|
elif code == 401:
|
2015-01-25 15:14:57 -05:00
|
|
|
raise traktAuthException(e)
|
2015-01-27 09:41:02 -05:00
|
|
|
elif code == 503:
|
|
|
|
raise traktServerBusy(e)
|
|
|
|
else:
|
|
|
|
raise traktException(e)
|
2015-01-25 15:14:57 -05:00
|
|
|
if 'token' in resp:
|
|
|
|
self.token = resp['token']
|
|
|
|
return True
|
|
|
|
return False
|
2014-11-28 17:07:26 -05:00
|
|
|
|
2015-01-25 15:14:57 -05:00
|
|
|
def traktRequest(self, path, data=None, method='GET'):
|
|
|
|
url = self.api_url + path
|
|
|
|
headers = self.headers
|
|
|
|
if not getattr(self, 'token', None):
|
|
|
|
self.validateAccount()
|
|
|
|
headers['trakt-user-login'] = self.username
|
|
|
|
headers['trakt-user-token'] = self.token
|
2014-11-28 17:07:26 -05:00
|
|
|
|
|
|
|
# request the URL from trakt and parse the result as json
|
|
|
|
try:
|
2015-01-27 14:26:53 -05:00
|
|
|
resp = requests.request(method, url, headers=headers, timeout=self.timeout,
|
|
|
|
data=json.dumps(data) if data else [], verify=self.verify)
|
2014-11-28 17:07:26 -05:00
|
|
|
|
|
|
|
# check for http errors and raise if any are present
|
|
|
|
resp.raise_for_status()
|
|
|
|
|
|
|
|
# convert response to json
|
|
|
|
resp = resp.json()
|
|
|
|
except (requests.HTTPError, requests.ConnectionError) as e:
|
2015-01-27 09:41:02 -05:00
|
|
|
code = getattr(e.response, 'status_code', None)
|
|
|
|
if not code:
|
|
|
|
# This is pretty much a fatal error if there is no status_code
|
|
|
|
# It means there basically was no response at all
|
|
|
|
raise traktException(e)
|
|
|
|
elif code == 502:
|
2015-01-25 15:14:57 -05:00
|
|
|
# Retry the request, cloudflare had a proxying issue
|
|
|
|
logger.log(u"Retrying trakt api request: %s" % path, logger.WARNING)
|
2015-01-25 21:33:57 -05:00
|
|
|
return self.traktRequest(path, data, method)
|
2015-01-27 09:41:02 -05:00
|
|
|
elif code == 401:
|
2014-12-06 01:51:24 -05:00
|
|
|
raise traktAuthException(e)
|
2015-01-27 09:41:02 -05:00
|
|
|
elif code == 503:
|
2014-12-06 01:51:24 -05:00
|
|
|
raise traktServerBusy(e)
|
2014-11-28 17:07:26 -05:00
|
|
|
else:
|
2014-12-06 01:51:24 -05:00
|
|
|
raise traktException(e)
|
2014-11-28 17:07:26 -05:00
|
|
|
|
|
|
|
# check and confirm trakt call did not fail
|
|
|
|
if isinstance(resp, dict) and resp.get('status', False) == 'failure':
|
|
|
|
if 'message' in resp:
|
|
|
|
raise traktException(resp['message'])
|
|
|
|
if 'error' in resp:
|
|
|
|
raise traktException(resp['error'])
|
|
|
|
else:
|
|
|
|
raise traktException('Unknown Error')
|
|
|
|
|
|
|
|
return resp
|