Created
April 3, 2023 19:48
-
-
Save Roshan-R/8bd44d93e47f409614a5d1574cd16cb8 to your computer and use it in GitHub Desktop.
Download file using ureq with progress bar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use indicatif::{ProgressBar, ProgressStyle}; | |
use std::fs::File; | |
use std::io; | |
const DOWNLOAD_TEMPLATE: &str = | |
"{msg} {spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({eta})"; | |
pub fn download_file(url: &str) -> io::Result<()> { | |
let resp = ureq::get(&url).call().unwrap(); | |
let length: u64 = resp.header("Content-Length").unwrap().parse().unwrap(); | |
let bar = ProgressBar::new(!0); | |
bar.set_message("Downloading"); | |
bar.set_style( | |
ProgressStyle::with_template(DOWNLOAD_TEMPLATE) | |
.unwrap() | |
.progress_chars("##-"), | |
); | |
bar.set_length(length); | |
let mut f = File::create("video.mp4").unwrap(); | |
io::copy(&mut bar.wrap_read(resp.into_reader()), &mut f).unwrap(); | |
Ok(()) | |
} | |
fn main() { | |
let url = | |
"https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4"; | |
download_file(url).unwrap(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment