2014-03-10 01:18:05 -04:00
|
|
|
# Author: Mr_Orange <mr_orange@hotmail.it>
|
|
|
|
# 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 json
|
|
|
|
from base64 import b64encode
|
|
|
|
|
|
|
|
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 TransmissionAPI(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(TransmissionAPI, self).__init__('Transmission', host, username, password)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
self.url = self.host + 'transmission/rpc'
|
|
|
|
|
|
|
|
def _get_auth(self):
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
post_data = json.dumps({'method': 'session-get', })
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
try:
|
2014-07-24 14:16:59 -04:00
|
|
|
self.response = self.session.post(self.url, data=post_data.encode('utf-8'), verify=sickbeard.TORRENT_VERIFY_CERT)
|
2014-03-10 01:18:05 -04:00
|
|
|
self.auth = re.search('X-Transmission-Session-Id:\s*(\w+)', self.response.text).group(1)
|
|
|
|
except:
|
2014-03-25 01:57:24 -04:00
|
|
|
return None
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
self.session.headers.update({'x-transmission-session-id': self.auth})
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
#Validating Transmission authorization
|
|
|
|
post_data = json.dumps({'arguments': {},
|
|
|
|
'method': 'session-get',
|
2014-03-25 01:57:24 -04:00
|
|
|
})
|
|
|
|
self._request(method='post', data=post_data)
|
|
|
|
|
|
|
|
return self.auth
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
def _add_torrent_uri(self, result):
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
arguments = {'filename': result.url,
|
|
|
|
'paused': 1 if sickbeard.TORRENT_PAUSED else 0,
|
|
|
|
'download-dir': sickbeard.TORRENT_PATH
|
|
|
|
}
|
|
|
|
post_data = json.dumps({'arguments': arguments,
|
|
|
|
'method': 'torrent-add',
|
|
|
|
})
|
2014-03-10 01:18:05 -04:00
|
|
|
self._request(method='post', data=post_data)
|
|
|
|
|
|
|
|
return self.response.json()['result'] == "success"
|
|
|
|
|
|
|
|
def _add_torrent_file(self, result):
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
arguments = {'metainfo': b64encode(result.content),
|
|
|
|
'paused': 1 if sickbeard.TORRENT_PAUSED else 0,
|
|
|
|
'download-dir': sickbeard.TORRENT_PATH
|
|
|
|
}
|
2014-03-10 01:18:05 -04:00
|
|
|
post_data = json.dumps({'arguments': arguments,
|
|
|
|
'method': 'torrent-add',
|
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 self.response.json()['result'] == "success"
|
|
|
|
|
|
|
|
def _set_torrent_ratio(self, result):
|
2014-03-25 01:57:24 -04:00
|
|
|
|
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
|
|
|
mode = 0
|
|
|
|
if ratio:
|
2014-06-12 17:02:50 -04:00
|
|
|
if float(ratio) == -1:
|
2014-05-23 02:42:11 -04:00
|
|
|
ratio = 0
|
|
|
|
mode = 2
|
2014-06-12 17:02:50 -04:00
|
|
|
elif float(ratio) >= 0:
|
2014-05-23 02:42:11 -04:00
|
|
|
ratio = float(ratio)
|
|
|
|
mode = 1 # Stop seeding at seedRatioLimit
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-08-24 23:49:00 -04:00
|
|
|
arguments = {'ids': [result.hash],
|
2014-03-25 01:57:24 -04:00
|
|
|
'seedRatioLimit': ratio,
|
|
|
|
'seedRatioMode': mode
|
|
|
|
}
|
2014-03-10 01:18:05 -04:00
|
|
|
post_data = json.dumps({'arguments': arguments,
|
|
|
|
'method': 'torrent-set',
|
2014-03-25 01:57:24 -04:00
|
|
|
})
|
|
|
|
self._request(method='post', data=post_data)
|
|
|
|
|
|
|
|
return self.response.json()['result'] == "success"
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-08-31 02:41:53 -04:00
|
|
|
def _set_torrent_seed_time(self, result):
|
|
|
|
|
|
|
|
if sickbeard.TORRENT_SEED_TIME:
|
2014-09-24 20:47:11 -04:00
|
|
|
time = int(60 * float(sickbeard.TORRENT_SEED_TIME))
|
2014-08-31 02:41:53 -04:00
|
|
|
arguments = {'ids': [result.hash],
|
|
|
|
'seedIdleLimit': time,
|
|
|
|
'seedIdleMode': 1
|
|
|
|
}
|
|
|
|
|
|
|
|
post_data = json.dumps({'arguments': arguments,
|
|
|
|
'method': 'torrent-set',
|
|
|
|
})
|
|
|
|
self._request(method='post', data=post_data)
|
|
|
|
|
|
|
|
return self.response.json()['result'] == "success"
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def _set_torrent_priority(self, result):
|
|
|
|
|
2014-08-24 23:49:00 -04:00
|
|
|
arguments = {'ids': [result.hash]}
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
if result.priority == -1:
|
|
|
|
arguments['priority-low'] = []
|
|
|
|
elif result.priority == 1:
|
|
|
|
# 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 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
post_data = json.dumps({'arguments': arguments,
|
|
|
|
'method': 'torrent-set',
|
2014-03-25 01:57:24 -04:00
|
|
|
})
|
|
|
|
self._request(method='post', data=post_data)
|
|
|
|
|
|
|
|
return self.response.json()['result'] == "success"
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
api = TransmissionAPI()
|