From 18da6210160b45c98da09d167adf5c4803e8fbfb Mon Sep 17 00:00:00 2001 From: Jamie Date: Sat, 9 Aug 2014 00:07:17 +0100 Subject: [PATCH] Trakt method, error checking, remove series --- .../default/config_notifications.tmpl | 7 +++- lib/trakt/__init__.py | 37 ++++++++++++------- sickbeard/__init__.py | 1 + sickbeard/traktChecker.py | 12 ++++++ sickbeard/webserve.py | 8 +++- 5 files changed, 50 insertions(+), 15 deletions(-) diff --git a/gui/slick/interfaces/default/config_notifications.tmpl b/gui/slick/interfaces/default/config_notifications.tmpl index 253c205d..cec0087e 100644 --- a/gui/slick/interfaces/default/config_notifications.tmpl +++ b/gui/slick/interfaces/default/config_notifications.tmpl @@ -1237,8 +1237,13 @@
+ +
diff --git a/lib/trakt/__init__.py b/lib/trakt/__init__.py index c9565a3b..255d0f12 100644 --- a/lib/trakt/__init__.py +++ b/lib/trakt/__init__.py @@ -1,7 +1,8 @@ -import urllib2 +from urllib2 import Request, urlopen, HTTPError, URLError + +import base64 +from sha import new as sha1 -from hashlib import sha1 - try: import json except ImportError: @@ -25,25 +26,35 @@ def TraktCall(method, api, username=None, password=None, data={}): if not api: return None - # if the username isn't given then it failed - if username and password: - password = sha1(password).hexdigest() - data["username"] = username - data["password"] = password - # replace the API string with what we found method = method.replace("%API%", api) + # make the full url + url = 'https://api.trakt.tv/' + method + # take the URL params and make a json object out of them - encoded_data = json.dumps(data) + encoded_data = json.JSONEncoder().encode(data) + + request = Request(url, encoded_data) + + # if the username isn't given then it failed + if username and password: + pwdsha1 = sha1(password).hexdigest() + base64string = base64.encodestring('%s:%s' % (username, pwdsha1)).replace('\n', '') + request.add_header("Accept", "*/*") + request.add_header("User-Agent", "CPython/2.7.5 Unknown/Unknown") + request.add_header("Authorization", "Basic %s" % base64string) # 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() + stream = urlopen(request).read() - resp = json.loads(resp) + # check if results are valid + if stream == '[]': + resp = 'NULL' + else: + resp = json.JSONDecoder().decode(stream) if ("error" in resp): raise Exception(resp["error"]) diff --git a/sickbeard/__init__.py b/sickbeard/__init__.py index 81b037aa..ef2d28bf 100755 --- a/sickbeard/__init__.py +++ b/sickbeard/__init__.py @@ -351,6 +351,7 @@ TRAKT_USERNAME = None TRAKT_PASSWORD = None TRAKT_API = '' TRAKT_REMOVE_WATCHLIST = False +TRAKT_REMOVE_SERIESLIST = False TRAKT_USE_WATCHLIST = False TRAKT_METHOD_ADD = 0 TRAKT_START_PAUSED = False diff --git a/sickbeard/traktChecker.py b/sickbeard/traktChecker.py index 0e1fd0b6..a940fc8d 100644 --- a/sickbeard/traktChecker.py +++ b/sickbeard/traktChecker.py @@ -53,6 +53,10 @@ class TraktChecker(): def findShow(self, indexer, indexerid): library = TraktCall("user/library/shows/all.json/%API%/" + sickbeard.TRAKT_USERNAME, sickbeard.TRAKT_API, sickbeard.TRAKT_USERNAME, sickbeard.TRAKT_PASSWORD) + if library == 'NULL': + logger.log(u"No shows found in your library, aborting library update", logger.DEBUG) + return + if not library: logger.log(u"Could not connect to trakt service, aborting library check", logger.ERROR) return @@ -102,6 +106,10 @@ class TraktChecker(): logger.log(u"Starting trakt show watchlist check", logger.DEBUG) watchlist = TraktCall("user/watchlist/shows.json/%API%/" + sickbeard.TRAKT_USERNAME, sickbeard.TRAKT_API, sickbeard.TRAKT_USERNAME, sickbeard.TRAKT_PASSWORD) + if watchlist == 'NULL': + logger.log(u"No shows found in your watchlist, aborting watchlist update", logger.DEBUG) + return + if not watchlist: logger.log(u"Could not connect to trakt service, aborting watchlist update", logger.ERROR) return @@ -134,6 +142,10 @@ class TraktChecker(): logger.log(u"Starting trakt episode watchlist check", logger.DEBUG) watchlist = TraktCall("user/watchlist/episodes.json/%API%/" + sickbeard.TRAKT_USERNAME, sickbeard.TRAKT_API, sickbeard.TRAKT_USERNAME, sickbeard.TRAKT_PASSWORD) + if watchlist == 'NULL': + logger.log(u"No episodes found in your watchlist, aborting watchlist update", logger.DEBUG) + return + if not watchlist: logger.log(u"Could not connect to trakt service, aborting watchlist update", logger.ERROR) return diff --git a/sickbeard/webserve.py b/sickbeard/webserve.py index edb46648..2e42a247 100644 --- a/sickbeard/webserve.py +++ b/sickbeard/webserve.py @@ -2252,7 +2252,7 @@ class ConfigNotifications(MainHandler): use_trakt=None, trakt_username=None, trakt_password=None, trakt_api=None, trakt_remove_watchlist=None, trakt_use_watchlist=None, trakt_method_add=None, trakt_start_paused=None, trakt_use_recommended=None, trakt_sync=None, - trakt_default_indexer=None, + trakt_default_indexer=None, trakt_remove_serieslist=None, use_synologynotifier=None, synologynotifier_notify_onsnatch=None, synologynotifier_notify_ondownload=None, synologynotifier_notify_onsubtitledownload=None, use_pytivo=None, pytivo_notify_onsnatch=None, pytivo_notify_ondownload=None, @@ -2360,6 +2360,7 @@ class ConfigNotifications(MainHandler): sickbeard.TRAKT_PASSWORD = trakt_password sickbeard.TRAKT_API = trakt_api sickbeard.TRAKT_REMOVE_WATCHLIST = config.checkbox_to_value(trakt_remove_watchlist) + sickbeard.TRAKT_REMOVE_SERIESLIST = config.checkbox_to_value(trakt_remove_serieslist) sickbeard.TRAKT_USE_WATCHLIST = config.checkbox_to_value(trakt_use_watchlist) sickbeard.TRAKT_METHOD_ADD = trakt_method_add sickbeard.TRAKT_START_PAUSED = config.checkbox_to_value(trakt_start_paused) @@ -2795,6 +2796,11 @@ class NewHomeAddShows(MainHandler): logger.log(u"Getting recommended shows from Trakt.tv", logger.DEBUG) recommendedlist = TraktCall("recommendations/shows.json/%API%", sickbeard.TRAKT_API, sickbeard.TRAKT_USERNAME, sickbeard.TRAKT_PASSWORD) + + if recommendedlist == 'NULL': + logger.log(u"No shows found in your recommendedlist, aborting recommendedlist update", logger.DEBUG) + return + if recommendedlist is None: logger.log(u"Could not connect to trakt service, aborting recommended list update", logger.ERROR) return