Don't show summary if empty, and more things

This commit is contained in:
blank X 2021-01-11 18:06:42 +07:00
parent e8470cb8b9
commit 9495d93fff
5 changed files with 47 additions and 34 deletions

2
Cargo.lock generated
View File

@ -330,7 +330,7 @@ dependencies = [
[[package]] [[package]]
name = "mangafetchi" name = "mangafetchi"
version = "0.1.5" version = "0.1.6"
dependencies = [ dependencies = [
"quick-xml", "quick-xml",
"reqwest", "reqwest",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "mangafetchi" name = "mangafetchi"
version = "0.1.5" version = "0.1.6"
authors = ["blank X <theblankx@protonmail.com>"] authors = ["blank X <theblankx@protonmail.com>"]
edition = "2018" edition = "2018"

View File

@ -56,14 +56,16 @@ pub async fn run(mut args: env::Args) {
let elem = BytesEnd::owned(b"link".to_vec()); let elem = BytesEnd::owned(b"link".to_vec());
writer.write_event(Event::End(elem)).unwrap(); writer.write_event(Event::End(elem)).unwrap();
let elem = BytesStart::owned(b"description".to_vec(), 11); if manga_info.summary.is_some() {
writer.write_event(Event::Start(elem)).unwrap(); 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(); let elem = BytesText::from_plain_str(&manga_info.summary.unwrap()).into_owned();
writer.write_event(Event::Text(elem)).unwrap(); writer.write_event(Event::Text(elem)).unwrap();
let elem = BytesEnd::owned(b"description".to_vec()); let elem = BytesEnd::owned(b"description".to_vec());
writer.write_event(Event::End(elem)).unwrap(); writer.write_event(Event::End(elem)).unwrap();
}
} }
for chapter in manga_info.chapters { for chapter in manga_info.chapters {

View File

@ -39,20 +39,23 @@ pub struct Manga {
pub status: String, pub status: String,
pub last_updated: String, pub last_updated: String,
pub genres: Vec<String>, pub genres: Vec<String>,
pub summary: String, pub summary: Option<String>,
pub chapters: Vec<Chapter> pub chapters: Vec<Chapter>
} }
impl fmt::Display for Manga { impl fmt::Display for Manga {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { 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.id,
self.name, self.name,
self.status, self.status,
self.last_updated, self.last_updated,
self.genres.join(", "), self.genres.join(", "),
self.authors.join(", "), self.authors.join(", "));
self.summary); 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 { for chapter in &self.chapters {
text.push_str(&format!("\n- {}", &chapter)); text.push_str(&format!("\n- {}", &chapter));
} }

View File

@ -105,7 +105,7 @@ fn parse_manganelo_manga(text: &str, manga_id: &str) -> structs::MangaOption {
let mut name: Option<String> = None; let mut name: Option<String> = None;
let mut status: Option<String> = None; let mut status: Option<String> = None;
let mut last_updated: Option<String> = None; let mut last_updated: Option<String> = None;
let mut summary = String::new(); let mut summary: Option<String> = None;
let mut authors = Vec::new(); let mut authors = Vec::new();
let mut genres = Vec::new(); let mut genres = Vec::new();
let mut chapters = Vec::new(); let mut chapters = Vec::new();
@ -201,18 +201,23 @@ fn parse_manganelo_manga(text: &str, manga_id: &str) -> structs::MangaOption {
continue; continue;
} }
}; };
let text = text.trim();
if name.is_none() && is_inside_h1 { 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 { } 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 { } 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 { } 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 { } else if last_updated.is_none() && is_inside_stre_value {
last_updated = Some(text); last_updated = Some(text.to_string());
} else if is_inside_description && !is_inside_h3 { } else if is_inside_description && !is_inside_h3 && !text.is_empty() {
summary.push_str(text.trim()); 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() { } else if is_inside_ul && is_inside_a && tmp_chapter_link.is_some() {
let chapter_name = match text.splitn(2, &[':', '-'][..]).nth(1) { let chapter_name = match text.splitn(2, &[':', '-'][..]).nth(1) {
Some(text) => Some(text.trim().to_string()), 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() domain: "manganelo.com".to_string()
}); });
tmp_chapter_link = None; 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() }); 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(), status: status.unwrap(),
last_updated: last_updated.unwrap(), last_updated: last_updated.unwrap(),
genres: genres, genres: genres,
summary: summary.trim().to_string(), summary: summary,
chapters: chapters chapters: chapters
} }
) )
@ -282,7 +287,7 @@ fn parse_mangakakalot_manga(text: &str, manga_id: &str) -> structs::MangaOption
let mut name: Option<String> = None; let mut name: Option<String> = None;
let mut status: Option<String> = None; let mut status: Option<String> = None;
let mut last_updated: Option<String> = None; let mut last_updated: Option<String> = None;
let mut summary = String::new(); let mut summary: Option<String> = None;
let mut is_inside_noidungm = false; let mut is_inside_noidungm = false;
let mut is_inside_h1 = false; let mut is_inside_h1 = false;
let mut is_inside_h2 = false; let mut is_inside_h2 = false;
@ -360,13 +365,14 @@ fn parse_mangakakalot_manga(text: &str, manga_id: &str) -> structs::MangaOption
continue; continue;
} }
}; };
let text = text.trim();
if is_inside_manga_info { if is_inside_manga_info {
if is_inside_h1 { if is_inside_h1 {
name = Some(text); name = Some(text.to_string());
} else if is_inside_authors && is_inside_a { } 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 { } else if is_inside_genres && is_inside_a {
genres.push(text.trim().to_string()); genres.push(text.to_string());
} else { } else {
match text.splitn(2, ' ').nth(0).unwrap() { match text.splitn(2, ' ').nth(0).unwrap() {
"Author(s)" => is_inside_authors = true, "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 { } else if is_inside_noidungm && !is_inside_h2 && !text.is_empty(){
summary.push_str(&text.trim()); 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() { } else if is_inside_chapter_list && is_inside_a && tmp_chapter_link.is_some() {
let chapter_name = match text.splitn(2, &[':', '-'][..]).nth(1) { let chapter_name = match text.splitn(2, &[':', '-'][..]).nth(1) {
Some(text) => Some(text.trim().to_string()), 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; tmp_chapter_link = None;
} else if is_inside_title { } 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 : ") { } else if text.trim().starts_with("REDIRECT : ") {
return structs::MangaOption::Redirect(structs::Redirect { url: text.splitn(2, ':').nth(1).unwrap().trim().to_string() }); return structs::MangaOption::Redirect(structs::Redirect { url: text.splitn(2, ':').nth(1).unwrap().trim().to_string() });
} }
}, },
Ok(Event::Empty(ref e)) => { Ok(Event::Empty(ref e)) => {
if is_inside_noidungm { if is_inside_noidungm && e.name() == b"br" && summary.is_some() {
if e.name() == b"br" { summary.as_mut().unwrap().push('\n');
summary.push_str("\n");
}
} }
}, },
Ok(Event::End(e)) => { Ok(Event::End(e)) => {
@ -447,7 +455,7 @@ fn parse_mangakakalot_manga(text: &str, manga_id: &str) -> structs::MangaOption
status: status.unwrap(), status: status.unwrap(),
last_updated: last_updated.unwrap(), last_updated: last_updated.unwrap(),
genres: genres, genres: genres,
summary: summary.trim().to_string(), summary: summary,
chapters: chapters chapters: chapters
}) })
} }