Fix data logger for clients on develop branch (DSM).

This commit is contained in:
JackDandy 2016-09-05 04:59:13 +01:00
parent 42d6141678
commit 5afcec2693
3 changed files with 51 additions and 41 deletions

View file

@ -136,6 +136,7 @@
* Change PrivateHD torrent provider * Change PrivateHD torrent provider
* Fix Add from Trakt * Fix Add from Trakt
* Change unpack files once only in auto post processing copy mode * 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) ### 0.11.14 (2016-07-25 03:10:00 UTC)

View file

@ -18,58 +18,66 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with SickGear. If not, see <http://www.gnu.org/licenses/>. # along with SickGear. If not, see <http://www.gnu.org/licenses/>.
# #
# 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 import sickbeard
from sickbeard.clients.generic import GenericClient from sickbeard.clients.generic import GenericClient
class DownloadStationAPI(GenericClient): class DownloadStationAPI(GenericClient):
def __init__(self, host=None, username=None, password=None): def __init__(self, host=None, username=None, password=None):
super(DownloadStationAPI, self).__init__('DownloadStation', host, username, password) 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): 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: try:
response = self.session.get(auth_url, verify=False) response = self.session.get(auth_url, verify=False)
self.auth = response.json()['data']['sid'] self.auth = response.json()['data']['sid']
except: except (StandardError, Exception):
return None return None
return self.auth return self.auth
def _add_torrent_uri(self, result): def _add_torrent_uri(self, result):
data = {'api':'SYNO.DownloadStation.Task', return self.send_dsm_request(params={'uri': result.url})
'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']
def _add_torrent_file(self, result): def _add_torrent_file(self, result):
data = {'api':'SYNO.DownloadStation.Task', return self.send_dsm_request(files={'file': ('%s.torrent' % result.name, result.content)})
'version':'1',
'method':'create', def send_dsm_request(self, params=None, files=None):
'session':'DownloadStation',
'_sid':self.auth api_params = {
} 'method': 'create',
'version': '1',
'api': 'SYNO.DownloadStation.Task',
'session': 'DownloadStation',
'_sid': self.auth
}
if sickbeard.TORRENT_PATH: if sickbeard.TORRENT_PATH:
data['destination'] = sickbeard.TORRENT_PATH api_params['destination'] = sickbeard.TORRENT_PATH
files = {'file':(result.name + '.torrent', result.content)}
data = api_params.copy()
if params:
data.update(params)
response = self._request(method='post', data=data, files=files) 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() api = DownloadStationAPI()

View file

@ -33,22 +33,23 @@ class GenericClient(object):
self.last_time = time.time() self.last_time = time.time()
self._get_auth() self._get_auth()
logger.log( logger.log('%s: sending %s request to %s with ...' % (self.name, method.upper(), self.url), logger.DEBUG)
'%s: Requested a %s connection to url %s with Params= %s' % (self.name, method.upper(), self.url, params) + lines = [('params', (str(params), '')[not params]),
' Data= ' + ('None' if not data else '%s%s' % (data[0:99], ('', '...')[200 < len(data)])) + ('data', (str(data), '')[not data]),
' Json= ' + ('None' if not kwargs.get('json') else '%s%s' % ('files', (str(files), '')[not files]),
(str(kwargs.get('json'))[0:99], ('json', (str(kwargs.get('json')), '')[not kwargs.get('json')])]
('', '...')[200 < len(str(kwargs.get('json')))])), m, c = 300, 100
logger.DEBUG 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 = []
logger.log( nch = len(chunks) - 1
'%s: Requested a %s connection to url %s with Params= %s' % (self.name, method.upper(), self.url, params) + for i, seg in enumerate(chunks):
('' if not data else ' Data= %s%s' % (data[0:100], ('', '...')[100 < len(data)])) + if nch == i and 'files' == arg:
('' if not kwargs.get('json') else ' Json= %s%s' % (str(kwargs.get('json'))[0:100], sample = ' ..excerpt(%s/%s)' % (m, len(lines[2][1]))
('', '...')[100 < len(str(kwargs.get('json')))])), seg = seg[0:c - (len(sample) - 2)] + sample
logger.DEBUG 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: if not self.auth:
logger.log('%s: Authentication Failed' % self.name, logger.ERROR) logger.log('%s: Authentication Failed' % self.name, logger.ERROR)
@ -228,5 +229,5 @@ class GenericClient(object):
if authenticated and self.auth: if authenticated and self.auth:
return True, 'Success: Connected and Authenticated' return True, 'Success: Connected and Authenticated'
return False, 'Error: Unable to get %s Authentication, check your config!' % self.name 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 return False, 'Error: Unable to connect to %s' % self.name