```rust use std::num::{NonZeroU8, NonZeroUsize}; use std::path::PathBuf; use std::time::Duration;
use anyhow::Result; use tracing::info; use url::Url;
use httpdownloader::{ breakpointresume::DownloadBreakpointResumeExtension, HttpDownloaderBuilder, speedlimiter::DownloadSpeedLimiterExtension, speedtracker::DownloadSpeedTrackerExtension, statustracker::DownloadStatusTrackerExtension, }; use httpdownloader::bsonfilearchiver::{ArchiveFilePath, BsonFileArchiverBuilder};
async fn main() -> Result<()> { { tracing_subscriber::fmt::init(); }
let save_dir = PathBuf::from("C:/download");
let test_url = Url::parse("https://dldir1.qq.com/qqfile/qq/PCQQ9.6.9/QQ9.6.9.28878.exe")?;
let (downloader, (_status_state, _speed_state, speed_limiter, ..)) =
HttpDownloaderBuilder::new(test_url, save_dir)
.chunk_size(NonZeroUsize::new(1024 * 1024 * 10).unwrap())
.download_connection_count(NonZeroU8::new(3).unwrap())
.build((
DownloadStatusTrackerExtension { log: true },
DownloadSpeedTrackerExtension { log: true },
DownloadSpeedLimiterExtension {
byte_count_per: None
},
DownloadBreakpointResumeExtension {
download_archiver_builder: BsonFileArchiverBuilder::new(ArchiveFilePath::Suffix("bson".to_string()))
}
));
info!("Start download");
let finished_future = downloader.start().await?;
// Print download Progress
tokio::spawn({
let mut downloaded_len_receiver = downloader.downloaded_len_receiver().clone();
async move {
let total_len = downloader.total_size().await;
if let Some(total_len) = total_len {
info!("Total size: {:.2} Mb",total_len as f64 / 1024_f64/ 1024_f64);
}
while downloaded_len_receiver.changed().await.is_ok() {
let progress = *downloaded_len_receiver.borrow();
if let Some(total_len) = total_len {
info!("Download Progress: {} %,{}/{}",progress*100/total_len,progress,total_len);
}
tokio::time::sleep(Duration::from_millis(1000)).await;
}
}
});
// Download speed limit
tokio::spawn(async move {
tokio::time::sleep(Duration::from_secs(2)).await;
info!("Start speed limit");
speed_limiter.change_speed(Some(1024 * 1024 * 2)).await;
tokio::time::sleep(Duration::from_secs(4)).await;
info!("Remove the download speed limit");
speed_limiter.change_speed(None).await;
});
info!("Wait for download to end");
let dec = finished_future.await?;
info!("Downloading end cause: {:?}", dec);
Ok(())
} ```