Change refactor client for Deluge 1.3.14 compatibility.

Change ensure IPT authentication is valid before use.
This commit is contained in:
JackDandy 2017-03-12 01:24:47 +00:00
parent ccedfb8259
commit 74340d34f8
4 changed files with 71 additions and 119 deletions

View file

@ -1,4 +1,10 @@
### 0.12.9 (2017-02-24 18:40:00 UTC) ### 0.12.10 (2017-03-12 16:00:00 UTC)
* Change refactor client for Deluge 1.3.14 compatibility
* Change ensure IPT authentication is valid before use
### 0.12.9 (2017-02-24 18:40:00 UTC)
* Fix issue saving custom NewznabProviders * Fix issue saving custom NewznabProviders

View file

@ -1,8 +1,9 @@
# Author: Mr_Orange <mr_orange@hotmail.it> # coding=utf-8
# URL: http://code.google.com/p/sickbeard/
# #
# This file is part of SickGear. # This file is part of SickGear.
# #
# Original author: Mr_Orange <mr_orange@hotmail.it>
#
# SickGear is free software: you can redistribute it and/or modify # SickGear is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or # the Free Software Foundation, either version 3 of the License, or
@ -30,65 +31,37 @@ class DelugeAPI(GenericClient):
super(DelugeAPI, self).__init__('Deluge', host, username, password) super(DelugeAPI, self).__init__('Deluge', host, username, password)
self.url = self.host + 'json' self.url = '%s/json' % self.host.rstrip('/')
def _post_json(self, data, process=True):
result = self.session.post(self.url, json=data, timeout=10, verify=sickbeard.TORRENT_VERIFY_CERT)
if process:
return result.json()['result']
def _request_json(self, data, process=None):
result = self._request(method='post', json=data, timeout=10)
if process:
return result.json()['result']
def _get_auth(self): def _get_auth(self):
post_data = json.dumps({'method': 'auth.login',
'params': [self.password],
'id': 1})
try: try:
self.auth = self.session.post( self.auth = self._post_json({'method': 'auth.login', 'params': [self.password], 'id': 1})
self.url,
data=post_data.encode('utf-8'),
verify=sickbeard.TORRENT_VERIFY_CERT
).json()['result']
post_data = json.dumps({'method': 'web.connected', connected = self._post_json({'method': 'web.connected', 'params': [], 'id': 10})
'params': [],
'id': 10})
connected = self.session.post(
self.url,
data=post_data.encode('utf-8'),
verify=sickbeard.TORRENT_VERIFY_CERT
).json()['result']
if not connected: if not connected:
post_data = json.dumps({'method': 'web.get_hosts', hosts = self._post_json({'method': 'web.get_hosts', 'params': [], 'id': 11})
'params': [], if 0 == len(hosts):
'id': 11}) logger.log('%s: WebUI does not contain daemons' % self.name, logger.ERROR)
hosts = self.session.post(
self.url,
data=post_data.encode('utf-8'),
verify=sickbeard.TORRENT_VERIFY_CERT
).json()['result']
if len(hosts) == 0:
logger.log(self.name + u': WebUI does not contain daemons',
logger.ERROR)
return None return None
post_data = json.dumps({'method': 'web.connect', self._post_json({'method': 'web.connect', 'params': [hosts[0][0]], 'id': 11}, False)
'params': [hosts[0][0]],
'id': 11})
self.session.post(self.url, data=post_data.encode('utf-8'),
verify=sickbeard.TORRENT_VERIFY_CERT)
post_data = json.dumps({'method': 'web.connected', connected = self._post_json({'method': 'web.connected', 'params': [], 'id': 10})
'params': [],
'id': 10})
connected = self.session.post(
self.url,
data=post_data.encode('utf-8'),
verify=sickbeard.TORRENT_VERIFY_CERT
).json()['result']
if not connected: if not connected:
logger.log(self.name + u': WebUI could not connect to daemon', logger.log('%s: WebUI could not connect to daemon' % self.name, logger.ERROR)
logger.ERROR)
return None return None
except RequestException: except RequestException:
return None return None
@ -97,31 +70,24 @@ class DelugeAPI(GenericClient):
def _add_torrent_uri(self, result): def _add_torrent_uri(self, result):
post_data = json.dumps({ result.hash = self._request_json({
'method': 'core.add_torrent_magnet', 'method': 'core.add_torrent_magnet',
'params': [result.url, { 'params': [result.url,
'move_completed': 'true', {'move_completed': 'true',
'move_completed_path': sickbeard.TV_DOWNLOAD_DIR 'move_completed_path': sickbeard.TV_DOWNLOAD_DIR}],
}], 'id': 2}, True)
'id': 2
})
result.hash = self._request(method='post',
data=post_data).json()['result']
return result.hash return result.hash
def _add_torrent_file(self, result): def _add_torrent_file(self, result):
post_data = json.dumps({'method': result.hash = self._request_json({
'core.add_torrent_file', 'method': 'core.add_torrent_file',
'params': [result.name + '.torrent', 'params': ['%s.torrent' % result.name,
b64encode(result.content), b64encode(result.content),
{'move_completed': 'true', {'move_completed': 'true',
'move_completed_path': 'move_completed_path': sickbeard.TV_DOWNLOAD_DIR}],
sickbeard.TV_DOWNLOAD_DIR}], 'id': 2}, True)
'id': 2})
result.hash = self._request(method='post',
data=post_data).json()['result']
return result.hash return result.hash
@ -129,50 +95,34 @@ class DelugeAPI(GenericClient):
label = sickbeard.TORRENT_LABEL label = sickbeard.TORRENT_LABEL
if ' ' in label: if ' ' in label:
logger.log(self.name + logger.log('%s: Invalid label. Label must not contain a space' % self.name, logger.ERROR)
u': Invalid label. Label must not contain a space',
logger.ERROR)
return False return False
if label: if label:
# check if label already exists and create it if not # check if label already exists and create it if not
post_data = json.dumps({ labels = self._request_json({
'method': 'label.get_labels', 'method': 'label.get_labels',
'params': [], 'params': [],
'id': 3 'id': 3}, True)
})
labels = self._request(method='post',
data=post_data).json()['result']
if labels is not None: if None is not labels:
if label not in labels: if label not in labels:
logger.log(self.name + ': ' + label + logger.log('%s: %s label does not exist in Deluge we must add it' % (self.name, label),
u' label does not exist in ' +
u'Deluge we must add it',
logger.DEBUG) logger.DEBUG)
post_data = json.dumps({ self._request_json({
'method': 'label.add', 'method': 'label.add',
'params': [label], 'params': [label],
'id': 4 'id': 4})
}) logger.log('%s: %s label added to Deluge' % (self.name, label), logger.DEBUG)
self._request(method='post', data=post_data)
logger.log(self.name + ': ' + label +
u' label added to Deluge', logger.DEBUG)
# add label to torrent # add label to torrent
post_data = json.dumps({ self._request_json({
'method': 'label.set_torrent', 'method': 'label.set_torrent',
'params': [result.hash, label], 'params': [result.hash, label],
'id': 5 'id': 5})
}) logger.log('%s: %s label added to torrent' % (self.name, label), logger.DEBUG)
self._request(method='post', data=post_data)
logger.log(self.name + ': ' + label +
u' label added to torrent',
logger.DEBUG)
else: else:
logger.log(self.name + ': ' + logger.log('%s: label plugin not detected' % self.name, logger.DEBUG)
u'label plugin not detected',
logger.DEBUG)
return False return False
return True return True
@ -184,42 +134,38 @@ class DelugeAPI(GenericClient):
ratio = result.ratio ratio = result.ratio
if ratio: if ratio:
post_data = json.dumps({'method': 'core.set_torrent_stop_at_ratio', self._request_json({
'params': [result.hash, True], 'method': 'core.set_torrent_stop_at_ratio',
'id': 5}) 'params': [result.hash, True],
self._request(method='post', data=post_data) 'id': 5})
post_data = json.dumps({'method': 'core.set_torrent_stop_ratio', self._request_json({
'params': [result.hash, float(ratio)], 'method': 'core.set_torrent_stop_ratio',
'id': 6}) 'params': [result.hash, float(ratio)],
self._request(method='post', data=post_data) 'id': 6})
return True return True
def _set_torrent_path(self, result): def _set_torrent_path(self, result):
if sickbeard.TORRENT_PATH: if sickbeard.TORRENT_PATH:
post_data = json.dumps({ self._request_json({
'method': 'core.set_torrent_move_completed', 'method': 'core.set_torrent_move_completed',
'params': [result.hash, True], 'params': [result.hash, True],
'id': 7 'id': 7})
})
self._request(method='post', data=post_data)
post_data = json.dumps({ self._request_json({
'method': 'core.set_torrent_move_completed_path', 'method': 'core.set_torrent_move_completed_path',
'params': [result.hash, sickbeard.TORRENT_PATH], 'params': [result.hash, sickbeard.TORRENT_PATH],
'id': 8 'id': 8})
})
self._request(method='post', data=post_data)
return True return True
def _set_torrent_pause(self, result): def _set_torrent_pause(self, result):
if sickbeard.TORRENT_PAUSED: if sickbeard.TORRENT_PAUSED:
post_data = json.dumps({'method': 'core.pause_torrent', self._request_json({
'params': [[result.hash]], 'method': 'core.pause_torrent',
'id': 9}) 'params': [[result.hash]],
self._request(method='post', data=post_data) 'id': 9})
return True return True

