You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.6 KiB

from couchpotato.core.downloaders.base import Downloader
from couchpotato.core.helpers.encoding import isInt
from couchpotato.core.logger import CPLog
import httplib
import json
import urllib
13 years ago
import urllib2
log = CPLog(__name__)
class Synology(Downloader):
type = ['torrent', 'torrent_magnet']
log = CPLog(__name__)
def download(self, data, movie, filedata = None):
log.error('Sending "%s" (%s) to Synology.', (data.get('name'), data.get('type')))
# Load host from config and split out port.
host = self.conf('host').split(':')
if not isInt(host[1]):
log.error('Config properties are not filled in correctly, port is missing.')
return False
try:
# Send request to Transmission
srpc = SynologyRPC(host[0], host[1], self.conf('username'), self.conf('password'))
remote_torrent = srpc.add_torrent_uri(data.get('url'))
13 years ago
log.info('Response: %s', remote_torrent)
return remote_torrent['success']
except Exception, err:
13 years ago
log.error('Exception while adding torrent: %s', err)
return False
class SynologyRPC(object):
13 years ago
'''SynologyRPC lite library'''
def __init__(self, host = 'localhost', port = 5000, username = None, password = None):
super(SynologyRPC, self).__init__()
13 years ago
self.download_url = 'http://%s:%s/webapi/DownloadStation/task.cgi' % (host, port)
self.auth_url = 'http://%s:%s/webapi/auth.cgi' % (host, port)
self.username = username
self.password = password
13 years ago
self.session_name = 'DownloadStation'
def _login(self):
if self.username and self.password:
13 years ago
args = {'api': 'SYNO.API.Auth', 'account': self.username, 'passwd': self.password, 'version': 2,
'method': 'login', 'session': self.session_name, 'format': 'sid'}
13 years ago
response = self._req(self.auth_url, args)
if response['success'] == True:
self.sid = response['data']['sid']
13 years ago
log.debug('Sid=%s', self.sid)
return response
13 years ago
elif self.username or self.password:
log.error('User or password missing, not using authentication.')
return False
def _logout(self):
args = {'api':'SYNO.API.Auth', 'version':1, 'method':'logout', 'session':self.session_name, '_sid':self.sid}
13 years ago
return self._req(self.auth_url, args)
def _req(self, url, args):
req = urllib2.Request(url, urllib.urlencode(args))
try:
req_open = urllib2.urlopen(req)
response = json.loads(req_open.read())
if response['success'] == True:
log.info('Synology action successfull')
return response
except httplib.InvalidURL, err:
log.error('Invalid Synology host, check your config %s', err)
return False
except urllib2.HTTPError, err:
log.error('SynologyRPC HTTPError: %s', err)
return False
except urllib2.URLError, err:
log.error('Unable to connect to Synology %s', err)
return False
def add_torrent_uri(self, torrent):
13 years ago
log.info('Adding torrent URL %s', torrent)
response = {}
# login
login = self._login()
if login and login['success'] == True:
13 years ago
log.info('Login success, adding torrent')
args = {'api':'SYNO.DownloadStation.Task', 'version':1, 'method':'create', 'uri':torrent, '_sid':self.sid}
13 years ago
response = self._req(self.download_url, args)
self._logout()
else:
13 years ago
log.error('Couldn\'t login to Synology, %s', login)
return response