Make info not shit itself when it gets an error

This commit is contained in:
blank X 2020-12-02 00:15:45 +07:00
parent 3bdf78e03d
commit 8a67b74ce2
4 changed files with 28 additions and 9 deletions

View File

@ -1,4 +1,5 @@
use crate::utils;
use crate::structs;
use std::env;
use std::path::Path;
@ -26,7 +27,10 @@ pub async fn run(args: env::Args) {
let cloned_client = client.clone();
let mut cloned_tx = tx.clone();
handles.push(tokio::spawn(async move {
let sauce_info = utils::get_sauce_info(cloned_client, sauce).await.unwrap();
let sauce_info = match utils::get_sauce_info(cloned_client, sauce).await.unwrap() {
structs::GalleryInfo::Info(sauce_info) => sauce_info,
structs::GalleryInfo::Error(sauce_error) => panic!("{} returned: {}", sauce, sauce_error.error)
};
let base_path = sauce_info.id.to_string();
let base_path = Path::new(&base_path);
match create_dir(base_path) {

View File

@ -16,15 +16,19 @@ pub async fn run(args: env::Args) {
exit(1);
}
let client = reqwest::Client::new();
let mut handles: Vec<JoinHandle<structs::GalleryInfo>> = Vec::with_capacity(sauces.len());
let mut handles: Vec<JoinHandle<(structs::GalleryInfo, usize)>> = Vec::with_capacity(sauces.len());
for sauce in sauces {
let cloned_client = client.clone();
handles.push(tokio::spawn(async move {
utils::get_sauce_info(cloned_client, sauce).await.unwrap()
(utils::get_sauce_info(cloned_client, sauce).await.unwrap(), sauce)
}));
}
for handle in handles {
println!("{}", utils::human_sauce_info(&handle.await.unwrap()));
let (sauce_info, sauce) = handle.await.unwrap();
match sauce_info {
structs::GalleryInfo::Info(sauce_info) => println!("{}", utils::human_sauce_info(&sauce_info)),
structs::GalleryInfo::Error(sauce_error) => eprintln!("Sauce: {}\nError: {}", sauce, sauce_error.error)
};
if remaining_to_show > 1 {
println!("");
remaining_to_show -= 1;

View File

@ -31,7 +31,7 @@ pub struct GalleryTagInfo {
}
#[derive(Deserialize, Debug)]
pub struct GalleryInfo {
pub struct GalleryInfoSuccess {
pub id: usize,
pub media_id: String,
pub title: GalleryTitleInfo,
@ -43,9 +43,21 @@ pub struct GalleryInfo {
pub num_favorites: usize
}
#[derive(Deserialize, Debug)]
pub struct GalleryInfoError {
pub error: String
}
#[derive(Deserialize, Debug)]
#[serde(untagged)]
pub enum GalleryInfo {
Info(GalleryInfoSuccess),
Error(GalleryInfoError)
}
#[derive(Deserialize, Debug)]
pub struct SearchInfo {
pub result: Vec<GalleryInfo>,
pub result: Vec<GalleryInfoSuccess>,
pub num_pages: usize,
pub per_page: usize
}

View File

@ -23,7 +23,6 @@ pub async fn get_sauce_info(client: reqwest::Client, sauce: usize) -> Result<str
let resp = client.get(&uri)
.send()
.await?;
assert!(resp.status().is_success());
let body = resp.text().await?;
let mut body: serde_json::Value = serde_json::from_str(&body).unwrap();
fix_gallery(&mut body).unwrap();
@ -36,7 +35,7 @@ pub async fn get_search_info(client: reqwest::Client, search_query: &str) -> Res
.query(&[("query", search_query)])
.send()
.await?;
assert_eq!(resp.status().is_success(), true);
assert!(resp.status().is_success());
let body = resp.text().await?;
let mut body: serde_json::Value = serde_json::from_str(&body).unwrap();
for i in 0..body["result"].as_array().unwrap().len() {
@ -73,7 +72,7 @@ pub fn get_arg_sauces(args: env::Args) -> Result<Vec<usize>, String> {
Ok(sauces)
}
pub fn human_sauce_info(sauce_info: &structs::GalleryInfo) -> String {
pub fn human_sauce_info(sauce_info: &structs::GalleryInfoSuccess) -> String {
let mut text = format!("Sauce: {}\nTitle: ", sauce_info.id);
let japanese_title = sauce_info.title.japanese.as_ref();
let english_title = sauce_info.title.english.as_ref();