1
0
mirror of https://github.com/moparisthebest/SickRage synced 2025-01-07 03:48:02 -05:00

Fixed more issues with TraktAPI code

This commit is contained in:
echel0n 2014-11-29 03:09:49 -08:00
parent 3d454979db
commit 42befb652d
4 changed files with 13 additions and 16 deletions

View File

@ -1,22 +1,19 @@
import hashlib
import requests import requests
from requests.auth import HTTPBasicAuth
from exceptions import traktException, traktAuthException, traktServerBusy from exceptions import traktException, traktAuthException, traktServerBusy
class TraktAPI(): class TraktAPI():
def __init__(self, apikey, username=None, password=None, use_https=False, timeout=5): def __init__(self, apikey, username=None, password=None, use_https=False, timeout=5):
self.apikey = apikey self.apikey = apikey
self.username = username self.username = username
self.password = password self.password = password
if password: hashlib.sha1(password.encode('utf-8')).hexdigest()
self.protocol = 'https://' if use_https else 'http://' self.protocol = 'https://' if use_https else 'http://'
self.timeout = timeout self.timeout = timeout
def validateAccount(self): def validateAccount(self):
url = '/account/test/%APIKEY%' return self.traktRequest("account/test/%APIKEY%")
return self.traktRequest(url)
def traktRequest(self, url, data=None): def traktRequest(self, url, data=None):
base_url = self.protocol + 'api.trakt.tv/%s' % url.replace('%APIKEY%', self.apikey).replace('%USER%', base_url = self.protocol + 'api.trakt.tv/%s' % url.replace('%APIKEY%', self.apikey).replace('%USER%',
@ -25,7 +22,7 @@ class TraktAPI():
# request the URL from trakt and parse the result as json # request the URL from trakt and parse the result as json
try: try:
resp = requests.get(base_url, resp = requests.get(base_url,
auth=(self.username, self.password) if self.username and self.password else None, auth=HTTPBasicAuth(self.username, self.password),
data=data if data else []) data=data if data else [])
# check for http errors and raise if any are present # check for http errors and raise if any are present
@ -34,12 +31,12 @@ class TraktAPI():
# convert response to json # convert response to json
resp = resp.json() resp = resp.json()
except (requests.HTTPError, requests.ConnectionError) as e: except (requests.HTTPError, requests.ConnectionError) as e:
if e.code == 401: if e.response.status_code == 401:
raise traktAuthException(e.message, e.code) raise traktAuthException(e.message)
elif e.code == 503: elif e.response.status_code == 503:
raise traktServerBusy(e.message, e.code) raise traktServerBusy(e.message)
else: else:
raise traktException(e.message, e.code) raise traktException(e.message)
# check and confirm trakt call did not fail # check and confirm trakt call did not fail
if isinstance(resp, dict) and resp.get('status', False) == 'failure': if isinstance(resp, dict) and resp.get('status', False) == 'failure':

View File

@ -47,7 +47,7 @@ class TraktNotifier:
""" """
trakt_id = sickbeard.indexerApi(ep_obj.show.indexer).config['trakt_id'] trakt_id = sickbeard.indexerApi(ep_obj.show.indexer).config['trakt_id']
trakt_api = TraktAPI(sickbeard.TRAKT_API, sickbeard.TRAKT_USERNAME, sickbeard.TRAKT_USERNAME) trakt_api = TraktAPI(sickbeard.TRAKT_API, sickbeard.TRAKT_USERNAME, sickbeard.TRAKT_PASSWORD)
if sickbeard.USE_TRAKT: if sickbeard.USE_TRAKT:
try: try:
@ -121,7 +121,7 @@ class TraktNotifier:
Returns: True if the request succeeded, False otherwise Returns: True if the request succeeded, False otherwise
""" """
trakt_api = TraktAPI(sickbeard.TRAKT_API, sickbeard.TRAKT_USERNAME, sickbeard.TRAKT_USERNAME) trakt_api = TraktAPI(sickbeard.TRAKT_API, sickbeard.TRAKT_USERNAME, sickbeard.TRAKT_PASSWORD)
try: try:
if trakt_api.validateAccount(): if trakt_api.validateAccount():

View File

@ -35,7 +35,7 @@ class TraktChecker():
def __init__(self): def __init__(self):
self.todoWanted = [] self.todoWanted = []
self.trakt_api = TraktAPI(sickbeard.TRAKT_API, sickbeard.TRAKT_USERNAME, sickbeard.TRAKT_USERNAME) self.trakt_api = TraktAPI(sickbeard.TRAKT_API, sickbeard.TRAKT_USERNAME, sickbeard.TRAKT_PASSWORD)
def run(self, force=False): def run(self, force=False):
try: try:

View File

@ -2973,7 +2973,7 @@ class NewHomeAddShows(MainHandler):
logger.log(u"Getting recommended shows from Trakt.tv", logger.DEBUG) logger.log(u"Getting recommended shows from Trakt.tv", logger.DEBUG)
trakt_api = TraktAPI(sickbeard.TRAKT_API, sickbeard.TRAKT_USERNAME, sickbeard.TRAKT_USERNAME) trakt_api = TraktAPI(sickbeard.TRAKT_API, sickbeard.TRAKT_USERNAME, sickbeard.TRAKT_PASSWORD)
try: try:
recommendedlist = trakt_api.traktRequest("recommendations/shows.json/%APIKEY%") recommendedlist = trakt_api.traktRequest("recommendations/shows.json/%APIKEY%")
@ -3018,7 +3018,7 @@ class NewHomeAddShows(MainHandler):
t.trending_shows = [] t.trending_shows = []
trakt_api = TraktAPI(sickbeard.TRAKT_API, sickbeard.TRAKT_USERNAME, sickbeard.TRAKT_USERNAME) trakt_api = TraktAPI(sickbeard.TRAKT_API, sickbeard.TRAKT_USERNAME, sickbeard.TRAKT_PASSWORD)
try: try:
trending_shows = trakt_api.traktRequest("shows/trending.json/%APIKEY%") trending_shows = trakt_api.traktRequest("shows/trending.json/%APIKEY%")