v0.10-frontend2

This commit is contained in:
2022-10-31 22:49:02 +05:30
parent 30cd957b00
commit b73adf45f8
4 changed files with 102 additions and 9 deletions

View File

@@ -71,6 +71,18 @@ struct Books {
docs: Vec<Docs>,
}
#[derive(Deserialize, Serialize, Debug)]
struct BookPageOL {
description: Option<DescriptionOL>,
}
#[derive(Deserialize, Serialize, Debug)]
struct DescriptionOL {
value: Option<String>,
}
impl Books {
fn set_all_cover_urls(&mut self) {
for book in self.docs.iter_mut() {
@@ -85,6 +97,23 @@ impl Books {
}
}
}
async fn set_all_descriptions(&mut self) {
for book in self.docs.iter_mut() {
let query = format!("https://openlibrary.org/{}.json", book.key);
let res = reqwest::get(query).await.expect("Unable to request");
let resjson = res.json::<BookPageOL>().await.expect("Unable to return value");
let description = resjson.description;
match description {
Some(description) => {
match description.value {
Some(value) => book.description = Some(value),
None => (),
}
}
None => (),
}
}
}
}
// Setup the command line interface with clap.
@@ -170,11 +199,12 @@ async fn search_openlibrary(
let query = format!("https://openlibrary.org/search.json?q={}", search);
let res = reqwest::get(query).await.expect("Unable to request");
let mut resjson = res.json::<Books>().await.expect("Unable to return value");
resjson.docs.truncate(10);
resjson.docs.truncate(12);
resjson.set_all_cover_urls();
// resjson.set_all_descriptions().await;
print!("Search token {:?}\n", search);
let mut vec = Vec::with_capacity(10);
for i in 0..10 as usize {
let mut vec = Vec::with_capacity(12);
for i in 0..12 as usize {
let doc = resjson.docs[i].clone();
vec.push(
BookUI{