2014-03-10 01:18:05 -04:00
|
|
|
# Authors: Mr_Orange <mr_orange@hotmail.it>, EchelonFour
|
|
|
|
# URL: http://code.google.com/p/sickbeard/
|
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# This file is part of SickRage.
|
2014-03-10 01:18:05 -04:00
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# SickRage is free software: you can redistribute it and/or modify
|
2014-03-10 01:18:05 -04: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-05-23 08:37:22 -04:00
|
|
|
# SickRage is distributed in the hope that it will be useful,
|
2014-03-10 01:18:05 -04: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-05-23 08:37:22 -04:00
|
|
|
# along with SickRage. If not, see <http://www.gnu.org/licenses/>.
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
import sickbeard
|
2014-05-08 18:28:28 -04:00
|
|
|
from sickbeard import logger
|
2014-03-10 01:18:05 -04:00
|
|
|
from sickbeard.clients.generic import GenericClient
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
class uTorrentAPI(GenericClient):
|
|
|
|
def __init__(self, host=None, username=None, password=None):
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
super(uTorrentAPI, self).__init__('uTorrent', host, username, password)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
self.url = self.host + 'gui/'
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-20 14:03:22 -04:00
|
|
|
def _request(self, method='get', params={}, files=None):
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
params.update({'token': self.auth})
|
2014-03-10 01:18:05 -04:00
|
|
|
return super(uTorrentAPI, self)._request(method=method, params=params, files=files)
|
|
|
|
|
|
|
|
def _get_auth(self):
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
try:
|
2014-04-26 03:09:00 -04:00
|
|
|
self.response = self.session.get(self.url + 'token.html', verify=False)
|
2014-03-10 01:18:05 -04:00
|
|
|
self.auth = re.findall("<div.*?>(.*?)</", self.response.text)[0]
|
2014-03-25 01:57:24 -04:00
|
|
|
except:
|
2014-03-10 01:18:05 -04:00
|
|
|
return None
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
return self.auth if not self.response.status_code == 404 else None
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def _add_torrent_uri(self, result):
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
params = {'action': 'add-url', 's': result.url}
|
2014-03-10 01:18:05 -04:00
|
|
|
return self._request(params=params)
|
|
|
|
|
|
|
|
def _add_torrent_file(self, result):
|
|
|
|
|
2014-03-25 01:57:24 -04: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 01:18:05 -04:00
|
|
|
|
|
|
|
def _set_torrent_label(self, result):
|
2014-03-25 01:57:24 -04:00
|
|
|
|
|
|
|
params = {'action': 'setprops',
|
|
|
|
'hash': result.hash,
|
|
|
|
's': 'label',
|
|
|
|
'v': sickbeard.TORRENT_LABEL
|
|
|
|
}
|
2014-03-10 01:18:05 -04:00
|
|
|
return self._request(params=params)
|
2014-04-28 10:34:27 -04:00
|
|
|
|
|
|
|
def _set_torrent_ratio(self, result):
|
|
|
|
|
2014-05-23 02:42:11 -04:00
|
|
|
ratio = None
|
2014-05-08 18:28:28 -04:00
|
|
|
if result.ratio:
|
|
|
|
ratio = result.ratio
|
|
|
|
|
2014-05-23 02:42:11 -04:00
|
|
|
if ratio:
|
2014-04-28 10:34:27 -04:00
|
|
|
params = {'action': 'setprops',
|
|
|
|
'hash': result.hash,
|
2014-05-23 02:42:11 -04:00
|
|
|
's': 'seed_override',
|
|
|
|
'v': '1'
|
2014-04-28 10:34:27 -04:00
|
|
|
}
|
2014-05-23 02:42:11 -04: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 10:34:27 -04:00
|
|
|
|
|
|
|
def _set_torrent_seed_time(self, result):
|
|
|
|
|
2014-05-08 18:28:28 -04:00
|
|
|
if sickbeard.TORRENT_SEED_TIME:
|
2014-04-28 10:34:27 -04: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:
|
|
|
|
return True
|
2014-04-25 05:34:24 -04:00
|
|
|
|
|
|
|
def _set_torrent_priority(self, result):
|
|
|
|
|
|
|
|
if result.priority == 1:
|
|
|
|
params = {'action': 'queuetop', 'hash': result.hash}
|
2014-04-28 10:34:27 -04:00
|
|
|
return self._request(params=params)
|
|
|
|
else:
|
|
|
|
return True
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def _set_torrent_pause(self, result):
|
|
|
|
|
|
|
|
if sickbeard.TORRENT_PAUSED:
|
2014-03-25 01:57:24 -04:00
|
|
|
params = {'action': 'pause', 'hash': result.hash}
|
2014-03-10 01:18:05 -04:00
|
|
|
else:
|
2014-03-25 01:57:24 -04:00
|
|
|
params = {'action': 'start', 'hash': result.hash}
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
return self._request(params=params)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
|
|
|
|
2014-04-25 05:34:24 -04:00
|
|
|
api = uTorrentAPI()
|