use std::time::Duration;
use anyhow::Context;
use librqbit::{AddTorrent, AddTorrentOptions, AddTorrentResponse, Session};
use tracing::info;
const MAGNET_LINK: &str = "magnet:?xt=urn:btih:cab507494d02ebb1178b38f2e9d7be299c86b862";
#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
match std::env::var("RUST_LOG") {
Ok(_) => {}
Err(_) => std::env::set_var("RUST_LOG", "info"),
}
tracing_subscriber::fmt::init();
let output_dir = std::env::args()
.nth(1)
.expect("the first argument should be the output directory");
let session = Session::new(output_dir.into())
.await
.context("error creating session")?;
let handle = match session
.add_torrent(
AddTorrent::from_url(MAGNET_LINK),
Some(AddTorrentOptions {
overwrite: true,
..Default::default()
}),
)
.await
.context("error adding torrent")?
{
AddTorrentResponse::Added(_, handle) => handle,
_ => unreachable!(),
};
handle.with_metadata(|r| {
info!("Details: {:?}", &r.info);
})?;
tokio::spawn({
let handle = handle.clone();
async move {
loop {
tokio::time::sleep(Duration::from_secs(1)).await;
let stats = handle.stats();
info!("{stats:}");
}
}
});
handle.wait_until_completed().await?;
info!("torrent downloaded");
Ok(())
}