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 re
|
|
|
|
from base64 import b64encode
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
class TransmissionAPI(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(TransmissionAPI, self).__init__('Transmission', host, username, password)
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
self.url = self.host + 'transmission/rpc'
|
2016-03-09 11:16:02 +00:00
|
|
|
self.blankable, self.download_dir = None, None
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
def _get_auth(self):
|
|
|
|
|
2014-03-25 05:57:24 +00:00
|
|
|
try:
|
2016-03-09 11:16:02 +00:00
|
|
|
response = self.session.post(self.url, json={'method': 'session-get'},
|
|
|
|
timeout=120, verify=sickbeard.TORRENT_VERIFY_CERT)
|
|
|
|
self.auth = re.search(r'(?i)X-Transmission-Session-Id:\s*(\w+)', response.text).group(1)
|
2014-03-10 05:18:05 +00:00
|
|
|
except:
|
2014-03-25 05:57:24 +00:00
|
|
|
return None
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
self.session.headers.update({'x-transmission-session-id': self.auth})
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2016-03-09 11:16:02 +00:00
|
|
|
# Validating Transmission authorization
|
|
|
|
response = self._request(method='post', json={'method': 'session-get', 'arguments': {}})
|
|
|
|
|
|
|
|
try:
|
|
|
|
resp = response.json()
|
|
|
|
self.blankable = 14386 >= int(re.findall(r'.*[(](\d+)', resp.get('arguments', {}).get('version', '(0)'))[0])
|
|
|
|
self.download_dir = resp.get('arguments', {}).get('download-dir', '')
|
|
|
|
except:
|
|
|
|
pass
|
2014-03-25 05:57:24 +00:00
|
|
|
|
|
|
|
return self.auth
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
def _add_torrent_uri(self, result):
|
|
|
|
|
2016-03-09 11:16:02 +00:00
|
|
|
return self._add_torrent({'filename': result.url})
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
def _add_torrent_file(self, result):
|
|
|
|
|
2016-03-09 11:16:02 +00:00
|
|
|
return self._add_torrent({'metainfo': b64encode(result.content)})
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2016-03-09 11:16:02 +00:00
|
|
|
def _add_torrent(self, t_object):
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2016-03-09 11:16:02 +00:00
|
|
|
download_dir = None
|
|
|
|
if sickbeard.TORRENT_PATH or self.blankable:
|
|
|
|
download_dir = sickbeard.TORRENT_PATH
|
|
|
|
elif self.download_dir:
|
|
|
|
download_dir = self.download_dir
|
|
|
|
else:
|
|
|
|
logger.log('Path required for Transmission Downloaded files location', logger.ERROR)
|
|
|
|
|
|
|
|
if not download_dir and not self.blankable:
|
|
|
|
return False
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2016-03-09 11:16:02 +00:00
|
|
|
t_object.update({'paused': (0, 1)[sickbeard.TORRENT_PAUSED], 'download-dir': download_dir})
|
|
|
|
response = self._request(method='post', json={'method': 'torrent-add', 'arguments': t_object})
|
2014-05-08 22:28:28 +00:00
|
|
|
|
2016-03-09 11:16:02 +00:00
|
|
|
return 'success' == response.json().get('result', '')
|
|
|
|
|
|
|
|
def _set_torrent_ratio(self, result):
|
|
|
|
|
|
|
|
ratio, mode = (result.ratio, None)[not result.ratio], 0
|
2014-05-23 06:42:11 +00:00
|
|
|
if ratio:
|
2016-03-09 11:16:02 +00:00
|
|
|
if -1 == float(ratio):
|
|
|
|
ratio, mode = 0, 2
|
|
|
|
elif 0 <= float(ratio):
|
|
|
|
ratio, mode = float(ratio), 1 # Stop seeding at seedRatioLimit
|
|
|
|
|
|
|
|
response = self._request(method='post', json={
|
|
|
|
'method': 'torrent-set',
|
|
|
|
'arguments': {'ids': [result.hash], 'seedRatioLimit': ratio, 'seedRatioMode': mode}})
|
|
|
|
|
|
|
|
return 'success' == response.json().get('result', '')
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-08-31 06:41:53 +00:00
|
|
|
def _set_torrent_seed_time(self, result):
|
|
|
|
|
2016-03-09 11:16:02 +00:00
|
|
|
if result.provider.seed_time or (sickbeard.TORRENT_SEED_TIME and -1 != sickbeard.TORRENT_SEED_TIME):
|
2015-09-12 17:06:54 +00:00
|
|
|
seed_time = result.provider.seed_time or sickbeard.TORRENT_SEED_TIME
|
2014-08-31 06:41:53 +00:00
|
|
|
|
2016-03-09 11:16:02 +00:00
|
|
|
response = self._request(method='post', json={
|
|
|
|
'method': 'torrent-set',
|
|
|
|
'arguments': {'ids': [result.hash], 'seedIdleLimit': int(seed_time) * 60, 'seedIdleMode': 1}})
|
2014-08-31 06:41:53 +00:00
|
|
|
|
2016-03-09 11:16:02 +00:00
|
|
|
return 'success' == response.json().get('result', '')
|
2014-08-31 06:41:53 +00:00
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
def _set_torrent_priority(self, result):
|
|
|
|
|
2014-08-25 03:49:00 +00:00
|
|
|
arguments = {'ids': [result.hash]}
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2016-03-09 11:16:02 +00:00
|
|
|
if -1 == result.priority:
|
2014-03-10 05:18:05 +00:00
|
|
|
arguments['priority-low'] = []
|
2016-03-09 11:16:02 +00:00
|
|
|
elif 1 == result.priority:
|
2014-03-10 05:18:05 +00:00
|
|
|
# set high priority for all files in torrent
|
|
|
|
arguments['priority-high'] = []
|
|
|
|
# move torrent to the top if the queue
|
|
|
|
arguments['queuePosition'] = 0
|
|
|
|
if sickbeard.TORRENT_HIGH_BANDWIDTH:
|
|
|
|
arguments['bandwidthPriority'] = 1
|
|
|
|
else:
|
|
|
|
arguments['priority-normal'] = []
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2016-03-09 11:16:02 +00:00
|
|
|
response = self._request(method='post', json={'method': 'torrent-set', 'arguments': arguments})
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2016-03-09 11:16:02 +00:00
|
|
|
return 'success' == response.json().get('result', '')
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
api = TransmissionAPI()
|