Hello i'm using rust to do a scraper of images i have seen that the crate reqwest now can be done in async way but i have a problem with the client
i'm using a client because i have to download various images
Before i was using reqwest in the sync way and with a get to every html and img.
Now is 2020 and i want it to do it async and reusing the client.
The problem perhaps is simple for you but nor for me
when i have to download the image and put it in a file i'm doing:
async fn bajar_imagen(url_imagen: &str, directorio_capitulo: &Path, cliente: &reqwest::Client) {
// dbg!(&url_imagen);
let mut url = String::from("https:");
let url2 = url_imagen.to_string();
url.push_str(&url2);
let url = Url::parse(&url).expect("Fallo en la tranformación url de la imagen");
match cliente.get(url.as_ref()).send().await{
Ok(res) => {
match res.text().await {
Ok( doc1) => {
let vec: Vec<&str> = url_imagen.split('/').collect();
if vec.len() >= 9 {
let vec2: Vec<&str> = vec[8].split('?').collect();
// dbg!(vec2[0]);
if !vec2.is_empty() {
let new_file = directorio_capitulo.join(vec2[0]);
let mut file = File::create(new_file).expect("Fallo al crear fichero vacio");
io::copy(&mut doc1.as_bytes(), &mut file).expect("Fallo al copiar al fichero");
} else {
println!("{}", Red.bold().paint("Han cambiado urls de imagenes"));
}
} else {
println!("{}", Red.bold().paint("Han cambiado urls de imagenes"));
}
}
Err(e) => {
println!(
"{}, {}",
Yellow
.bold()
.paint("Error al bajar imagen mirar status, borrar directorio"),
e
);
borrar_directorio_y_panic(&directorio_capitulo);
}
}
}
Err(e) => {
println!(
"{}, {}",
Yellow
.bold()
.paint("Error al bajar imagen mirar status, borrar directorio"),
e
);
borrar_directorio_y_panic(&directorio_capitulo);
}
}
}
i'm not sure why the file is not a good jpg. i know that the url is correct, but i don't know how to transform
this in to a file.
io::copy(&mut doc1.as_bytes(), &mut file).expect("Fallo al copiar al fichero");
Any help?