简体   繁体   English

有没有像 onDownloadComplete、onDownloadError 或类似的 aria2 通知监听的例子?

[英]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.但是没有任何示例如何获取通知,例如onDownloadCompleteonDownloadError等。我假设有一种方法可以请求 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.

相关问题 使用aria2的RPC接口时如何配置超时? - How to config timeout when using the RPC interface of aria2? 无法使用 Python xmlrpc 在 aria2 中添加选项? - Can not add options in aria2 using Python xmlrpc? 如何在我的 python GUI 程序中使用 aria2? - How to use aria2 in my python GUI program? 获取Aria2(aria2c)以继续下载通过RPC添加的文件 - Getting Aria2 (aria2c) to resume download of files added via RPC MATLAB 中是否有类似 enumerate() 的类似函数 - Is there any similar function in MATLAB like enumerate() Spark RDD中是否有类似sql中的“ like”功能的类似功能? - Is there any similar function in spark RDD like the 'like' function in sql? 有什么方法可以在python中访问$ var之类的变量(类似于shell脚本)? - Is there any way to access variable like $var (similar to shell scripting) in python? python 中的 np.where() 在 gocv 中有没有类似的函数? - Is there any similar function in gocv like np.where() in python? pytorch 中的张量有没有类似 df.mask 的函数? - any similar function like df.mask for tensor in pytorch? python 中是否有任何类似的 function 极性,例如 pandas 中的“转换”? - is there any similar function in python poloars like 'transform' in pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM