2014-11-12 16:43:14 +00:00
|
|
|
# Authors: Mr_Orange <mr_orange@hotmail.it>
|
2014-03-10 05:18:05 +00:00
|
|
|
# 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 re
|
|
|
|
|
|
|
|
import sickbeard
|
2014-05-08 22:28:28 +00:00
|
|
|
from sickbeard import logger
|
2014-03-10 05:18:05 +00:00
|
|
|
from sickbeard.clients.generic import GenericClient
|
2015-02-12 16:32:06 +00:00
|
|
|
import urllib
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
class uTorrentAPI(GenericClient):
|
|
|
|
def __init__(self, host=None, username=None, password=None):
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
super(uTorrentAPI, self).__init__('uTorrent', host, username, password)
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
self.url = self.host + 'gui/'
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-20 18:03:22 +00:00
|
|
|
def _request(self, method='get', params={}, files=None):
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-02-12 16:32:06 +00:00
|
|
|
return super(uTorrentAPI, self)._request(
|
|
|
|
method=method,
|
2015-02-12 21:28:42 +00:00
|
|
|
params='token={0:s}&{1:s}'.format(self.auth, '&'.join(
|
|
|
|
['%s' % urllib.urlencode(dict([[key, str(value)]])) for key, value in params.iteritems()])) if any(params) else params,
|
2015-02-12 16:32:06 +00:00
|
|
|
files=files)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
def _get_auth(self):
|
|
|
|
|
2014-03-25 05:57:24 +00:00
|
|
|
try:
|
2015-05-06 17:25:12 +00:00
|
|
|
response = self.session.get(self.url + 'token.html', verify=False)
|
|
|
|
self.auth = re.findall('<div.*?>(.*?)</', response.text)[0]
|
|
|
|
return self.auth if not response.status_code == 404 else None
|
2014-03-25 05:57:24 +00:00
|
|
|
except:
|
2014-03-10 05:18:05 +00:00
|
|
|
return None
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
def _add_torrent_uri(self, result):
|
|
|
|
|
2014-03-25 05:57:24 +00:00
|
|
|
params = {'action': 'add-url', 's': result.url}
|
2014-03-10 05:18:05 +00:00
|
|
|
return self._request(params=params)
|
|
|
|
|
|
|
|
def _add_torrent_file(self, result):
|
|
|
|
|
2014-03-25 05:57:24 +00:00
|
|
|
params = {'action': 'add-file'}
|
|
|
|
files = {'torrent_file': (result.name + '.torrent', result.content)}
|
|
|
|
return self._request(method='post', params=params, files=files)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
def _set_torrent_label(self, result):
|
2014-03-25 05:57:24 +00:00
|
|
|
|
|
|
|
params = {'action': 'setprops',
|
|
|
|
'hash': result.hash,
|
|
|
|
's': 'label',
|
|
|
|
'v': sickbeard.TORRENT_LABEL
|
|
|
|
}
|
2014-03-10 05:18:05 +00:00
|
|
|
return self._request(params=params)
|
2014-04-28 14:34:27 +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-05-23 06:42:11 +00:00
|
|
|
if ratio:
|
2014-04-28 14:34:27 +00:00
|
|
|
params = {'action': 'setprops',
|
|
|
|
'hash': result.hash,
|
2014-05-23 06:42:11 +00:00
|
|
|
's': 'seed_override',
|
|
|
|
'v': '1'
|
2014-04-28 14:34:27 +00:00
|
|
|
}
|
2014-05-23 06:42:11 +00:00
|
|
|
if self._request(params=params):
|
|
|
|
params = {'action': 'setprops',
|
|
|
|
'hash': result.hash,
|
|
|
|
's': 'seed_ratio',
|
|
|
|
'v': float(ratio) * 10
|
|
|
|
}
|
|
|
|
return self._request(params=params)
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
2014-04-28 14:34:27 +00:00
|
|
|
|
|
|
|
def _set_torrent_seed_time(self, result):
|
|
|
|
|
2014-05-08 22:28:28 +00:00
|
|
|
if sickbeard.TORRENT_SEED_TIME:
|
2014-04-28 14:34:27 +00:00
|
|
|
time = 3600 * float(sickbeard.TORRENT_SEED_TIME)
|
|
|
|
params = {'action': 'setprops',
|
|
|
|
'hash': result.hash,
|
|
|
|
's': 'seed_override',
|
|
|
|
'v': '1'
|
|
|
|
}
|
|
|
|
if self._request(params=params):
|
|
|
|
params = {'action': 'setprops',
|
|
|
|
'hash': result.hash,
|
|
|
|
's': 'seed_time',
|
|
|
|
'v': time
|
|
|
|
}
|
|
|
|
return self._request(params=params)
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
else:
|
2015-05-06 17:25:12 +00:00
|
|
|
return True
|
|
|
|
|
2014-04-25 09:34:24 +00:00
|
|
|
def _set_torrent_priority(self, result):
|
|
|
|
|
|
|
|
if result.priority == 1:
|
|
|
|
params = {'action': 'queuetop', 'hash': result.hash}
|
2014-04-28 14:34:27 +00:00
|
|
|
return self._request(params=params)
|
|
|
|
else:
|
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_pause(self, result):
|
|
|
|
|
|
|
|
if sickbeard.TORRENT_PAUSED:
|
2014-03-25 05:57:24 +00:00
|
|
|
params = {'action': 'pause', 'hash': result.hash}
|
2014-03-10 05:18:05 +00:00
|
|
|
else:
|
2014-03-25 05:57:24 +00:00
|
|
|
params = {'action': 'start', 'hash': result.hash}
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
return self._request(params=params)
|
2014-03-25 05:57:24 +00:00
|
|
|
|
|
|
|
|
2015-05-06 17:25:12 +00:00
|
|
|
api = uTorrentAPI()
|