View file

@ -56,7 +56,7 @@ class GenericClient(object):
return False return False
try: try:
response = self.session.__getattribute__(method)(self.url, params=params, data=data, files=files, response = self.session.__getattribute__(method)(self.url, params=params, data=data, files=files,
timeout=120, verify=False, **kwargs) timeout=kwargs.pop('timeout', 120), verify=False, **kwargs)
except requests.exceptions.ConnectionError as e: except requests.exceptions.ConnectionError as e:
logger.log('%s: Unable to connect %s' % (self.name, ex(e)), logger.ERROR) logger.log('%s: Unable to connect %s' % (self.name, ex(e)), logger.ERROR)
return False return False

View file

@ -47,7 +47,7 @@ class IPTorrentsProvider(generic.TorrentProvider):
return super(IPTorrentsProvider, self)._authorised( return super(IPTorrentsProvider, self)._authorised(
logged_in=(lambda y='': all( logged_in=(lambda y='': all(
['IPTorrents' in y, self.has_all_cookies()] + ['IPTorrents' in y, 'type="password"' not in y[0:2048], self.has_all_cookies()] +
[(self.session.cookies.get(x) or 'sg!no!pw') in self.digest for x in 'uid', 'pass'])), [(self.session.cookies.get(x) or 'sg!no!pw') in self.digest for x in 'uid', 'pass'])),
failed_msg=(lambda y=None: u'Invalid cookie details for %s. Check settings')) failed_msg=(lambda y=None: u'Invalid cookie details for %s. Check settings'))