簡體   English   中英

有沒有像 onDownloadComplete、onDownloadError 或類似的 aria2 通知監聽的例子?

[英]Any example of aria2 notifications listening like onDownloadComplete, onDownloadError or similar?

我正在嘗試使用aria2工具編排文件下載。 文檔中有一個例子,如:

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"}'

它開始下載http://example.org/file並返回一些 GID(下載 ID) 2089b05ecca3d829 ...

Aria2 支持通知。 但是沒有任何示例如何獲取通知,例如onDownloadCompleteonDownloadError等。我假設有一種方法可以請求 aria2 在某些(我的)IP 和端口上通過 JSON-RPC (HTTP) 呼叫我。 但是我找不到如何請求 aria2 執行此操作的方法(如何使用我的 IP、端口訂閱通知)。 Python、Ruby 或類似的任何示例都會非常有幫助。

參考 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()

收到消息(json 對象)后,您將收到 onDownloadComplete 事件,然后您可以為所欲為。 我建議你使用 ariap(上面@RandomB 提到過), https: //pawamoy.github.io/aria2p/reference/api/#aria2p.api.API.listen_to_notifications

使用回調設置通知。

 ......
 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM