implementing consument api as a test

This commit is contained in:
Zastian Pretorius
2023-01-27 21:46:53 +00:00
parent f435f290ac
commit 634643657e
3 changed files with 115 additions and 132 deletions

View File

@@ -1,14 +1,21 @@
use isahc::config::Configurable;
use isahc::{ReadResponseExt, Request, RequestExt};
use regex::Regex;
use std::fs::File;
use std::io::prelude::*;
//use serde_json::json;
pub fn get_anime_html(url: &str) -> String {
pub fn search_anime(query: String) -> (Vec<String>, Vec<String>, Vec<String>) {
let req = Request::builder()
.uri(url)
.uri(format!(
"https://api.consumet.org/meta/anilist/{}",
query
.replace(" ", "%20")
.replace(":", "%3A")
.replace("!", "%21")
.replace("?", "%3F")
.replace("'", "%27")
))
.redirect_policy(isahc::config::RedirectPolicy::Follow)
.header(
"user-agent",
@@ -16,74 +23,78 @@ pub fn get_anime_html(url: &str) -> String {
)
.body(())
.unwrap();
req.send().unwrap().text().unwrap()
}
let json = req.send().unwrap().text().unwrap();
let json: serde_json::Value = serde_json::from_str(&json).unwrap();
let mut titles = Vec::new();
let mut ids = Vec::new();
let mut images = Vec::new();
for i in 0..json["results"].as_array().unwrap().len() {
titles.push(
json["results"][i]["title"]["userPreferred"]
.as_str()
.unwrap()
.to_string(),
);
pub fn get_post(id: &str) -> String {
let resp = Request::builder()
.method("POST")
.uri("https://yugen.to/api/embed/")
.header("x-requested-with", "XMLHttpRequest")
.body(id)
.unwrap()
.send()
.unwrap()
.text();
let resp: String = resp.as_ref().unwrap().to_string();
resp
}
pub fn get_animes(query: String) -> (Vec<String>, Vec<String>, Vec<String>) {
let query = query.replace(" ", "+");
let html = get_anime_html(&format!("https://yugen.to/search/?q={}", query));
let re = Regex::new(r#"href="(/anime[^"]*)""#).unwrap();
let mut animes_links = Vec::new();
for cap in re.captures_iter(&html) {
animes_links.push(cap[1].to_string());
ids.push(json["results"][i]["id"].as_str().unwrap().to_string());
//convert ids to i32
images.push(json["results"][i]["image"].as_str().unwrap().to_string());
}
let re = Regex::new(r#"/" title="([^"]*)""#).unwrap();
let mut animes_names = Vec::new();
for cap in re.captures_iter(&html) {
animes_names.push(cap[1].to_string());
}
let re = Regex::new(r#"data-src="([^"]*)"#).unwrap();
let mut animes_images = Vec::new();
for cap in re.captures_iter(&html) {
animes_images.push(cap[1].to_string());
}
(animes_links, animes_names, animes_images)
(ids, titles, images)
}
pub fn get_anime_info(url: &str) -> (i32, u16) {
let url = format!("https://yugen.to{}watch", url);
let html = get_anime_html(&url);
//print html and exit
let re = Regex::new(r#""mal_id":(\d*)"#).unwrap();
let mal_id = re.captures(&html).unwrap()[1].parse().unwrap();
let re =
Regex::new(r#"Episodes</div><span class="description" style="font-size: \d*px;">(\d*)"#)
.unwrap();
let episodes = re.captures(&html).unwrap()[1].parse().unwrap();
(mal_id, episodes)
pub fn get_episodes(id: &i32) -> (Vec<String>, Vec<String>) {
let req = Request::builder()
.uri(format!(
"https://api.consumet.org/meta/anilist/info/{}?provider=gogoanime",
id
))
.redirect_policy(isahc::config::RedirectPolicy::Follow)
.header(
"user-agent",
"Mozilla/5.0 (X11; Linux x86_64; rv:99.0) Gecko/20100101 Firefox/100.0",
)
.body(())
.unwrap();
let json = req.send().unwrap().text().unwrap();
let json: serde_json::Value = serde_json::from_str(&json).unwrap();
let mut titles = Vec::new();
let mut ids = Vec::new();
for i in 0..json["episodes"].as_array().unwrap().len() {
titles.push(json["episodes"][i]["title"].as_str().unwrap().to_string());
ids.push(json["episodes"][i]["id"].as_str().unwrap().to_string());
}
(titles, ids)
}
pub fn get_anime_link(url: &str, episode: u64) -> String {
let url = &format!(
"https://yugen.to/watch{}{}/",
url.replace("/anime", ""),
episode
);
let html = get_anime_html(url);
let re = Regex::new(r#"/e/([^/]*)"#).unwrap();
let capture = re.captures(&html).unwrap();
let id = &capture[1];
let id = format!("id={}%3D&ac=0", id);
let json = get_post(&id);
let re = Regex::new(r#"hls": \["(.*)","#).unwrap();
let capture = re.captures(&json).unwrap();
let link = &capture[1];
//return the link
link.to_string()
pub fn get_episode_link(ep_id: &str) -> String {
let req = Request::builder()
.uri(format!(
"https://api.consumet.org/meta/anilist/watch/{}?provider=gogoanime",
ep_id
))
.redirect_policy(isahc::config::RedirectPolicy::Follow)
.header(
"user-agent",
"Mozilla/5.0 (X11; Linux x86_64; rv:99.0) Gecko/20100101 Firefox/100.0",
)
.body(())
.unwrap();
let json = req.send().unwrap().text().unwrap();
let json: serde_json::Value = serde_json::from_str(&json).unwrap();
let url = "";
std::fs::write("test.json", json.to_string()).unwrap();
for i in 0..json["sources"].as_array().unwrap().len() {
//return json["sources"][i]["url"].as_str().unwrap().to_string(); where json["sources"][i]["quality"].as_str().unwrap().contains("1080")
if json["sources"][i]["quality"]
.as_str()
.unwrap()
.contains("1080")
{
return json["sources"][i]["url"].as_str().unwrap().to_string();
}
}
url.to_string()
}
pub fn get_image(url: &str, path: &str) {