[英]Any example of aria2 notifications listening like onDownloadComplete, onDownloadError or similar?
I am trying to orchestrate a downloading of files with aria2 tool.我正在尝试使用aria2工具编排文件下载。 There is an example in the documentation like:文档中有一个例子,如:
import urllib2, json
jsonreq = json.dumps({'jsonrpc':'2.0', 'id':'qwer',
'method':'aria2.addUri',
'params':[['http://example.org/file']]})
c = urllib2.urlopen('http://localhost:6800/jsonrpc', jsonreq)
c.read()
# The result:
# '{"id":"qwer","jsonrpc":"2.0","result":"2089b05ecca3d829"}'
It starts download of http://example.org/file
and returns some GID (download ID) 2089b05ecca3d829
...它开始下载http://example.org/file
并返回一些 GID(下载 ID) 2089b05ecca3d829
...
Aria2 supports notifications. Aria2 支持通知。 But no any examples how to get a notification, for example, onDownloadComplete , onDownloadError , etc. I assume, there is a way to request the aria2 to call me through JSON-RPC (HTTP) on some (my) IP and port.但是没有任何示例如何获取通知,例如onDownloadComplete 、 onDownloadError等。我假设有一种方法可以请求 aria2 在某些(我的)IP 和端口上通过 JSON-RPC (HTTP) 呼叫我。 But I cannot find a way how to request aria2 to do it (how to subscribe to the notifications with my IP, port).但是我找不到如何请求 aria2 执行此操作的方法(如何使用我的 IP、端口订阅通知)。 Any example in Python, Ruby or similar will be very helpful. Python、Ruby 或类似的任何示例都会非常有帮助。
Refered to aria2 docs You should use websocket to communicate with RPC server:参考 aria2 docs你应该使用 websocket 与 RPC 服务器进行通信:
import websocket
from pprint import pprint
ws_server = "ws://localhost:6800/jsonrpc"
socket = websocket.create_connection(ws_server)
while 1:
message = socket.recv()
pprint(message)
time.sleep(3)
socket.close()
after you get the message (json object), you'll get the onDownloadComplete event, then you can do whatever you like.收到消息(json 对象)后,您将收到 onDownloadComplete 事件,然后您可以为所欲为。 I suggest you use ariap (above @RandomB Mentioned it), https://pawamoy.github.io/aria2p/reference/api/#aria2p.api.API.listen_to_notifications我建议你使用 ariap(上面@RandomB 提到过), https: //pawamoy.github.io/aria2p/reference/api/#aria2p.api.API.listen_to_notifications
set notification with a callback.使用回调设置通知。
......
def start_listen(self):
self.api.listen_to_notifications(
threaded=True,
on_download_complete=self.done,
timeout=1,
handle_signals=False
)
def done(self, api, gid):
down = api.get_download(gid)
dataMedia = str(down.files[0].path)
def stop_listen(self):
while len(self.links) > 0:
gids = list(self.links.keys())
for down in self.api.get_downloads(gids):
if down.status in ['waiting', 'active']:
time.sleep(5)
break
else:
del self.links[down.gid]
if down.status in ['complete']:
#print(f'>>>>>{down.gid} Completed')
pass
else:
print(Back.RED + f'{down.gid}|{down.files[0]}|{down.status}|error msg: {down.error_message}', end = '')
print(Style.RESET_ALL)
print('all finished!!!!')
self.api.stop_listening()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.