2014-11-20 07:40:33 -05:00
|
|
|
import hashlib
|
|
|
|
import requests
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-07-01 04:49:12 -04:00
|
|
|
def TraktCall(method, api, username=None, password=None, data={}):
|
2014-11-20 07:40:33 -05:00
|
|
|
base_url = 'http://api.trakt.tv/'
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-11-20 07:40:33 -05:00
|
|
|
# if username and password given then encode password with sha1
|
|
|
|
auth = None
|
2014-08-08 19:07:17 -04:00
|
|
|
if username and password:
|
2014-11-20 07:40:33 -05:00
|
|
|
auth = (username, hashlib.sha1(password.encode('utf-8')).hexdigest())
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
# request the URL from trakt and parse the result as json
|
|
|
|
try:
|
2014-11-20 07:40:33 -05:00
|
|
|
resp = requests.get(base_url + method.replace("%API%", api), auth=auth, data=data).json()
|
|
|
|
if isinstance(resp, dict) and resp.get('status', False) == 'failure':
|
|
|
|
raise Exception(resp.get('error', 'Unknown Error'))
|
|
|
|
except:
|
2014-03-10 01:18:05 -04:00
|
|
|
return None
|
|
|
|
|
|
|
|
return resp
|