Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/comfyfluffy/aria2-ws-rs
aria2 websocket jsonrpc bindings in Rust.
https://github.com/comfyfluffy/aria2-ws-rs
aria2 aria2-rpc rust
Last synced: about 1 month ago
JSON representation
aria2 websocket jsonrpc bindings in Rust.
- Host: GitHub
- URL: https://github.com/comfyfluffy/aria2-ws-rs
- Owner: ComfyFluffy
- License: mit
- Created: 2022-01-09T04:27:57.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2022-11-17T12:43:30.000Z (about 2 years ago)
- Last Synced: 2024-03-15T12:27:07.912Z (10 months ago)
- Topics: aria2, aria2-rpc, rust
- Language: Rust
- Homepage:
- Size: 93.8 KB
- Stars: 15
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# aria2-ws
An aria2 websocket jsonrpc in Rust.
Built with `tokio`.
[Docs.rs](https://docs.rs/aria2_ws)
[aria2 RPC docs](https://aria2.github.io/manual/en/html/aria2c.html#methods)
[Changelog](./CHANGELOG.md)
## Features
- Methods and typed responses
- Auto reconnect
- Ensures `on_download_complete` and `on_error` callback to be executed even after reconnected.
- Notification subscription## Example
```rust
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();
}```