From 5afcec26937adcf6ff5ebb281e59aa1f01550779 Mon Sep 17 00:00:00 2001 From: JackDandy Date: Mon, 5 Sep 2016 04:59:13 +0100 Subject: [PATCH] Fix data logger for clients on develop branch (DSM). --- CHANGES.md | 1 + sickbeard/clients/download_station.py | 56 +++++++++++++++------------ sickbeard/clients/generic.py | 35 +++++++++-------- 3 files changed, 51 insertions(+), 41 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 46e9555a..3fb8433d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -136,6 +136,7 @@ * Change PrivateHD torrent provider * Fix Add from Trakt * Change unpack files once only in auto post processing copy mode +* Fix data logger for clients ### 0.11.14 (2016-07-25 03:10:00 UTC) diff --git a/sickbeard/clients/download_station.py b/sickbeard/clients/download_station.py index 4238f5b2..20eab872 100644 --- a/sickbeard/clients/download_station.py +++ b/sickbeard/clients/download_station.py @@ -18,58 +18,66 @@ # You should have received a copy of the GNU General Public License # along with SickGear. If not, see . # -# Uses the Synology Download Station API: http://download.synology.com/download/Document/DeveloperGuide/Synology_Download_Station_Web_API.pdf +# Uses the Synology Download Station API: +# http://download.synology.com/download/Document/DeveloperGuide/Synology_Download_Station_Web_API.pdf import sickbeard from sickbeard.clients.generic import GenericClient + class DownloadStationAPI(GenericClient): def __init__(self, host=None, username=None, password=None): super(DownloadStationAPI, self).__init__('DownloadStation', host, username, password) - self.url = self.host + 'webapi/DownloadStation/task.cgi' + self.url = '%swebapi/DownloadStation/task.cgi' % self.host def _get_auth(self): - auth_url = self.host + 'webapi/auth.cgi?api=SYNO.API.Auth&version=2&method=login&account=' + self.username + '&passwd=' + self.password + '&session=DownloadStation&format=sid' + auth_url = ('%swebapi/auth.cgi?api=SYNO.API.Auth&' % self.host + + 'version=2&method=login&account=%s&passwd=%s' % (self.username, self.password) + + '&session=DownloadStation&format=sid') try: response = self.session.get(auth_url, verify=False) self.auth = response.json()['data']['sid'] - except: + except (StandardError, Exception): return None return self.auth def _add_torrent_uri(self, result): - data = {'api':'SYNO.DownloadStation.Task', - 'version':'1', 'method':'create', - 'session':'DownloadStation', - '_sid':self.auth, - 'uri':result.url - } - if sickbeard.TORRENT_PATH: - data['destination'] = sickbeard.TORRENT_PATH - response = self._request(method='post', data=data) - - return response.json()['success'] + return self.send_dsm_request(params={'uri': result.url}) def _add_torrent_file(self, result): - data = {'api':'SYNO.DownloadStation.Task', - 'version':'1', - 'method':'create', - 'session':'DownloadStation', - '_sid':self.auth - } + return self.send_dsm_request(files={'file': ('%s.torrent' % result.name, result.content)}) + + def send_dsm_request(self, params=None, files=None): + + api_params = { + 'method': 'create', + 'version': '1', + 'api': 'SYNO.DownloadStation.Task', + 'session': 'DownloadStation', + '_sid': self.auth + } + if sickbeard.TORRENT_PATH: - data['destination'] = sickbeard.TORRENT_PATH - files = {'file':(result.name + '.torrent', result.content)} + api_params['destination'] = sickbeard.TORRENT_PATH + + data = api_params.copy() + if params: + data.update(params) response = self._request(method='post', data=data, files=files) - return response.json()['success'] + try: + result = response.json()['success'] + except (StandardError, Exception): + result = None + + return result api = DownloadStationAPI() diff --git a/sickbeard/clients/generic.py b/sickbeard/clients/generic.py index ce0ebaf4..543f14ea 100644 --- a/sickbeard/clients/generic.py +++ b/sickbeard/clients/generic.py @@ -33,22 +33,23 @@ class GenericClient(object): self.last_time = time.time() self._get_auth() - logger.log( - '%s: Requested a %s connection to url %s with Params= %s' % (self.name, method.upper(), self.url, params) + - ' Data= ' + ('None' if not data else '%s%s' % (data[0:99], ('', '...')[200 < len(data)])) + - ' Json= ' + ('None' if not kwargs.get('json') else '%s%s' % - (str(kwargs.get('json'))[0:99], - ('', '...')[200 < len(str(kwargs.get('json')))])), - logger.DEBUG - ) - - logger.log( - '%s: Requested a %s connection to url %s with Params= %s' % (self.name, method.upper(), self.url, params) + - ('' if not data else ' Data= %s%s' % (data[0:100], ('', '...')[100 < len(data)])) + - ('' if not kwargs.get('json') else ' Json= %s%s' % (str(kwargs.get('json'))[0:100], - ('', '...')[100 < len(str(kwargs.get('json')))])), - logger.DEBUG - ) + logger.log('%s: sending %s request to %s with ...' % (self.name, method.upper(), self.url), logger.DEBUG) + lines = [('params', (str(params), '')[not params]), + ('data', (str(data), '')[not data]), + ('files', (str(files), '')[not files]), + ('json', (str(kwargs.get('json')), '')[not kwargs.get('json')])] + m, c = 300, 100 + type_chunks = [(linetype, [ln[i:i + c] for i in range(0, min(len(ln), m), c)]) for linetype, ln in lines if ln] + for (arg, chunks) in type_chunks: + output = [] + nch = len(chunks) - 1 + for i, seg in enumerate(chunks): + if nch == i and 'files' == arg: + sample = ' ..excerpt(%s/%s)' % (m, len(lines[2][1])) + seg = seg[0:c - (len(sample) - 2)] + sample + output += ['%s: request %s= %s%s%s' % (self.name, arg, ('', '..')[bool(i)], seg, ('', '..')[i != nch])] + for out in output: + logger.log(out, logger.DEBUG) if not self.auth: logger.log('%s: Authentication Failed' % self.name, logger.ERROR) @@ -228,5 +229,5 @@ class GenericClient(object): if authenticated and self.auth: return True, 'Success: Connected and Authenticated' return False, 'Error: Unable to get %s Authentication, check your config!' % self.name - except Exception: + except (StandardError, Exception): return False, 'Error: Unable to connect to %s' % self.name