mirror of
https://github.com/mrfluffy-dev/kami.git
synced 2026-01-17 04:50:32 +00:00
implementing consument api as a test
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
use crate::{
|
||||
get_an_history, get_an_progress, get_anime_id, get_user_anime_progress, update_anime_progress,
|
||||
get_an_history, get_an_progress, get_user_anime_progress, update_anime_progress,
|
||||
write_an_progress,
|
||||
};
|
||||
use crate::{get_anime_link, get_animes, get_image};
|
||||
use crate::{get_episode_link, get_episodes, get_image, search_anime};
|
||||
use crate::{open_cast, open_video};
|
||||
|
||||
use crossterm::{
|
||||
@@ -22,8 +22,6 @@ use tui::{
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
use viuer::{print_from_file, terminal_size, Config};
|
||||
|
||||
use super::scraper::get_anime_info;
|
||||
|
||||
enum InputMode {
|
||||
Normal,
|
||||
Editing,
|
||||
@@ -90,8 +88,8 @@ struct App {
|
||||
input_mode: InputMode,
|
||||
/// History of recorded messages
|
||||
messages: StatefulList<String>,
|
||||
episodes: (Vec<String>, Vec<String>),
|
||||
title: String,
|
||||
link: String,
|
||||
ep: u64,
|
||||
progress: i32,
|
||||
anime_id: i32,
|
||||
@@ -108,8 +106,8 @@ impl<'a> App {
|
||||
image: String::new(),
|
||||
input_mode: InputMode::Normal,
|
||||
messages: StatefulList::with_items(Vec::new()),
|
||||
episodes: (Vec::new(), Vec::new()),
|
||||
title: String::new(),
|
||||
link: String::new(),
|
||||
ep: 0,
|
||||
progress: 0,
|
||||
anime_id: 0,
|
||||
@@ -257,9 +255,13 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
|
||||
app.progress = 0;
|
||||
let selected = app.messages.state.selected();
|
||||
app.title = app.messages.items[selected.unwrap()].clone();
|
||||
app.link = app.animes.0[selected.unwrap()].clone();
|
||||
let anime_info = get_anime_info(&app.animes.0[selected.unwrap()]);
|
||||
app.anime_id = get_anime_id(anime_info.0);
|
||||
app.anime_id = app.animes.0[selected.unwrap()]
|
||||
.clone()
|
||||
.parse::<i32>()
|
||||
.unwrap();
|
||||
app.episodes = get_episodes(
|
||||
&app.animes.0[selected.unwrap()].parse::<i32>().unwrap(),
|
||||
);
|
||||
app.messages.items.clear();
|
||||
if app.token == "local" || app.anime_id == 0 {
|
||||
app.progress = get_an_progress(&app.title) as i32;
|
||||
@@ -269,8 +271,8 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
|
||||
get_user_anime_progress(app.anime_id, app.token.as_str());
|
||||
app.messages.state.select(Some(app.progress as usize));
|
||||
}
|
||||
if anime_info.1 == 1 {
|
||||
let link = get_anime_link(&app.link, 1);
|
||||
if app.episodes.0.len() == 1 {
|
||||
let link = get_episode_link(&app.episodes.1[0]);
|
||||
if !app.cast.0 {
|
||||
open_video((link, format!("{} Episode 1", &app.title)));
|
||||
} else {
|
||||
@@ -282,14 +284,24 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
|
||||
let selected = app.messages.state.selected();
|
||||
let image_url = app.animes.2[selected.unwrap()].clone();
|
||||
if app.token == "local" || app.anime_id == 0 {
|
||||
write_an_progress((&app.title, &app.link, &image_url), &1);
|
||||
write_an_progress(
|
||||
(&app.title, &app.anime_id.to_string(), &image_url),
|
||||
&1,
|
||||
);
|
||||
} else {
|
||||
update_anime_progress(app.anime_id, 1, app.token.as_str());
|
||||
write_an_progress((&app.title, &app.link, &image_url), &1);
|
||||
write_an_progress(
|
||||
(&app.title, &app.anime_id.to_string(), &image_url),
|
||||
&1,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
for ep in 1..anime_info.1 + 1 {
|
||||
app.messages.push(format!("Episode {}", ep));
|
||||
for ep in 1..app.episodes.1.len() + 1 {
|
||||
app.messages.push(format!(
|
||||
"Episode {}: {}",
|
||||
ep,
|
||||
app.episodes.0[ep - 1]
|
||||
));
|
||||
}
|
||||
ep_select = true;
|
||||
}
|
||||
@@ -301,9 +313,11 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
|
||||
.nth(selected.unwrap())
|
||||
.unwrap()
|
||||
.replace("Episode ", "")
|
||||
.split(":")
|
||||
.collect::<Vec<&str>>()[0]
|
||||
.parse::<u64>()
|
||||
.unwrap();
|
||||
let link = get_anime_link(&app.link, app.ep);
|
||||
let link = get_episode_link(&app.episodes.1[app.ep as usize - 1]);
|
||||
if !app.cast.0 {
|
||||
open_video((link, format!("{} Episode {}", &app.title, app.ep)));
|
||||
} else {
|
||||
@@ -315,14 +329,20 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
|
||||
let image_url = &app.image;
|
||||
if app.ep > app.progress as u64 {
|
||||
if app.token == "local" || app.anime_id == 0 {
|
||||
write_an_progress((&app.title, &app.link, &image_url), &app.ep);
|
||||
write_an_progress(
|
||||
(&app.title, &app.anime_id.to_string(), &image_url),
|
||||
&app.ep,
|
||||
);
|
||||
} else {
|
||||
update_anime_progress(
|
||||
app.anime_id,
|
||||
app.ep as usize,
|
||||
app.token.as_str(),
|
||||
);
|
||||
write_an_progress((&app.title, &app.link, &image_url), &app.ep);
|
||||
write_an_progress(
|
||||
(&app.title, &app.anime_id.to_string(), &image_url),
|
||||
&app.ep,
|
||||
);
|
||||
}
|
||||
app.progress = app.ep as i32;
|
||||
}
|
||||
@@ -333,7 +353,7 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
|
||||
InputMode::Editing => match key.code {
|
||||
KeyCode::Enter => {
|
||||
//push app.input into app.messages with '
|
||||
app.animes = get_animes(app.input.drain(..).collect());
|
||||
app.animes = search_anime(app.input.drain(..).collect());
|
||||
app.messages.items.clear();
|
||||
for anime in &app.animes.1 {
|
||||
app.messages.push(anime.to_string());
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
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)
|
||||
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()
|
||||
.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());
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
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_anime_link(url: &str, episode: u64) -> String {
|
||||
let url = &format!(
|
||||
"https://yugen.to/watch{}{}/",
|
||||
url.replace("/anime", ""),
|
||||
episode
|
||||
.to_string(),
|
||||
);
|
||||
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()
|
||||
|
||||
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());
|
||||
}
|
||||
(ids, titles, images)
|
||||
}
|
||||
|
||||
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_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) {
|
||||
|
||||
@@ -40,54 +40,6 @@ pub fn get_token() -> String {
|
||||
token
|
||||
}
|
||||
|
||||
pub fn get_anime_id(mal_id: i32) -> i32 {
|
||||
const QUERY: &str = "
|
||||
query ($id: Int, $search: Int) {
|
||||
Media (id: $id, idMal: $search, type: ANIME) {
|
||||
id
|
||||
title {
|
||||
native
|
||||
romaji
|
||||
english
|
||||
}
|
||||
}
|
||||
}
|
||||
";
|
||||
let json = json!({
|
||||
"query": QUERY,
|
||||
"variables": {
|
||||
"search": mal_id
|
||||
}
|
||||
});
|
||||
let resp = Request::builder()
|
||||
.method("POST")
|
||||
.uri("https://graphql.anilist.co/")
|
||||
.header("Content-Type", "application/json")
|
||||
.header("Accept", "application/json")
|
||||
.body(json.to_string())
|
||||
.unwrap()
|
||||
.send()
|
||||
.unwrap()
|
||||
.text();
|
||||
let regex = regex::Regex::new(r#"id":(.*?),"#).unwrap();
|
||||
let resp: String = resp.as_ref().unwrap().to_string();
|
||||
//if error let id = 0
|
||||
let id = match regex.captures(&resp) {
|
||||
Some(captures) => captures[1].parse::<i32>().unwrap(),
|
||||
None => 0,
|
||||
};
|
||||
|
||||
// let id = regex
|
||||
// .captures(&resp)
|
||||
// .unwrap()
|
||||
// .get(1)
|
||||
// .unwrap()
|
||||
// .as_str()
|
||||
// .parse::<i32>()
|
||||
// .unwrap();
|
||||
id
|
||||
}
|
||||
|
||||
//get the user id from the token
|
||||
fn get_user_id(token: &str) -> i32 {
|
||||
const QUERY: &str = "query {
|
||||
|
||||
Reference in New Issue
Block a user