2014-03-10 05:18:05 +00:00
|
|
|
# Author: Mr_Orange <mr_orange@hotmail.it>
|
|
|
|
# URL: http://code.google.com/p/sickbeard/
|
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# This file is part of SickGear.
|
2014-03-10 05:18:05 +00:00
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# SickGear is free software: you can redistribute it and/or modify
|
2014-03-10 05:18:05 +00:00
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# SickGear is distributed in the hope that it will be useful,
|
2014-03-10 05:18:05 +00:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2014-11-12 16:43:14 +00:00
|
|
|
# along with SickGear. If not, see <http://www.gnu.org/licenses/>.
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
import json
|
|
|
|
from base64 import b64encode
|
|
|
|
|
|
|
|
import sickbeard
|
|
|
|
from sickbeard import logger
|
|
|
|
from sickbeard.clients.generic import GenericClient
|
2015-05-06 17:25:12 +00:00
|
|
|
from lib.requests.exceptions import RequestException
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
|
2014-03-25 05:57:24 +00:00
|
|
|
class DelugeAPI(GenericClient):
|
2014-03-10 05:18:05 +00:00
|
|
|
def __init__(self, host=None, username=None, password=None):
|
|
|
|
|
|
|
|
super(DelugeAPI, self).__init__('Deluge', host, username, password)
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
self.url = self.host + 'json'
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
def _get_auth(self):
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2015-05-06 17:25:12 +00:00
|
|
|
post_data = json.dumps({'method': 'auth.login',
|
|
|
|
'params': [self.password],
|
|
|
|
'id': 1})
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
try:
|
2015-05-06 17:25:12 +00:00
|
|
|
self.auth = self.session.post(
|
|
|
|
self.url,
|
|
|
|
data=post_data.encode('utf-8'),
|
|
|
|
verify=sickbeard.TORRENT_VERIFY_CERT
|
|
|
|
).json()['result']
|
|
|
|
|
|
|
|
post_data = json.dumps({'method': 'web.connected',
|
|
|
|
'params': [],
|
|
|
|
'id': 10})
|
|
|
|
|
|
|
|
connected = self.session.post(
|
|
|
|
self.url,
|
|
|
|
data=post_data.encode('utf-8'),
|
|
|
|
verify=sickbeard.TORRENT_VERIFY_CERT
|
|
|
|
).json()['result']
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2015-05-06 17:25:12 +00:00
|
|
|
if not connected:
|
|
|
|
post_data = json.dumps({'method': 'web.get_hosts',
|
|
|
|
'params': [],
|
|
|
|
'id': 11})
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
post_data = json.dumps({'method': 'web.connect',
|
|
|
|
'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',
|
|
|
|
'params': [],
|
|
|
|
'id': 10})
|
|
|
|
connected = self.session.post(
|
|
|
|
self.url,
|
|
|
|
data=post_data.encode('utf-8'),
|
|
|
|
verify=sickbeard.TORRENT_VERIFY_CERT
|
|
|
|
).json()['result']
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
if not connected:
|
2015-05-06 17:25:12 +00:00
|
|
|
logger.log(self.name + u': WebUI could not connect to daemon',
|
|
|
|
logger.ERROR)
|
2014-03-10 05:18:05 +00:00
|
|
|
return None
|
2015-05-06 17:25:12 +00:00
|
|
|
except RequestException:
|
|
|
|
return None
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
return self.auth
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
def _add_torrent_uri(self, result):
|
|
|
|
|
2015-05-06 17:25:12 +00:00
|
|
|
post_data = json.dumps({
|
|
|
|
'method': 'core.add_torrent_magnet',
|
|
|
|
'params': [result.url, {
|
|
|
|
'move_completed': 'true',
|
|
|
|
'move_completed_path': sickbeard.TV_DOWNLOAD_DIR
|
|
|
|
}],
|
|
|
|
'id': 2
|
2014-03-25 05:57:24 +00:00
|
|
|
})
|
2015-05-06 17:25:12 +00:00
|
|
|
result.hash = self._request(method='post',
|
|
|
|
data=post_data).json()['result']
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2015-05-06 17:25:12 +00:00
|
|
|
return result.hash
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
def _add_torrent_file(self, result):
|
|
|
|
|
2015-05-06 17:25:12 +00:00
|
|
|
post_data = json.dumps({'method':
|
|
|
|
'core.add_torrent_file',
|
|
|
|
'params': [result.name + '.torrent',
|
|
|
|
b64encode(result.content),
|
|
|
|
{'move_completed': 'true',
|
|
|
|
'move_completed_path':
|
|
|
|
sickbeard.TV_DOWNLOAD_DIR}],
|
|
|
|
'id': 2})
|
|
|
|
result.hash = self._request(method='post',
|
|
|
|
data=post_data).json()['result']
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2015-05-06 17:25:12 +00:00
|
|
|
return result.hash
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
def _set_torrent_label(self, result):
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-05-11 07:31:38 +00:00
|
|
|
label = sickbeard.TORRENT_LABEL
|
2014-05-08 22:28:28 +00:00
|
|
|
if ' ' in label:
|
2015-05-06 17:25:12 +00:00
|
|
|
logger.log(self.name +
|
|
|
|
u': Invalid label. Label must not contain a space',
|
|
|
|
logger.ERROR)
|
2014-05-08 22:28:28 +00:00
|
|
|
return False
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
if label:
|
|
|
|
# check if label already exists and create it if not
|
2015-05-06 17:25:12 +00:00
|
|
|
post_data = json.dumps({
|
|
|
|
'method': 'label.get_labels',
|
|
|
|
'params': [],
|
|
|
|
'id': 3
|
2014-03-25 05:57:24 +00:00
|
|
|
})
|
2015-05-06 17:25:12 +00:00
|
|
|
labels = self._request(method='post',
|
|
|
|
data=post_data).json()['result']
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2015-05-06 17:25:12 +00:00
|
|
|
if labels is not None:
|
2014-03-10 05:18:05 +00:00
|
|
|
if label not in labels:
|
2015-05-06 17:25:12 +00:00
|
|
|
logger.log(self.name + ': ' + label +
|
|
|
|
u' label does not exist in ' +
|
|
|
|
u'Deluge we must add it',
|
2014-03-25 05:57:24 +00:00
|
|
|
logger.DEBUG)
|
2015-05-06 17:25:12 +00:00
|
|
|
post_data = json.dumps({
|
|
|
|
'method': 'label.add',
|
|
|
|
'params': [label],
|
|
|
|
'id': 4
|
2014-03-25 05:57:24 +00:00
|
|
|
})
|
2014-03-10 05:18:05 +00:00
|
|
|
self._request(method='post', data=post_data)
|
2015-05-06 17:25:12 +00:00
|
|
|
logger.log(self.name + ': ' + label +
|
|
|
|
u' label added to Deluge', logger.DEBUG)
|
|
|
|
|
|
|
|
# add label to torrent
|
|
|
|
post_data = json.dumps({
|
|
|
|
'method': 'label.set_torrent',
|
|
|
|
'params': [result.hash, label],
|
|
|
|
'id': 5
|
2014-03-25 05:57:24 +00:00
|
|
|
})
|
2014-03-10 05:18:05 +00:00
|
|
|
self._request(method='post', data=post_data)
|
2015-05-06 17:25:12 +00:00
|
|
|
logger.log(self.name + ': ' + label +
|
|
|
|
u' label added to torrent',
|
|
|
|
logger.DEBUG)
|
2014-03-10 05:18:05 +00:00
|
|
|
else:
|
2015-05-06 17:25:12 +00:00
|
|
|
logger.log(self.name + ': ' +
|
|
|
|
u'label plugin not detected',
|
|
|
|
logger.DEBUG)
|
2014-03-10 05:18:05 +00:00
|
|
|
return False
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2015-05-06 17:25:12 +00:00
|
|
|
return True
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
def _set_torrent_ratio(self, result):
|
|
|
|
|
2014-05-23 06:42:11 +00:00
|
|
|
ratio = None
|
2014-05-08 22:28:28 +00:00
|
|
|
if result.ratio:
|
|
|
|
ratio = result.ratio
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-05-23 06:42:11 +00:00
|
|
|
if ratio:
|
2015-05-06 17:25:12 +00:00
|
|
|
post_data = json.dumps({'method': 'core.set_torrent_stop_at_ratio',
|
|
|
|
'params': [result.hash, True],
|
|
|
|
'id': 5})
|
2014-05-23 06:42:11 +00:00
|
|
|
self._request(method='post', data=post_data)
|
2014-05-08 22:28:28 +00:00
|
|
|
|
2015-05-06 17:25:12 +00:00
|
|
|
post_data = json.dumps({'method': 'core.set_torrent_stop_ratio',
|
|
|
|
'params': [result.hash, float(ratio)],
|
|
|
|
'id': 6})
|
2014-05-23 06:42:11 +00:00
|
|
|
self._request(method='post', data=post_data)
|
2014-03-10 05:18:05 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def _set_torrent_path(self, result):
|
|
|
|
|
|
|
|
if sickbeard.TORRENT_PATH:
|
2015-05-06 17:25:12 +00:00
|
|
|
post_data = json.dumps({
|
|
|
|
'method': 'core.set_torrent_move_completed',
|
|
|
|
'params': [result.hash, True],
|
|
|
|
'id': 7
|
2014-03-25 05:57:24 +00:00
|
|
|
})
|
2014-03-10 05:18:05 +00:00
|
|
|
self._request(method='post', data=post_data)
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2015-05-06 17:25:12 +00:00
|
|
|
post_data = json.dumps({
|
|
|
|
'method': 'core.set_torrent_move_completed_path',
|
|
|
|
'params': [result.hash, sickbeard.TORRENT_PATH],
|
|
|
|
'id': 8
|
2014-03-25 05:57:24 +00:00
|
|
|
})
|
2014-03-10 05:18:05 +00:00
|
|
|
self._request(method='post', data=post_data)
|
|
|
|
return True
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
def _set_torrent_pause(self, result):
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
if sickbeard.TORRENT_PAUSED:
|
2015-05-06 17:25:12 +00:00
|
|
|
post_data = json.dumps({'method': 'core.pause_torrent',
|
|
|
|
'params': [[result.hash]],
|
|
|
|
'id': 9})
|
2014-03-10 05:18:05 +00:00
|
|
|
self._request(method='post', data=post_data)
|
2014-03-25 05:57:24 +00:00
|
|
|
return True
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-05-06 17:25:12 +00:00
|
|
|
api = DelugeAPI()
|