ED_LRR/rust/src/download.rs

64 lines
2.0 KiB
Rust

use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use std::fs::File;
use std::io;
use std::path::PathBuf;
use structopt::StructOpt;
#[derive(Debug, StructOpt, Clone)]
pub struct DownloadOpts {
#[structopt(
long = "bodies",
default_value = "https://www.edsm.net/dump/bodies.json"
)]
/// Url to bodies.json
bodies_url: String,
#[structopt(
long = "systems",
default_value = "https://www.edsm.net/dump/systemsWithCoordinates.json"
)]
/// Url to systemsWithCoordinates.json
systems_url: String,
}
fn fetch_url(url: &str, prog_bar: &ProgressBar) -> std::io::Result<()> {
let outfile = url.split('/').last().unwrap();
let template = format!("{} {}", "[{elapsed_precise}] {binary_bytes}", outfile);
prog_bar.set_style(ProgressStyle::default_bar().template(&template));
let client = reqwest::Client::builder().gzip(true).build().unwrap();
let resp = client.get(url).send().unwrap();
let target_path = PathBuf::from(format!("dumps/{}", outfile));
if target_path.exists() {
eprintln!("Error: Target {} exists!", outfile);
return Ok(());
}
let mut target = File::create(target_path)?;
io::copy(&mut prog_bar.wrap_read(resp), &mut target).unwrap();
prog_bar.finish();
Ok(())
}
pub fn download(opts: DownloadOpts) -> std::io::Result<()> {
let mut threads = Vec::new();
let m = MultiProgress::new();
{
let opts = opts.clone();
let pb = m.add(ProgressBar::new(0));
threads.push(std::thread::spawn(move || {
fetch_url(&opts.bodies_url, &pb).unwrap();
}));
};
{
let opts = opts.clone();
let pb = m.add(ProgressBar::new(0));
threads.push(std::thread::spawn(move || {
fetch_url(&opts.systems_url, &pb).unwrap();
}));
}
m.join_and_clear().unwrap();
for th in threads {
th.join().unwrap();
}
Ok(())
}