-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathpyaria2.py
53 lines (43 loc) · 1.5 KB
/
pyaria2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
# coding=utf-8
class Jsonrpc(object):
MUTI_METHOD = 'system.multicall'
ADDURI_METHOD = 'aria2.addUri'
def __init__(self, host, port, token=None):
self._idCount = 0
self.host = host
self.port = port
self.serverUrl = "http://{host}:{port}/jsonrpc".format(**locals())
def _genParams(self, method , uris=None, options=None, cid=None):
p = {
'jsonrpc': '2.0',
'id': self._idCount,
'method': method,
'test': 'test',
'params': []
}
if uris:
p['params'].append(uris)
if options:
p['params'].append(options)
return p
def _post(self, action, params, onSuccess, onFail=None):
import requests
import json
if onFail is None:
onFail = Jsonrpc._defaultErrorHandle
paramsObject = self._genParams(action, *params)
resp = requests.post(self.serverUrl, data=json.dumps(paramsObject))
result = resp.json()
if "error" in result:
return onFail(result["error"]["code"], result["error"]["message"])
else:
return onSuccess(resp)
def addUris(self, uri, options=None):
def success(response):
return response.text
return self._post(Jsonrpc.ADDURI_METHOD, [[uri,], options], success)
@staticmethod
def _defaultErrorHandle(code, message):
print ("ERROR: {},{}".format(code, message))
return None