-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy patharia2c.py
148 lines (125 loc) · 4.46 KB
/
aria2c.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import json
import requests
class Aria2c:
'''
Example :
client = Aria2c('localhost', '6800')
# print server version
print(client.getVer())
# add a task to server
client.addUri('http://example.com/file.iso')
# provide addtional options
option = {"out": "new_file_name.iso"}
client.addUri('http://example.com/file.iso', option)
'''
IDPREFIX = "nbfs"
ADD_URI = 'aria2.addUri'
GET_VER = 'aria2.getVersion'
STOPPED = 'aria2.tellStopped'
ACTIVE = 'aria2.tellActive'
STATUS = 'aria2.tellStatus'
def __init__(self, host, port, token=None):
self.host = host
self.port = port
self.token = token
self.serverUrl = "http://{host}:{port}/jsonrpc".format(**locals())
def _genPayload(self, method, uris=None, options=None, cid=None, IDPREFIX=None):
cid = IDPREFIX + cid if cid else Aria2c.IDPREFIX
p = {
'jsonrpc': '2.0',
'id': cid,
'method': method,
'params': []
}
if self.token:
p['params'].append("token:" + self.token)
if uris:
p['params'].append(uris)
if options:
p['params'].append(options)
return p
@staticmethod
def _defaultErrorHandler(code, message):
print("ERROR: {}, {}".format(code, message))
return None
def _post(self, action, params, onSuc, onFail=None):
if onFail is None:
onFail = Aria2c._defaultErrorHandler
payloads = self._genPayload(action, *params)
resp = requests.post(self.serverUrl, data=json.dumps(payloads))
result = resp.json()
if "error" in result:
return onFail(result["error"]["code"], result["error"]["message"])
else:
return onSuc(resp)
def addUri(self, uri, options=None):
def success(response):
return response.text
return self._post(Aria2c.ADD_URI, [[uri, ], options], success)
def getVer(self):
def success(response):
return response.json()['result']['version']
return self._post(Aria2c.GET_VER, [], success)
def getStopped(self, offset, number):
def success(response):
return response.json()
return self._post(Aria2c.STOPPED, [offset, number], success)
def getActive(self, offset, number):
def success(response):
return response.json()
return self._post(Aria2c.ACTIVE, [offset, number], success)
# {
# "bitfield":"80",
# "completedLength":"100352",
# "connections":"0",
# "dir":"/home/chi/aria2/download",
# "downloadSpeed":"0",
# "errorCode":"0",
# "errorMessage":"",
# "files":[
# {
# "completedLength":"100352",
# "index":"1",
# "length":"100352",
# "path":"/home/chi/aria2/download/file-sample_100kB.doc",
# "selected":"true",
# "uris":[
# {
# "status":"used",
# "uri":"https://file-examples-com.github.io/uploads/2017/02/file-sample_100kB.doc"
# },
# {
# "status":"waiting",
# "uri":"https://file-examples-com.github.io/uploads/2017/02/file-sample_100kB.doc"
# },
# {
# "status":"waiting",
# "uri":"https://file-examples-com.github.io/uploads/2017/02/file-sample_100kB.doc"
# },
# {
# "status":"waiting",
# "uri":"https://file-examples-com.github.io/uploads/2017/02/file-sample_100kB.doc"
# },
# {
# "status":"waiting",
# "uri":"https://file-examples-com.github.io/uploads/2017/02/file-sample_100kB.doc"
# },
# {
# "status":"waiting",
# "uri":"https://file-examples-com.github.io/uploads/2017/02/file-sample_100kB.doc"
# }
# ]
# }
# ],
# "gid":"614a8c08af12cdfe",
# "numPieces":"1",
# "pieceLength":"1048576",
# "status":"complete",
# "totalLength":"100352",
# "uploadLength":"0",
# "uploadSpeed":"0"
# }
def getStatus(self, gid: str):
def success(response):
return response.json()
return self._post(Aria2c.STATUS, [gid], success)