Properly show hentai ID

This commit is contained in:
blank X 2021-02-13 16:53:15 +07:00
parent e03d746ce6
commit 56898120d9
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
5 changed files with 26 additions and 5 deletions

2
Cargo.lock generated
View File

@ -206,7 +206,7 @@ checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04"
[[package]] [[package]]
name = "hentaihavenrs" name = "hentaihavenrs"
version = "0.1.0" version = "0.1.1"
dependencies = [ dependencies = [
"clap", "clap",
"quick-xml", "quick-xml",

View File

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

View File

@ -37,7 +37,7 @@ pub async fn view(arg_m: &ArgMatches<'_>) {
if one_done { if one_done {
println!(""); println!("");
} }
println!("ID: {}\n{}", id, &hentai); println!("{}", &hentai);
}, },
None => { None => {
if one_done { if one_done {

View File

@ -23,6 +23,7 @@ impl fmt::Display for SearchResult {
#[derive(Debug)] #[derive(Debug)]
pub struct HentaiInfo { pub struct HentaiInfo {
pub id: String,
pub slug: String, pub slug: String,
pub title: String, pub title: String,
pub views: usize, pub views: usize,
@ -34,7 +35,8 @@ pub struct HentaiInfo {
impl fmt::Display for HentaiInfo { impl fmt::Display for HentaiInfo {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "Title: {}\nViews: {}\nCensored: {}\nGenres: {}\nEpisodes: {}\nSummary:\n{}", write!(formatter, "ID: {}\nTitle: {}\nViews: {}\nCensored: {}\nGenres: {}\nEpisodes: {}\nSummary:\n{}",
&self.id,
&self.title, &self.title,
self.views, self.views,
match self.censored { match self.censored {

View File

@ -19,7 +19,7 @@ pub async fn search(client: reqwest::Client, query: &str) -> Result<Vec<structs:
pub async fn get_hentai(client: reqwest::Client, id: &str) -> Result<Option<structs::HentaiInfo>, structs::Error> { pub async fn get_hentai(client: reqwest::Client, id: &str) -> Result<Option<structs::HentaiInfo>, structs::Error> {
let url = match id.contains(|c: char| !c.is_digit(10)) { let url = match id.contains(|c: char| !c.is_digit(10)) {
true => format!("https://hentaihaven.xxx/watch/{}", &id), true => format!("https://hentaihaven.xxx/watch/{}", &id),
false => format!("https://hentaihaven.xxx/?post_type=wp-manga&p={}", &id) false => format!("https://hentaihaven.xxx/?p={}", &id)
}; };
let resp = client.get(&url) let resp = client.get(&url)
.send() .send()
@ -27,6 +27,7 @@ pub async fn get_hentai(client: reqwest::Client, id: &str) -> Result<Option<stru
if resp.status() != 200 { if resp.status() != 200 {
return Ok(None); return Ok(None);
} }
let mut id = String::new();
let slug = resp.url().path().trim_end_matches('/').rsplitn(2, '/').nth(0).unwrap().to_string(); let slug = resp.url().path().trim_end_matches('/').rsplitn(2, '/').nth(0).unwrap().to_string();
let text = resp.text().await?.replace("&nbsp;", " "); let text = resp.text().await?.replace("&nbsp;", " ");
let mut reader = Reader::from_str(&text); let mut reader = Reader::from_str(&text);
@ -109,6 +110,20 @@ pub async fn get_hentai(client: reqwest::Client, id: &str) -> Result<Option<stru
Err(_) => () Err(_) => ()
}; };
} }
} else if id.is_empty() {
let data_post = e.attributes()
.find(|i| {
match i.as_ref() {
Ok(i) => i.key == b"data-post",
Err(_) => false
}
});
if let Some(data_post) = data_post {
match data_post.unwrap().unescape_and_decode_value(&reader) {
Ok(data_post) => id = data_post,
Err(_) => ()
};
}
} }
} }
}, },
@ -166,9 +181,13 @@ pub async fn get_hentai(client: reqwest::Client, id: &str) -> Result<Option<stru
}; };
buf.clear(); buf.clear();
} }
if id.is_empty() {
return Ok(None);
}
episode_urls.reverse(); episode_urls.reverse();
summary = summary.trim().to_string(); summary = summary.trim().to_string();
Ok(Some(structs::HentaiInfo { Ok(Some(structs::HentaiInfo {
id: id,
slug: slug, slug: slug,
title: title, title: title,
views: rank, views: rank,