2014-03-10 01:18:05 -04:00
|
|
|
# Author: Mr_Orange <mr_orange@hotmail.it>
|
|
|
|
# URL: http://code.google.com/p/sickbeard/
|
|
|
|
#
|
|
|
|
# This file is part of Sick Beard.
|
|
|
|
#
|
|
|
|
# Sick Beard is free software: you can redistribute it and/or modify
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# Sick Beard is distributed in the hope that it will be useful,
|
|
|
|
# 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
|
|
|
|
# along with Sick Beard. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import json
|
|
|
|
from base64 import b64encode
|
|
|
|
|
|
|
|
import sickbeard
|
|
|
|
from sickbeard import logger
|
|
|
|
from sickbeard.clients.generic import GenericClient
|
|
|
|
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
class DelugeAPI(GenericClient):
|
2014-03-10 01:18:05 -04:00
|
|
|
def __init__(self, host=None, username=None, password=None):
|
|
|
|
|
|
|
|
super(DelugeAPI, self).__init__('Deluge', host, username, password)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
self.url = self.host + 'json'
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def _get_auth(self):
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
post_data = json.dumps({"method": "auth.login",
|
|
|
|
"params": [self.password],
|
|
|
|
"id": 1
|
2014-03-25 01:57:24 -04:00
|
|
|
})
|
2014-03-10 01:18:05 -04:00
|
|
|
try:
|
|
|
|
self.response = self.session.post(self.url, data=post_data.encode('utf-8'))
|
|
|
|
except:
|
2014-03-25 01:57:24 -04:00
|
|
|
return None
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
self.auth = self.response.json()["result"]
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
post_data = json.dumps({"method": "web.connected",
|
|
|
|
"params": [],
|
|
|
|
"id": 10
|
2014-03-25 01:57:24 -04:00
|
|
|
})
|
2014-03-10 01:18:05 -04:00
|
|
|
try:
|
|
|
|
self.response = self.session.post(self.url, data=post_data.encode('utf-8'))
|
|
|
|
except:
|
|
|
|
return None
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
connected = self.response.json()['result']
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
if not connected:
|
|
|
|
post_data = json.dumps({"method": "web.get_hosts",
|
|
|
|
"params": [],
|
|
|
|
"id": 11
|
2014-03-25 01:57:24 -04:00
|
|
|
})
|
2014-03-10 01:18:05 -04:00
|
|
|
try:
|
|
|
|
self.response = self.session.post(self.url, data=post_data.encode('utf-8'))
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
hosts = self.response.json()['result']
|
|
|
|
if len(hosts) == 0:
|
|
|
|
logger.log(self.name + u': WebUI does not contain daemons', logger.ERROR)
|
|
|
|
return None
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
post_data = json.dumps({"method": "web.connect",
|
|
|
|
"params": [hosts[0][0]],
|
|
|
|
"id": 11
|
2014-03-25 01:57:24 -04:00
|
|
|
})
|
2014-03-10 01:18:05 -04:00
|
|
|
try:
|
|
|
|
self.response = self.session.post(self.url, data=post_data.encode('utf-8'))
|
|
|
|
except:
|
|
|
|
return None
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
post_data = json.dumps({"method": "web.connected",
|
|
|
|
"params": [],
|
|
|
|
"id": 10
|
2014-03-25 01:57:24 -04:00
|
|
|
})
|
2014-03-10 01:18:05 -04:00
|
|
|
try:
|
|
|
|
self.response = self.session.post(self.url, data=post_data.encode('utf-8'))
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
|
|
|
connected = self.response.json()['result']
|
|
|
|
if not connected:
|
|
|
|
logger.log(self.name + u': WebUI could not connect to daemon', logger.ERROR)
|
|
|
|
return None
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
return self.auth
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def _add_torrent_uri(self, result):
|
|
|
|
|
|
|
|
post_data = json.dumps({"method": "core.add_torrent_magnet",
|
2014-03-25 01:57:24 -04:00
|
|
|
"params": [result.url, {"move_completed": "true",
|
|
|
|
"move_completed_path": sickbeard.TV_DOWNLOAD_DIR}],
|
2014-03-10 01:18:05 -04:00
|
|
|
"id": 2
|
2014-03-25 01:57:24 -04:00
|
|
|
})
|
2014-03-10 01:18:05 -04:00
|
|
|
self._request(method='post', data=post_data)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
result.hash = self.response.json()['result']
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
return self.response.json()['result']
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def _add_torrent_file(self, result):
|
|
|
|
|
|
|
|
post_data = json.dumps({"method": "core.add_torrent_file",
|
2014-03-25 01:57:24 -04:00
|
|
|
"params": [result.name + '.torrent', b64encode(result.content),
|
|
|
|
{"move_completed": "true",
|
|
|
|
"move_completed_path": sickbeard.TV_DOWNLOAD_DIR}],
|
2014-03-10 01:18:05 -04:00
|
|
|
"id": 2
|
2014-03-25 01:57:24 -04:00
|
|
|
})
|
2014-03-10 01:18:05 -04:00
|
|
|
self._request(method='post', data=post_data)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
result.hash = self.response.json()['result']
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
return self.response.json()['result']
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def _set_torrent_label(self, result):
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
label = sickbeard.TORRENT_LABEL.lower()
|
|
|
|
if label:
|
|
|
|
# check if label already exists and create it if not
|
|
|
|
post_data = json.dumps({"method": 'label.get_labels',
|
|
|
|
"params": [],
|
|
|
|
"id": 3
|
2014-03-25 01:57:24 -04:00
|
|
|
})
|
2014-03-10 01:18:05 -04:00
|
|
|
self._request(method='post', data=post_data)
|
|
|
|
labels = self.response.json()['result']
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-20 14:03:22 -04:00
|
|
|
if labels != None:
|
2014-03-10 01:18:05 -04:00
|
|
|
if label not in labels:
|
2014-03-25 01:57:24 -04:00
|
|
|
logger.log(self.name + ': ' + label + u" label does not exist in Deluge we must add it",
|
|
|
|
logger.DEBUG)
|
2014-03-10 01:18:05 -04:00
|
|
|
post_data = json.dumps({"method": 'label.add',
|
|
|
|
"params": [label],
|
|
|
|
"id": 4
|
2014-03-25 01:57:24 -04:00
|
|
|
})
|
2014-03-10 01:18:05 -04:00
|
|
|
self._request(method='post', data=post_data)
|
2014-03-25 01:57:24 -04:00
|
|
|
logger.log(self.name + ': ' + label + u" label added to Deluge", logger.DEBUG)
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
# add label to torrent
|
2014-03-25 01:57:24 -04:00
|
|
|
post_data = json.dumps({"method": 'label.set_torrent',
|
|
|
|
"params": [result.hash, label],
|
|
|
|
"id": 5
|
|
|
|
})
|
2014-03-10 01:18:05 -04:00
|
|
|
self._request(method='post', data=post_data)
|
2014-03-25 01:57:24 -04:00
|
|
|
logger.log(self.name + ': ' + label + u" label added to torrent", logger.DEBUG)
|
2014-03-10 01:18:05 -04:00
|
|
|
else:
|
|
|
|
logger.log(self.name + ': ' + u"label plugin not detected", logger.DEBUG)
|
|
|
|
return False
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
return not self.response.json()['error']
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def _set_torrent_ratio(self, result):
|
|
|
|
|
|
|
|
if sickbeard.TORRENT_RATIO:
|
|
|
|
post_data = json.dumps({"method": "core.set_torrent_stop_at_ratio",
|
|
|
|
"params": [result.hash, True],
|
|
|
|
"id": 5
|
2014-03-25 01:57:24 -04:00
|
|
|
})
|
2014-03-10 01:18:05 -04:00
|
|
|
self._request(method='post', data=post_data)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
post_data = json.dumps({"method": "core.set_torrent_stop_ratio",
|
2014-03-25 01:57:24 -04:00
|
|
|
"params": [result.hash, float(sickbeard.TORRENT_RATIO)],
|
2014-03-10 01:18:05 -04:00
|
|
|
"id": 6
|
2014-03-25 01:57:24 -04:00
|
|
|
})
|
2014-03-10 01:18:05 -04:00
|
|
|
self._request(method='post', data=post_data)
|
|
|
|
|
|
|
|
return not self.response.json()['error']
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
return True
|
|
|
|
|
|
|
|
def _set_torrent_path(self, result):
|
|
|
|
|
|
|
|
if sickbeard.TORRENT_PATH:
|
|
|
|
post_data = json.dumps({"method": "core.set_torrent_move_completed",
|
|
|
|
"params": [result.hash, True],
|
|
|
|
"id": 7
|
2014-03-25 01:57:24 -04:00
|
|
|
})
|
2014-03-10 01:18:05 -04:00
|
|
|
self._request(method='post', data=post_data)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
post_data = json.dumps({"method": "core.set_torrent_move_completed_path",
|
|
|
|
"params": [result.hash, sickbeard.TORRENT_PATH],
|
|
|
|
"id": 8
|
2014-03-25 01:57:24 -04:00
|
|
|
})
|
2014-03-10 01:18:05 -04:00
|
|
|
self._request(method='post', data=post_data)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
return not self.response.json()['error']
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
return True
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def _set_torrent_pause(self, result):
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
if sickbeard.TORRENT_PAUSED:
|
|
|
|
post_data = json.dumps({"method": "core.pause_torrent",
|
|
|
|
"params": [[result.hash]],
|
|
|
|
"id": 9
|
2014-03-25 01:57:24 -04:00
|
|
|
})
|
2014-03-10 01:18:05 -04:00
|
|
|
self._request(method='post', data=post_data)
|
|
|
|
|
|
|
|
return not self.response.json()['error']
|
2014-03-25 01:57:24 -04:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
api = DelugeAPI()
|