#json-rpc #aria2 #websocket #api-bindings

aria2-ws

支持通知的 aria2 WebSocket JSONRPC API

7 个版本 (4 个重大更新)

0.5.0 2024年5月26日
0.4.0 2022年11月22日
0.3.0 2022年5月23日
0.2.2 2022年4月17日
0.1.0 2022年1月9日

#435 in 网络编程

Download history 153/week @ 2024-05-24 17/week @ 2024-05-31 6/week @ 2024-06-07 3/week @ 2024-06-14 3/week @ 2024-06-28 32/week @ 2024-07-05 151/week @ 2024-07-26 13/week @ 2024-08-02

每月下载量 164

MIT 许可证

53KB
1K SLoC

aria2-ws

Rust 编写的 aria2 WebSocket jsonrpc

使用 tokio 构建。

Docs.rs

aria2 RPC 文档

变更日志

功能

  • 方法和类型化响应
  • 自动重连
  • 确保在重新连接后执行 on_download_completeon_error 回调。
  • 通知订阅

示例

use std::sync::Arc;

use aria2_ws::{Client, Callbacks, TaskOptions};
use futures::FutureExt;
use serde_json::json;
use tokio::{spawn, sync::Semaphore};

async fn example() {
    let client = Client::connect("ws://127.0.0.1:6800/jsonrpc", None)
        .await
        .unwrap();
    let options = TaskOptions {
        split: Some(2),
        header: Some(vec!["Referer: https://www.pixiv.net/".to_string()]),
        // Add extra options which are not included in TaskOptions.
        extra_options: json!({"max-download-limit": "100K"})
            .as_object()
            .unwrap()
            .clone(),
        ..Default::default()
    };

    let mut not = client.subscribe_notifications();
    spawn(async move {
        loop {
            match not.recv().await {
                Ok(msg) => println!("Received notification {:?}", &msg),
                Err(broadcast::error::RecvError::Closed) => {
                    println!("Notification channel closed");
                    break;
                }
                Err(broadcast::error::RecvError::Lagged(_)) => {
                    println!("Notification channel lagged");
                }
            }
        }
    });

    // use `tokio::sync::Semaphore` to wait for all tasks to finish.
    let semaphore = Arc::new(Semaphore::new(0));
    client
        .add_uri(
            vec![
                "https://i.pximg.net/img-original/img/2020/05/15/06/56/03/81572512_p0.png"
                    .to_string(),
            ],
            Some(options.clone()),
            None,
            Some(Callbacks {
                on_download_complete: Some({
                    let s = semaphore.clone();
                    async move {
                        s.add_permits(1);
                        println!("Task 1 completed!");
                    }
                    .boxed()
                }),
                on_error: Some({
                    let s = semaphore.clone();
                    async move {
                        s.add_permits(1);
                        println!("Task 1 error!");
                    }
                    .boxed()
                }),
            }),
        )
        .await
        .unwrap();

    // Will 404
    client
        .add_uri(
            vec![
                "https://i.pximg.net/img-original/img/2022/01/05/23/32/16/95326322_p0.pngxxxx"
                    .to_string(),
            ],
            Some(options.clone()),
            None,
            Some(Callbacks {
                on_download_complete: Some({
                    let s = semaphore.clone();
                    async move {
                        s.add_permits(1);
                        println!("Task 2 completed!");
                    }
                    .boxed()
                }),
                on_error: Some({
                    let s = semaphore.clone();
                    async move {
                        s.add_permits(1);
                        println!("Task 2 error!");
                    }
                    .boxed()
                }),
            }),
        )
        .await
        .unwrap();

    // Wait for 2 tasks to finish.
    let _ = semaphore.acquire_many(2).await.unwrap();

    // Force shutdown aria2.
    // client.force_shutdown().await.unwrap();
}

依赖项

~6–8.5MB
~148K SLoC