1
0
mirror of https://github.com/moparisthebest/SickRage synced 2024-11-11 03:45:01 -05:00
SickRage/lib/trakt/__init__.py
2014-11-20 04:40:33 -08:00

24 lines
771 B
Python

import hashlib
import requests
def TraktCall(method, api, username=None, password=None, data={}):
base_url = 'http://api.trakt.tv/'
# if the API isn't given then it failed
if not api:
return None
# if username and password given then encode password with sha1
auth = None
if username and password:
auth = (username, hashlib.sha1(password.encode('utf-8')).hexdigest())
# request the URL from trakt and parse the result as json
try:
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:
return None
return resp