2020-09-12 12:26:07 +00:00
|
|
|
use crate::structs;
|
|
|
|
|
|
|
|
use std::env;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Write;
|
|
|
|
use tokio::stream::StreamExt;
|
|
|
|
extern crate serde_json;
|
|
|
|
extern crate reqwest;
|
|
|
|
|
2020-12-02 15:34:42 +00:00
|
|
|
pub async fn get_sauce_info(client: reqwest::Client, sauce: i32) -> Result<structs::GalleryInfo, reqwest::Error> {
|
2020-09-12 12:26:07 +00:00
|
|
|
let mut uri = String::from("https://nhentai.net/api/gallery/");
|
|
|
|
uri.push_str(&sauce.to_string());
|
|
|
|
let resp = client.get(&uri)
|
|
|
|
.send()
|
|
|
|
.await?;
|
2020-12-02 16:00:36 +00:00
|
|
|
Ok(serde_json::from_str(&resp.text().await?).unwrap())
|
2020-09-12 12:26:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_search_info(client: reqwest::Client, search_query: &str) -> Result<structs::SearchInfo, reqwest::Error> {
|
|
|
|
let uri = "https://nhentai.net/api/galleries/search";
|
|
|
|
let resp = client.get(uri)
|
|
|
|
.query(&[("query", search_query)])
|
|
|
|
.send()
|
|
|
|
.await?;
|
2020-12-02 16:00:36 +00:00
|
|
|
Ok(serde_json::from_str(&resp.text().await?).unwrap())
|
2020-09-12 12:26:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn download_file(client: reqwest::Client, url: &str, file_name: &str) -> Result<(), reqwest::Error> {
|
|
|
|
let resp = client.get(url)
|
|
|
|
.send()
|
|
|
|
.await?;
|
|
|
|
let mut file = File::create(&file_name).unwrap();
|
|
|
|
let mut stream = resp.bytes_stream();
|
|
|
|
while let Some(item) = stream.next().await {
|
|
|
|
file.write(&item?).unwrap();
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-12-02 15:34:42 +00:00
|
|
|
pub fn get_arg_sauces(args: env::Args) -> Result<Vec<i32>, String> {
|
|
|
|
let mut sauces: Vec<i32> = Vec::new();
|
2020-09-12 12:26:07 +00:00
|
|
|
for sauce in args {
|
2020-12-02 15:34:42 +00:00
|
|
|
let sauce: i32 = match sauce.parse() {
|
2020-09-12 12:26:07 +00:00
|
|
|
Ok(sauce) => sauce,
|
|
|
|
Err(_) => {
|
|
|
|
return Err(format!("{} is not a number/sauce", sauce));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if !sauces.contains(&sauce) {
|
|
|
|
sauces.push(sauce);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(sauces)
|
|
|
|
}
|