2014-03-10 01:18:05 -04:00
|
|
|
import urllib2
|
|
|
|
|
|
|
|
from hashlib import sha1
|
|
|
|
|
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except ImportError:
|
|
|
|
from lib import simplejson as json
|
|
|
|
|
2014-07-01 04:49:12 -04:00
|
|
|
def TraktCall(method, api, username=None, password=None, data={}):
|
2014-03-10 01:18:05 -04:00
|
|
|
"""
|
|
|
|
A generic method for communicating with trakt. Uses the method and data provided along
|
|
|
|
with the auth info to send the command.
|
|
|
|
|
|
|
|
method: The URL to use at trakt, relative, no leading slash.
|
|
|
|
api: The API string to provide to trakt
|
|
|
|
username: The username to use when logging in
|
|
|
|
password: The unencrypted password to use when logging in
|
|
|
|
|
|
|
|
Returns: A boolean representing success
|
|
|
|
"""
|
|
|
|
#logger.log("trakt: Call method " + method, logger.DEBUG)
|
|
|
|
|
|
|
|
# if the API isn't given then it failed
|
|
|
|
if not api:
|
|
|
|
return None
|
|
|
|
|
|
|
|
# if the username isn't given then it failed
|
2014-07-01 04:49:12 -04:00
|
|
|
if username and password:
|
|
|
|
password = sha1(password).hexdigest()
|
|
|
|
data["username"] = username
|
|
|
|
data["password"] = password
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
# replace the API string with what we found
|
|
|
|
method = method.replace("%API%", api)
|
|
|
|
|
|
|
|
# take the URL params and make a json object out of them
|
2014-07-01 04:49:12 -04:00
|
|
|
encoded_data = json.dumps(data)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
# request the URL from trakt and parse the result as json
|
|
|
|
try:
|
|
|
|
#logger.log("trakt: Calling method http://api.trakt.tv/" + method + ", with data" + encoded_data, logger.DEBUG)
|
|
|
|
stream = urllib2.urlopen("http://api.trakt.tv/" + method, encoded_data)
|
|
|
|
resp = stream.read()
|
|
|
|
|
|
|
|
resp = json.loads(resp)
|
|
|
|
|
|
|
|
if ("error" in resp):
|
|
|
|
raise Exception(resp["error"])
|
|
|
|
|
|
|
|
except (IOError):
|
|
|
|
#logger.log("trakt: Failed calling method", logger.ERROR)
|
|
|
|
return None
|
|
|
|
|
|
|
|
#logger.log("trakt: Failed calling method", logger.ERROR)
|
|
|
|
return resp
|