From 9495d93fffe0cbd7fb5cf568018119e8e10b19c1 Mon Sep 17 00:00:00 2001 From: blank X Date: Mon, 11 Jan 2021 18:06:42 +0700 Subject: [PATCH] Don't show summary if empty, and more things --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/commands/feed.rs | 14 +++++++----- src/structs.rs | 11 ++++++---- src/utils.rs | 52 +++++++++++++++++++++++++------------------- 5 files changed, 47 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 353b8a6..08253fa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -330,7 +330,7 @@ dependencies = [ [[package]] name = "mangafetchi" -version = "0.1.5" +version = "0.1.6" dependencies = [ "quick-xml", "reqwest", diff --git a/Cargo.toml b/Cargo.toml index 5e6563b..5365a6a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mangafetchi" -version = "0.1.5" +version = "0.1.6" authors = ["blank X "] edition = "2018" diff --git a/src/commands/feed.rs b/src/commands/feed.rs index c544b82..455c50e 100644 --- a/src/commands/feed.rs +++ b/src/commands/feed.rs @@ -56,14 +56,16 @@ pub async fn run(mut args: env::Args) { let elem = BytesEnd::owned(b"link".to_vec()); writer.write_event(Event::End(elem)).unwrap(); - let elem = BytesStart::owned(b"description".to_vec(), 11); - writer.write_event(Event::Start(elem)).unwrap(); + if manga_info.summary.is_some() { + let elem = BytesStart::owned(b"description".to_vec(), 11); + writer.write_event(Event::Start(elem)).unwrap(); - let elem = BytesText::from_plain_str(&manga_info.summary).into_owned(); - writer.write_event(Event::Text(elem)).unwrap(); + let elem = BytesText::from_plain_str(&manga_info.summary.unwrap()).into_owned(); + writer.write_event(Event::Text(elem)).unwrap(); - let elem = BytesEnd::owned(b"description".to_vec()); - writer.write_event(Event::End(elem)).unwrap(); + let elem = BytesEnd::owned(b"description".to_vec()); + writer.write_event(Event::End(elem)).unwrap(); + } } for chapter in manga_info.chapters { diff --git a/src/structs.rs b/src/structs.rs index 0166300..0b94e67 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -39,20 +39,23 @@ pub struct Manga { pub status: String, pub last_updated: String, pub genres: Vec, - pub summary: String, + pub summary: Option, pub chapters: Vec } impl fmt::Display for Manga { fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { - let mut text = format!("ID: {}\nName: {}\nStatus: {}\nLast Updated: {}\nGenres: {}\nAuthors: {}\nSummary:\n{}\nChapters:", + let mut text = format!("ID: {}\nName: {}\nStatus: {}\nLast Updated: {}\nGenres: {}\nAuthors: {}", self.id, self.name, self.status, self.last_updated, self.genres.join(", "), - self.authors.join(", "), - self.summary); + self.authors.join(", ")); + if self.summary.is_some() { + text.push_str(&format!("\nSummary:\n{}", self.summary.as_ref().unwrap())); + } + text.push_str("\nChapters:"); for chapter in &self.chapters { text.push_str(&format!("\n- {}", &chapter)); } diff --git a/src/utils.rs b/src/utils.rs index 74300bb..a806df3 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -105,7 +105,7 @@ fn parse_manganelo_manga(text: &str, manga_id: &str) -> structs::MangaOption { let mut name: Option = None; let mut status: Option = None; let mut last_updated: Option = None; - let mut summary = String::new(); + let mut summary: Option = None; let mut authors = Vec::new(); let mut genres = Vec::new(); let mut chapters = Vec::new(); @@ -201,18 +201,23 @@ fn parse_manganelo_manga(text: &str, manga_id: &str) -> structs::MangaOption { continue; } }; + let text = text.trim(); if name.is_none() && is_inside_h1 { - name = Some(text); + name = Some(text.to_string()); } else if is_inside_authors && is_inside_td && is_inside_a { - authors.push(text.trim().to_string()); + authors.push(text.to_string()); } else if is_inside_status && is_inside_td { - status = Some(text); + status = Some(text.to_string()); } else if is_inside_genres && is_inside_td && is_inside_a { - genres.push(text.trim().to_string()); + genres.push(text.to_string()); } else if last_updated.is_none() && is_inside_stre_value { - last_updated = Some(text); - } else if is_inside_description && !is_inside_h3 { - summary.push_str(text.trim()); + last_updated = Some(text.to_string()); + } else if is_inside_description && !is_inside_h3 && !text.is_empty() { + if summary.is_some() { + summary.as_mut().unwrap().push_str(text); + } else { + summary = Some(text.to_string()); + } } else if is_inside_ul && is_inside_a && tmp_chapter_link.is_some() { let chapter_name = match text.splitn(2, &[':', '-'][..]).nth(1) { Some(text) => Some(text.trim().to_string()), @@ -224,7 +229,7 @@ fn parse_manganelo_manga(text: &str, manga_id: &str) -> structs::MangaOption { domain: "manganelo.com".to_string() }); tmp_chapter_link = None; - } else if text.trim().starts_with("REDIRECT : ") { + } else if text.starts_with("REDIRECT : ") { return structs::MangaOption::Redirect(structs::Redirect { url: text.splitn(2, ':').nth(1).unwrap().trim().to_string() }); } }, @@ -260,7 +265,7 @@ fn parse_manganelo_manga(text: &str, manga_id: &str) -> structs::MangaOption { status: status.unwrap(), last_updated: last_updated.unwrap(), genres: genres, - summary: summary.trim().to_string(), + summary: summary, chapters: chapters } ) @@ -282,7 +287,7 @@ fn parse_mangakakalot_manga(text: &str, manga_id: &str) -> structs::MangaOption let mut name: Option = None; let mut status: Option = None; let mut last_updated: Option = None; - let mut summary = String::new(); + let mut summary: Option = None; let mut is_inside_noidungm = false; let mut is_inside_h1 = false; let mut is_inside_h2 = false; @@ -360,13 +365,14 @@ fn parse_mangakakalot_manga(text: &str, manga_id: &str) -> structs::MangaOption continue; } }; + let text = text.trim(); if is_inside_manga_info { if is_inside_h1 { - name = Some(text); + name = Some(text.to_string()); } else if is_inside_authors && is_inside_a { - authors.push(text.trim().to_string()); + authors.push(text.to_string()); } else if is_inside_genres && is_inside_a { - genres.push(text.trim().to_string()); + genres.push(text.to_string()); } else { match text.splitn(2, ' ').nth(0).unwrap() { "Author(s)" => is_inside_authors = true, @@ -380,8 +386,12 @@ fn parse_mangakakalot_manga(text: &str, manga_id: &str) -> structs::MangaOption _ => () } } - } else if is_inside_noidungm && !is_inside_h2 { - summary.push_str(&text.trim()); + } else if is_inside_noidungm && !is_inside_h2 && !text.is_empty(){ + if summary.is_some() { + summary.as_mut().unwrap().push_str(text); + } else { + summary = Some(text.to_string()); + } } else if is_inside_chapter_list && is_inside_a && tmp_chapter_link.is_some() { let chapter_name = match text.splitn(2, &[':', '-'][..]).nth(1) { Some(text) => Some(text.trim().to_string()), @@ -394,16 +404,14 @@ fn parse_mangakakalot_manga(text: &str, manga_id: &str) -> structs::MangaOption }); tmp_chapter_link = None; } else if is_inside_title { - is_title_real = !text.trim().is_empty(); + is_title_real = !text.is_empty(); } else if text.trim().starts_with("REDIRECT : ") { return structs::MangaOption::Redirect(structs::Redirect { url: text.splitn(2, ':').nth(1).unwrap().trim().to_string() }); } }, Ok(Event::Empty(ref e)) => { - if is_inside_noidungm { - if e.name() == b"br" { - summary.push_str("\n"); - } + if is_inside_noidungm && e.name() == b"br" && summary.is_some() { + summary.as_mut().unwrap().push('\n'); } }, Ok(Event::End(e)) => { @@ -447,7 +455,7 @@ fn parse_mangakakalot_manga(text: &str, manga_id: &str) -> structs::MangaOption status: status.unwrap(), last_updated: last_updated.unwrap(), genres: genres, - summary: summary.trim().to_string(), + summary: summary, chapters: chapters }) }