mirror of
https://github.com/moparisthebest/SickRage
synced 2025-01-05 10:58:01 -05:00
Fixed more issues with TraktAPI code
This commit is contained in:
parent
3d454979db
commit
42befb652d
@ -1,22 +1,19 @@
|
||||
import hashlib
|
||||
import requests
|
||||
|
||||
from requests.auth import HTTPBasicAuth
|
||||
from exceptions import traktException, traktAuthException, traktServerBusy
|
||||
|
||||
class TraktAPI():
|
||||
def __init__(self, apikey, username=None, password=None, use_https=False, timeout=5):
|
||||
self.apikey = apikey
|
||||
|
||||
self.username = username
|
||||
self.password = password
|
||||
if password: hashlib.sha1(password.encode('utf-8')).hexdigest()
|
||||
|
||||
self.protocol = 'https://' if use_https else 'http://'
|
||||
self.timeout = timeout
|
||||
|
||||
def validateAccount(self):
|
||||
url = '/account/test/%APIKEY%'
|
||||
return self.traktRequest(url)
|
||||
return self.traktRequest("account/test/%APIKEY%")
|
||||
|
||||
def traktRequest(self, url, data=None):
|
||||
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
|
||||
try:
|
||||
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 [])
|
||||
|
||||
# check for http errors and raise if any are present
|
||||
@ -34,12 +31,12 @@ class TraktAPI():
|
||||
# convert response to json
|
||||
resp = resp.json()
|
||||
except (requests.HTTPError, requests.ConnectionError) as e:
|
||||
if e.code == 401:
|
||||
raise traktAuthException(e.message, e.code)
|
||||
elif e.code == 503:
|
||||
raise traktServerBusy(e.message, e.code)
|
||||
if e.response.status_code == 401:
|
||||
raise traktAuthException(e.message)
|
||||
elif e.response.status_code == 503:
|
||||
raise traktServerBusy(e.message)
|
||||
else:
|
||||
raise traktException(e.message, e.code)
|
||||
raise traktException(e.message)
|
||||
|
||||
# check and confirm trakt call did not fail
|
||||
if isinstance(resp, dict) and resp.get('status', False) == 'failure':
|
||||
|
@ -47,7 +47,7 @@ class TraktNotifier:
|
||||
"""
|
||||
|
||||
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:
|
||||
try:
|
||||
@ -121,7 +121,7 @@ class TraktNotifier:
|
||||
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:
|
||||
if trakt_api.validateAccount():
|
||||
|
@ -35,7 +35,7 @@ class TraktChecker():
|
||||
|
||||
def __init__(self):
|
||||
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):
|
||||
try:
|
||||
|
@ -2973,7 +2973,7 @@ class NewHomeAddShows(MainHandler):
|
||||
|
||||
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:
|
||||
recommendedlist = trakt_api.traktRequest("recommendations/shows.json/%APIKEY%")
|
||||
@ -3018,7 +3018,7 @@ class NewHomeAddShows(MainHandler):
|
||||
|
||||
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:
|
||||
trending_shows = trakt_api.traktRequest("shows/trending.json/%APIKEY%")
|
||||
|
Loading…
Reference in New Issue
Block a user