You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
2.1 KiB
89 lines
2.1 KiB
use super::*;
|
|
use std::{
|
|
path::Path,
|
|
};
|
|
use tokio::{
|
|
fs::{
|
|
OpenOptions,
|
|
},
|
|
prelude::*,
|
|
stream::StreamExt,
|
|
};
|
|
use termprogress::{
|
|
Display, ProgressBar, Spinner, progress, spinner,
|
|
};
|
|
|
|
//TODO: Create a progress task module for atomically printing infos and stuff
|
|
//TODO: Create a module for temp files, pass the temp file to `perform` and do the regex fixing after `perform`
|
|
|
|
/// Download a loli async
|
|
pub async fn perform(url: impl AsRef<str>, path: impl AsRef<Path>) -> Result<loli::Loli, error::Error>
|
|
{
|
|
let url = url.as_ref();
|
|
let path = path.as_ref();
|
|
|
|
let mut progress = spinner::Spin::with_title("Starting request...", Default::default());
|
|
progress.refresh();
|
|
let mut resp = reqwest::get(url).await?;
|
|
progress.bump();
|
|
progress.set_title(&format!("Starting download to {:?}...", path));
|
|
let mut file = OpenOptions::new()
|
|
.create(true)
|
|
.truncate(true)
|
|
.write(true)
|
|
.open(path).await?;
|
|
|
|
let len = resp.content_length();
|
|
let mut version_inc = || {
|
|
progress.bump();
|
|
};
|
|
version_inc();
|
|
|
|
let mut bytes = resp.bytes_stream();
|
|
while let Some(buffer) = bytes.next().await {
|
|
file.write(buffer?.as_ref()).await?;
|
|
version_inc();
|
|
}
|
|
|
|
progress.complete_with("OK");
|
|
|
|
loop{}
|
|
}
|
|
|
|
pub async fn work(conf: config::Config) -> Result<(), Box<dyn std::error::Error>>
|
|
{
|
|
let rating = conf.rating;
|
|
let mut children = Vec::new();
|
|
|
|
for path in conf.output.into_iter()
|
|
{
|
|
let url = url::parse(&rating);
|
|
children.push(tokio::task::spawn(async move {
|
|
println!("Starting download ({})...", url);
|
|
let path = match path {
|
|
config::OutputType::File(file) => file,
|
|
config::OutputType::Directory(dir) => {
|
|
//TODO: Implement downloading to temp and renaming to hash
|
|
unimplemented!();
|
|
},
|
|
};
|
|
match perform(&url, &path).await {
|
|
Err(e) => panic!("Failed downloading {} -> {:?}: {}", url, path, e),
|
|
Ok(v) => v,
|
|
}
|
|
}));
|
|
}
|
|
|
|
for child in children.into_iter()
|
|
{
|
|
match child.await {
|
|
Ok(v) => (),
|
|
Err(err) => {
|
|
println!("Child failed: {}", err);
|
|
},
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|