Browse Source

Merge branch 'feature/FixLibtraktPy3' into develop

pull/1289/head
JackDandy 5 years ago
parent
commit
f6c31b2c63
  1. 5
      lib/exceptions_helper.py
  2. 13
      lib/libtrakt/indexerapiinterface.py
  3. 37
      lib/libtrakt/trakt.py
  4. 2
      sickbeard/logger.py

5
lib/exceptions_helper.py

@ -21,8 +21,13 @@ from six import PY2, string_types
if PY2:
from encodingKludge import fixStupidEncodings
# noinspection PyUnreachableCode
if False:
from typing import AnyStr
def ex(e):
# type: (BaseException) -> AnyStr
"""Returns a unicode string from the exception text if it exists"""
if not PY2:

13
lib/libtrakt/indexerapiinterface.py

@ -6,6 +6,9 @@ from exceptions_helper import ex
from six import iteritems
from .trakt import TraktAPI
log = logging.getLogger('trakt_api')
log.addHandler(logging.NullHandler())
class ShowContainer(dict):
"""Simple dict that holds a series of Show instances
@ -33,10 +36,6 @@ class ShowContainer(dict):
super(ShowContainer, self).__setitem__(key, value)
def log():
return logging.getLogger('trakt_api')
class TraktSearchTypes(object):
text = 1
trakt_id = 'trakt'
@ -98,11 +97,11 @@ class TraktIndexer(object):
all_series = [all_series]
if 0 == len(all_series):
log().debug('Series result returned zero')
log.debug('Series result returned zero')
raise TraktShowNotFound('Show-name search returned zero results (cannot find show on TVDB)')
if None is not self.config['custom_ui']:
log().debug('Using custom UI %s' % (repr(self.config['custom_ui'])))
log.debug('Using custom UI %s' % (repr(self.config['custom_ui'])))
custom_ui = self.config['custom_ui']
ui = custom_ui(config=self.config)
@ -162,6 +161,6 @@ class TraktIndexer(object):
re.sub(r'T.*$', '', str(d.get('first_aired'))) or d.get('year'))
filtered.append(d)
except TraktException as e:
log().debug('Could not connect to Trakt service: %s' % ex(e))
log.debug('Could not connect to Trakt service: %s' % ex(e))
return filtered

37
lib/libtrakt/trakt.py

@ -4,10 +4,14 @@ import json
import sickbeard
import time
import datetime
from sickbeard import logger
import logging
from exceptions_helper import ex
from .exceptions import *
log = logging.getLogger('libtrakt')
log.addHandler(logging.NullHandler())
class TraktAccount(object):
max_auth_fail = 9
@ -151,7 +155,7 @@ class TraktAPI(object):
try:
TraktAPI().trakt_request('/oauth/revoke', send_oauth=account, method='POST')
except TraktException:
logger.log('Failed to remove account from trakt.tv')
log.info('Failed to remove account from trakt.tv')
sickbeard.TRAKT_ACCOUNTS.pop(account)
sickbeard.save_config()
return True
@ -255,8 +259,8 @@ class TraktAPI(object):
except requests.RequestException as e:
code = getattr(e.response, 'status_code', None)
if not code:
if 'timed out' in e:
logger.log(u'Timeout connecting to Trakt', logger.WARNING)
if 'timed out' in ex(e):
log.warning(u'Timeout connecting to Trakt')
if count >= self.max_retrys:
raise TraktTimeout()
return self.trakt_request(path, data, headers, url, count=count, sleep_retry=sleep_retry,
@ -264,12 +268,12 @@ class TraktAPI(object):
# This is pretty much a fatal error if there is no status_code
# It means there basically was no response at all
else:
logger.log(u'Could not connect to Trakt. Error: {0}'.format(e), logger.WARNING)
raise TraktException('Could not connect to Trakt. Error: {0}'.format(e))
log.warning(u'Could not connect to Trakt. Error: %s' % ex(e))
raise TraktException('Could not connect to Trakt. Error: %s' % ex(e))
elif 502 == code:
# Retry the request, Cloudflare had a proxying issue
logger.log(u'Retrying Trakt api request: %s' % path, logger.WARNING)
log.warning(u'Retrying Trakt api request: %s' % path)
if count >= self.max_retrys:
raise TraktCloudFlareException()
return self.trakt_request(path, data, headers, url, count=count, sleep_retry=sleep_retry,
@ -282,7 +286,7 @@ class TraktAPI(object):
return self.trakt_request(path, data, headers, url, count=count, sleep_retry=sleep_retry,
send_oauth=send_oauth, method=method)
logger.log(u'Unauthorized. Please check your Trakt settings', logger.WARNING)
log.warning(u'Unauthorized. Please check your Trakt settings')
sickbeard.TRAKT_ACCOUNTS[send_oauth].auth_failure()
raise TraktAuthException()
@ -297,21 +301,24 @@ class TraktAPI(object):
raise TraktAuthException()
elif code in (500, 501, 503, 504, 520, 521, 522):
if count >= self.max_retrys:
logger.log(u'Trakt may have some issues and it\'s unavailable. Code: %s' % code, logger.WARNING)
log.warning(u'Trakt may have some issues and it\'s unavailable. Code: %s' % code)
raise TraktServerError(error_code=code)
# http://docs.trakt.apiary.io/#introduction/status-codes
logger.log(u'Trakt may have some issues and it\'s unavailable. Trying again', logger.WARNING)
log.warning(u'Trakt may have some issues and it\'s unavailable. Trying again')
return self.trakt_request(path, data, headers, url, count=count, sleep_retry=sleep_retry,
send_oauth=send_oauth, method=method)
elif 404 == code:
logger.log(u'Trakt error (404) the resource does not exist: %s%s' % (url, path), logger.WARNING)
log.warning(u'Trakt error (404) the resource does not exist: %s%s' % (url, path))
raise TraktMethodNotExisting('Trakt error (404) the resource does not exist: %s%s' % (url, path))
else:
logger.log(u'Could not connect to Trakt. Code error: {0}'.format(code), logger.ERROR)
raise TraktException('Could not connect to Trakt. Code error: {0}'.format(code))
log.error(u'Could not connect to Trakt. Code error: {0}'.format(code))
raise TraktException('Could not connect to Trakt. Code error: %s' % ex(code))
except ValueError as e:
logger.log(u'Value Error: {0}'.format(e), logger.ERROR)
raise TraktValueError(u'Value Error: {0}'.format(e))
log.error(u'Value Error: %s' % ex(e))
raise TraktValueError(u'Value Error: %s' % ex(e))
except (BaseException, Exception) as e:
log.error('Exception: %s' % ex(e))
raise TraktException('Could not connect to Trakt. Code error: %s' % ex(e))
# check and confirm Trakt call did not fail
if isinstance(resp, dict) and 'failure' == resp.get('status', None):

2
sickbeard/logger.py

@ -74,7 +74,7 @@ class SBRotatingLogHandler(object):
self.log_lock = threading.Lock()
self.log_types = ['sickbeard', 'tornado.application', 'tornado.general', 'subliminal', 'adba', 'encodingKludge',
'tvdb_api']
self.external_loggers = ['sg_helper']
self.external_loggers = ['sg_helper', 'libtrakt', 'trakt_api']
self.log_types_null = ['tornado.access']
def __del__(self):

Loading…
Cancel
Save