Add utils::human_sauce_info

This commit is contained in:
blank X 2020-12-01 22:02:05 +07:00
parent a849053fd3
commit 3bdf78e03d
2 changed files with 42 additions and 37 deletions

View File

@ -1,9 +1,10 @@
use crate::utils; use crate::utils;
use crate::structs;
use std::env; use std::env;
use std::process::exit; use std::process::exit;
use tokio::task::JoinHandle; use tokio::task::JoinHandle;
use std::collections::BTreeMap; //use std::collections::BTreeMap;
extern crate tokio; extern crate tokio;
extern crate reqwest; extern crate reqwest;
@ -15,48 +16,15 @@ pub async fn run(args: env::Args) {
exit(1); exit(1);
} }
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let mut handles: Vec<JoinHandle<String>> = Vec::new(); let mut handles: Vec<JoinHandle<structs::GalleryInfo>> = Vec::with_capacity(sauces.len());
for sauce in sauces { for sauce in sauces {
let cloned_client = client.clone(); let cloned_client = client.clone();
handles.push(tokio::spawn(async move { handles.push(tokio::spawn(async move {
let sauce_info = utils::get_sauce_info(cloned_client, sauce).await.unwrap(); utils::get_sauce_info(cloned_client, sauce).await.unwrap()
let mut text = format!("Sauce: {}\nTitle: ", sauce_info.id);
let japanese_title = &sauce_info.title.japanese.unwrap_or_default();
let english_title = &sauce_info.title.english.unwrap_or_default();
if english_title.as_str() != "" {
text.push_str(&english_title);
if japanese_title.as_str() != "" {
text.push_str(&format!("\nJapanese Title: {}", &japanese_title));
}
} else {
text.push_str(&japanese_title);
}
let mut tag_hashmap = BTreeMap::new();
for tag_info in sauce_info.tags {
let tag_key = tag_info.r#type;
let tag_value = tag_info.name;
let tag_vec = tag_hashmap.entry(tag_key).or_insert(Vec::new());
tag_vec.push(tag_value);
}
for (tag_key, tag_value) in &tag_hashmap {
let tag_key = match tag_key.as_str() {
"tag" => "Tags",
"artist" => "Artists",
"parody" => "Parodies",
"character" => "Characters",
"group" => "Groups",
"language" => "Languages",
"category" => "Categories",
_ => tag_key
};
text.push_str(&format!("\n{}: {}", tag_key, tag_value.join(", ")));
}
text.push_str(&format!("\nPages: {}\nFavorites: {}", sauce_info.num_pages, sauce_info.num_favorites));
text
})); }));
} }
for handle in handles { for handle in handles {
println!("{}", handle.await.unwrap()); println!("{}", utils::human_sauce_info(&handle.await.unwrap()));
if remaining_to_show > 1 { if remaining_to_show > 1 {
println!(""); println!("");
remaining_to_show -= 1; remaining_to_show -= 1;

View File

@ -4,6 +4,7 @@ use std::env;
use std::fs::File; use std::fs::File;
use std::io::Write; use std::io::Write;
use tokio::stream::StreamExt; use tokio::stream::StreamExt;
use std::collections::BTreeMap;
extern crate serde_json; extern crate serde_json;
extern crate reqwest; extern crate reqwest;
@ -71,3 +72,39 @@ pub fn get_arg_sauces(args: env::Args) -> Result<Vec<usize>, String> {
} }
Ok(sauces) Ok(sauces)
} }
pub fn human_sauce_info(sauce_info: &structs::GalleryInfo) -> 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();
if english_title.is_some() {
text.push_str(english_title.unwrap());
if japanese_title.is_some() {
text.push_str(&format!("\nJapanese Title: {}", japanese_title.unwrap()));
}
} else {
text.push_str(japanese_title.unwrap());
}
let mut tag_hashmap = BTreeMap::new();
for tag_info in &sauce_info.tags {
let tag_key = tag_info.r#type.as_str();
let tag_value = tag_info.name.as_str();
let tag_vec = tag_hashmap.entry(tag_key).or_insert(Vec::new());
tag_vec.push(tag_value);
}
for (tag_key, tag_value) in tag_hashmap {
let tag_key = match tag_key {
"tag" => "Tags",
"artist" => "Artists",
"parody" => "Parodies",
"character" => "Characters",
"group" => "Groups",
"language" => "Languages",
"category" => "Categories",
_ => tag_key
};
text.push_str(&format!("\n{}: {}", tag_key, tag_value.join(", ")));
}
text.push_str(&format!("\nPages: {}\nFavorites: {}", sauce_info.num_pages, sauce_info.num_favorites));
text
}