Skip to content

Instantly share code, notes, and snippets.

@jmlemetayer
Created April 10, 2016 13:21
Show Gist options
  • Save jmlemetayer/3bbf652c3503215b3a9f2c9ffaf87306 to your computer and use it in GitHub Desktop.
Save jmlemetayer/3bbf652c3503215b3a9f2c9ffaf87306 to your computer and use it in GitHub Desktop.
Transmission to Synology Download Station on complete script
#!/usr/bin/env python
# -*- coding: utf8 -*-
import json
import os
import requests
import urllib2
import urlparse
class SynoAPI:
def __init__(self, url, account, passwd):
self.url = url
self.account = account
self.passwd = passwd
self.session = 'DownloadStation'
self.sid = None
def request(self, api, method, path, version, data={}):
url = self.url + '/webapi/' + path
data['api'] = api
data['method'] = method
data['version'] = version
if self.sid != None:
data['_sid'] = self.sid
response = requests.post(url, data, timeout=10)
ret = json.loads(response.text)
if 'success' in ret:
if ret['success'] == False:
if 'error' in ret and 'code' in ret['error']:
raise NameError('Error code: %d' % ret['error']['code'])
elif 'data' in ret:
return ret['data']
else:
return {}
else:
raise NameError('Error: empty response')
def info(self, api):
ret = self.request('SYNO.API.Info', 'query', \
'query.cgi', 1, {'query': api})
return ret[api]
def requestapi(self, api, method, data={}):
i = self.info(api)
return self.request(api, method, i['path'], i['maxVersion'], data)
def login(self):
data = {'account': self.account, \
'passwd': self.passwd, \
'session': self.session, \
'format': 'sid'}
ret = self.requestapi('SYNO.API.Auth', 'login', data)
self.sid = ret['sid']
def logout(self):
data = {'session': self.session}
ret = self.requestapi('SYNO.API.Auth', 'logout', data)
self.sid = None
def file_mkdir(self, name):
data = {'folder_path': '/' + self.download_destination, \
'name': name.decode('utf8').encode('utf8'), \
'force_parent': True}
self.requestapi('SYNO.FileStation.CreateFolder', 'create', data)
def download_config(self, uri, username, password, destination):
self.download_uri = uri
self.download_username = username
self.download_password = password
self.download_destination = destination
def download_create(self, filename):
dir = os.path.dirname(filename)
if not dir:
destination = self.download_destination
else:
self.file_mkdir(dir)
destination = os.path.join(self.download_destination, dir)
uri = urllib2.quote(filename.decode('utf8').encode('utf8'))
uri = urlparse.urljoin(self.download_uri + '/', uri)
data = {'uri': uri, \
'username': self.download_username, \
'password': self.download_password, \
'destination': destination}
self.requestapi('SYNO.DownloadStation.Task', 'create', data)
class TransmissionAPI:
def __init__(self):
self.dir = os.getenv('TR_TORRENT_DIR')
self.name = os.getenv('TR_TORRENT_NAME')
def foreachfile(self, callback):
path = os.path.join(self.dir, self.name)
if os.path.isfile(path):
callback(os.path.relpath(path, self.dir))
elif os.path.isdir(path):
for root, dirs, files in os.walk(path):
for file in files:
callback(os.path.relpath(os.path.join(root, file),self.dir))
else:
raise NameError('Not a regular file %s' % path)
t = TransmissionAPI()
s = SynoAPI('https://my.syno.url', \
'username', \
'password')
s.download_config('https://my.downloads.url', \
'username', \
'password', \
'destination')
s.login()
t.foreachfile(s.download_create)
s.logout()
@jmlemetayer
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment