hanimers/src/utils.rs

81 lines
2.6 KiB
Rust
Raw Permalink Normal View History

2021-02-02 16:52:15 +00:00
use crate::structs;
use quick_xml::events::Event;
2022-04-30 11:30:36 +00:00
use quick_xml::Reader;
2021-02-02 16:52:15 +00:00
extern crate reqwest;
extern crate serde_json;
2022-04-30 11:30:36 +00:00
pub async fn search(
client: reqwest::Client,
query: &str,
tags: Vec<&str>,
broad_search: bool,
) -> Result<Vec<structs::SearchResult>, structs::Error> {
2021-02-02 16:52:15 +00:00
let tags_mode = match broad_search {
true => "OR",
2022-04-30 11:30:36 +00:00
false => "AND",
2021-02-02 16:52:15 +00:00
};
Ok(serde_json::from_str(
&serde_json::from_str::<structs::SearchResultMetadata>(
&client.post("https://search.htv-services.com")
.header("Content-Type", "application/json")
.body(serde_json::json!(
{"search_text": &query, "tags": tags, "tags_mode": tags_mode, "brands": [], "blacklist": [], "order_by": "created_at_unix", "ordering": "desc", "page": 0}
).to_string())
.send()
.await?
.text()
.await?
)?.hits
)?)
}
2022-04-30 11:30:36 +00:00
pub async fn get_hentai(
client: reqwest::Client,
id: &str,
) -> Result<Option<structs::HentaiInfo>, structs::Error> {
let resp = client
.get(&format!("https://hanime.tv/videos/hentai/{}", id))
2021-02-02 16:52:15 +00:00
.send()
.await?;
if resp.status() != 200 {
return Ok(None);
}
let text = resp.text().await?;
let mut reader = Reader::from_str(&text);
reader.check_end_names(false);
let mut buf = Vec::new();
let mut script = String::new();
let mut is_inside_script = false;
let mut to_return = None;
loop {
match reader.read_event(&mut buf) {
Ok(Event::Start(ref e)) if e.name() == b"script" => is_inside_script = true,
Ok(Event::Text(e)) if is_inside_script => {
let text = match reader.decode(e.escaped()) {
Ok(text) => text,
2022-04-30 11:30:36 +00:00
Err(_) => continue,
2021-02-02 16:52:15 +00:00
};
if !script.is_empty() || text.starts_with("window.__NUXT__={") {
script.push_str(&text);
}
2022-04-30 11:30:36 +00:00
}
2021-02-02 16:52:15 +00:00
Ok(Event::End(ref e)) if e.name() == b"script" && is_inside_script => {
if script.is_empty() {
is_inside_script = false;
} else {
2022-04-30 11:30:36 +00:00
to_return = Some(serde_json::from_str(
script.splitn(2, "=").nth(1).unwrap().trim_end_matches(';'),
)?);
2021-02-02 16:52:15 +00:00
break;
}
2022-04-30 11:30:36 +00:00
}
2021-02-02 16:52:15 +00:00
Err(err) => panic!("Error at position {}: {:?}", reader.buffer_position(), err),
Ok(Event::Eof) => break,
2022-04-30 11:30:36 +00:00
_ => (),
2021-02-02 16:52:15 +00:00
};
buf.clear();
}
Ok(to_return)
}