Add examples
This commit is contained in:
parent
adb98a2d89
commit
2ebbc0a828
9 changed files with 541 additions and 30 deletions
|
|
@ -35,3 +35,4 @@ clone_to_owned = {path="../clone_to_owned", package="librqbit-clone-to-owned", v
|
|||
librqbit-core = {path="../librqbit_core", version = "2.2.1"}
|
||||
|
||||
[dev-dependencies]
|
||||
tracing-subscriber = "0.3"
|
||||
|
|
@ -8,6 +8,8 @@ use tracing::info;
|
|||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let info_hash = Id20::from_str("64a980abe6e448226bb930ba061592e44c3781a1").unwrap();
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let dht = Dht::new().await.context("error initializing DHT")?;
|
||||
let mut stream = dht.get_peers(info_hash).await?;
|
||||
|
||||
|
|
@ -61,3 +61,4 @@ dashmap = "5.5.3"
|
|||
|
||||
[dev-dependencies]
|
||||
futures = {version = "0.3"}
|
||||
tracing-subscriber = "0.3"
|
||||
|
|
@ -3,38 +3,10 @@
|
|||
A torrent library 100% written in rust
|
||||
|
||||
## Basic example
|
||||
See ```examples``` folder.
|
||||
This is a simple program on how to use this library
|
||||
This program will just download a simple torrent file with a Magnet link
|
||||
|
||||
```rust
|
||||
use std::error::Error;
|
||||
use std::path::PathBuf;
|
||||
use librqbit::session::{AddTorrentResponse, Session};
|
||||
use librqbit::spawn_utils::BlockingSpawner;
|
||||
|
||||
const MAGNET_LINK: &str = "magnet:?..."; // Put your magnet link here
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>>{
|
||||
|
||||
// Create the session
|
||||
let session = Session::new("C:\\Anime".parse().unwrap(), BlockingSpawner::new(false)).await?;
|
||||
|
||||
// Add the torrent to the session
|
||||
let handle = match session.add_torrent(MAGNET_LINK, None).await {
|
||||
Ok(AddTorrentResponse::Added(handle)) => {
|
||||
Ok(handle)
|
||||
},
|
||||
Err(e) => {
|
||||
eprintln!("Something goes wrong when downloading torrent : {:?}", e);
|
||||
Err(())
|
||||
}
|
||||
_ => Err(())
|
||||
}.expect("Failed to add torrent to the session");
|
||||
|
||||
// Wait until the download is completed
|
||||
handle.wait_until_completed().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
```
|
||||
69
crates/librqbit/examples/ubuntu.rs
Normal file
69
crates/librqbit/examples/ubuntu.rs
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
// For production-grade code look at rqbit::main(), which does the same but has more options.
|
||||
//
|
||||
// Usage:
|
||||
// cargo run --release --example ubuntu /tmp/ubuntu/
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::Context;
|
||||
use librqbit::session::{AddTorrentOptions, AddTorrentResponse, Session};
|
||||
use tracing::info;
|
||||
|
||||
// This is ubuntu-21.04-live-server-amd64.iso.torrent
|
||||
// You can also pass filenames and URLs to add_torrent().
|
||||
const MAGNET_LINK: &str = "magnet:?xt=urn:btih:cab507494d02ebb1178b38f2e9d7be299c86b862";
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), anyhow::Error> {
|
||||
// Output logs to console.
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let output_dir = std::env::args()
|
||||
.nth(1)
|
||||
.expect("the first argument should be the output directory");
|
||||
|
||||
// Create the session
|
||||
let session = Session::new(output_dir.into(), Default::default())
|
||||
.await
|
||||
.context("error creating session")?;
|
||||
|
||||
// Add the torrent to the session
|
||||
let handle = match session
|
||||
.add_torrent(
|
||||
MAGNET_LINK,
|
||||
Some(AddTorrentOptions {
|
||||
// Set this to true to allow writing on top of existing files.
|
||||
// If the file is partially downloaded, librqbit will only download the
|
||||
// missing pieces.
|
||||
//
|
||||
// Otherwise it will throw an error that the file exists.
|
||||
overwrite: false,
|
||||
..Default::default()
|
||||
}),
|
||||
)
|
||||
.await
|
||||
.context("error adding torrent")?
|
||||
{
|
||||
AddTorrentResponse::Added(handle) => handle,
|
||||
// For a brand new session other variants won't happen.
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
// Print stats periodically.
|
||||
tokio::spawn({
|
||||
let handle = handle.clone();
|
||||
async move {
|
||||
loop {
|
||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
let stats = handle.torrent_state().stats_snapshot();
|
||||
info!("stats: {stats:?}");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Wait until the download is completed
|
||||
handle.wait_until_completed().await?;
|
||||
info!("torrent downloaded");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -251,7 +251,10 @@ impl Session {
|
|||
torrent_from_file(url)?
|
||||
};
|
||||
let dht_rx = match self.dht.as_ref() {
|
||||
Some(dht) => Some(dht.get_peers(torrent.info_hash).await?),
|
||||
Some(dht) => {
|
||||
debug!("reading peers for {:?} from DHT", torrent.info_hash);
|
||||
Some(dht.get_peers(torrent.info_hash).await?)
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
let trackers = torrent
|
||||
|
|
|
|||
|
|
@ -38,3 +38,14 @@ impl BlockingSpawner {
|
|||
f()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for BlockingSpawner {
|
||||
fn default() -> Self {
|
||||
let allow_block_in_place = match tokio::runtime::Handle::current().runtime_flavor() {
|
||||
tokio::runtime::RuntimeFlavor::CurrentThread => false,
|
||||
tokio::runtime::RuntimeFlavor::MultiThread => true,
|
||||
_ => true,
|
||||
};
|
||||
Self::new(allow_block_in_place)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue