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]]
name = "mangafetchi"
version = "0.1.5"
version = "0.1.6"
dependencies = [
"quick-xml",
"reqwest",

View File

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

View File

@ -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 {

View File

@ -39,20 +39,23 @@ pub struct Manga {
pub status: String,
pub last_updated: String,
pub genres: Vec<String>,
pub summary: String,
pub summary: Option<String>,
pub chapters: Vec<Chapter>
}
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));
}

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 status: 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 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<String> = None;
let mut status: 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_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
})
}