chore: merge with consumet branch

This commit is contained in:
newbee1905
2023-02-03 17:13:58 +07:00
5 changed files with 204 additions and 184 deletions

View File

@@ -8,16 +8,28 @@ use rust_cast::{
}; };
use std::str::FromStr; use std::str::FromStr;
pub fn open_video(link: (String, String)) { pub fn open_video(link: (String, String, String)) {
let title = link.1; if link.2 == "null" {
let title = title.replace("-", " "); let title = link.1;
let arg: String = format!("--force-media-title={}", title); let title = title.replace("-", " ");
let _ = std::process::Command::new("mpv") let arg: String = format!("--force-media-title={}", title);
.arg(link.0) let _ = std::process::Command::new("mpv")
.arg(arg) .arg(link.0)
.output() .arg(arg)
.expect("failed to open mpv"); .output()
.expect("failed to open mpv");
} else {
let title = link.1;
let title = title.replace("-", " ");
let arg1: String = format!("--force-media-title={}", title);
let arg2: String = format!("--sub-files={}", link.2);
let _ = std::process::Command::new("mpv")
.arg(link.0)
.arg(arg1)
.arg(arg2)
.output()
.expect("failed to open mpv");
}
// clear terminal // clear terminal
} }

View File

@@ -1,14 +1,21 @@
use isahc::config::Configurable; use isahc::config::Configurable;
use isahc::{ReadResponseExt, Request, RequestExt}; use isahc::{ReadResponseExt, Request, RequestExt};
use regex::Regex;
use std::fs::File; use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
//use serde_json::json; //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() 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) .redirect_policy(isahc::config::RedirectPolicy::Follow)
.header( .header(
"user-agent", "user-agent",
@@ -16,74 +23,95 @@ pub fn get_anime_html(url: &str) -> String {
) )
.body(()) .body(())
.unwrap(); .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 { ids.push(json["results"][i]["id"].as_str().unwrap().to_string());
let resp = Request::builder() //convert ids to i32
.method("POST") images.push(json["results"][i]["image"].as_str().unwrap().to_string());
.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());
} }
let re = Regex::new(r#"/" title="([^"]*)""#).unwrap(); (ids, titles, images)
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) { pub fn get_episodes(id: &i32, provider: &str) -> (Vec<String>, Vec<String>) {
let url = format!("https://yugen.to{}watch", url); let req = Request::builder()
let html = get_anime_html(&url); .uri(format!(
//print html and exit "https://api.consumet.org/meta/anilist/info/{}?provider={}",
let re = Regex::new(r#""mal_id":(\d*)"#).unwrap(); id, provider
let mal_id = re.captures(&html).unwrap()[1].parse().unwrap(); ))
let re = .redirect_policy(isahc::config::RedirectPolicy::Follow)
Regex::new(r#"Episodes</div><span class="description" style="font-size: \d*px;">(\d*)"#) .header(
.unwrap(); "user-agent",
let episodes = re.captures(&html).unwrap()[1].parse().unwrap(); "Mozilla/5.0 (X11; Linux x86_64; rv:99.0) Gecko/20100101 Firefox/100.0",
(mal_id, episodes) )
.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 { pub fn get_episode_link(ep_id: &str, provider: &str) -> (String, String) {
let url = &format!( let req = Request::builder()
"https://yugen.to/watch{}{}/", .uri(format!(
url.replace("/anime", ""), "https://api.consumet.org/meta/anilist/watch/{}?provider={}",
episode ep_id, provider
); ))
let html = get_anime_html(url); .redirect_policy(isahc::config::RedirectPolicy::Follow)
let re = Regex::new(r#"/e/([^/]*)"#).unwrap(); .header(
let capture = re.captures(&html).unwrap(); "user-agent",
let id = &capture[1]; "Mozilla/5.0 (X11; Linux x86_64; rv:99.0) Gecko/20100101 Firefox/100.0",
let id = format!("id={}%3D&ac=0", id); )
let json = get_post(&id); .body(())
let re = Regex::new(r#"hls": \["(.*)","#).unwrap(); .unwrap();
let capture = re.captures(&json).unwrap(); let json = req.send().unwrap().text().unwrap();
let link = &capture[1]; let json: serde_json::Value = serde_json::from_str(&json).unwrap();
//return the link let mut url = String::new();
link.to_string() std::fs::write("test.json", json.to_string()).unwrap();
let mut subtitle = String::new();
let _error_vec = Vec::new();
let sub_array = json["subtitles"].as_array().unwrap_or(&_error_vec);
for i in 0..sub_array.len() {
//set subtitle to lang = English
if json["subtitles"][i]["lang"].as_str().unwrap_or("null") == "English" {
subtitle = json["subtitles"][i]["url"]
.as_str()
.unwrap_or("null")
.to_string();
// add \ before the first : in the url
subtitle = subtitle.replace(":", "\\:");
}
}
let mut highest_quality = 0;
for i in 0..json["sources"].as_array().unwrap().len() {
let quality = json["sources"][i]["quality"]
.as_str()
.unwrap()
.replace("p", "")
.parse::<i32>()
.unwrap_or(0);
if quality > highest_quality {
highest_quality = quality;
url = json["sources"][i]["url"].as_str().unwrap().to_string();
}
}
(url.to_string(), subtitle)
} }
pub fn get_image(url: &str, path: &str) { pub fn get_image(url: &str, path: &str) {

View File

@@ -40,54 +40,6 @@ pub fn get_token() -> String {
token 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 //get the user id from the token
fn get_user_id(token: &str) -> i32 { fn get_user_id(token: &str) -> i32 {
const QUERY: &str = "query { const QUERY: &str = "query {

View File

@@ -29,14 +29,14 @@ fn main() {
if let Some(arg) = std::env::args().nth(count + 1) { if let Some(arg) = std::env::args().nth(count + 1) {
//get the next argument and see if it is = to gogo of vrv //get the next argument and see if it is = to gogo of vrv
match arg.as_str() { match arg.as_str() {
"vrv" | "gogo" => { "zoro" | "gogoanime" => {
provider = arg; provider = arg;
count += 1; count += 1;
} }
&_ => provider = "gogo".to_string(), &_ => provider = "gogoanime".to_string(),
} }
} else { } else {
provider = "vrv".to_string(); provider = "zoro".to_string();
} }
} }
"--reader" | "-R" => { "--reader" | "-R" => {
@@ -49,9 +49,14 @@ fn main() {
} }
&_ => reader = "bat".to_string(), &_ => reader = "bat".to_string(),
} }
} else { } else {
provider = "glow".to_string(); provider = "glow".to_string();
} }
} else {
provider = "gogo".to_string();
} }
"--cast" | "-C" => { "--cast" | "-C" => {
if let Some(arg) = std::env::args().nth(count + 1) { if let Some(arg) = std::env::args().nth(count + 1) {
@@ -132,13 +137,13 @@ fn print_help() {
); );
println!( println!(
"if no provider is entered it will default to {}", "if no provider is entered it will default to {}",
"vrv".green() "gogo".green()
); );
println!( println!(
"if the -r argument is not used it will default to {}", "if the -r argument is not used it will default to {}",
"gogo".green() "zoro".green()
); );
println!("the providers are {} or {}", "gogo".green(), "vrv".green()); println!("the providers are {} or {}", "gogoanime".green(), "zoro".green());
println!(""); println!("");
println!("reader:\t\t{}", format_args!("{}", "-R --reader".red())); println!("reader:\t\t{}", format_args!("{}", "-R --reader".red()));
println!( println!(

View File

@@ -1,9 +1,9 @@
use crate::ui::{app::app::KamiApp, input::InputMode, list::StatefulList}; use crate::ui::{app::app::KamiApp, input::InputMode, list::StatefulList};
use crate::anime::player::{open_cast, open_video}; use crate::anime::player::{open_cast, open_video};
use crate::anime::scraper::{get_anime_info, get_anime_link, get_animes, get_image}; use crate::anime::scraper::{get_episode_link, get_episodes, get_image, search_anime};
use crate::anime::trackers::{ use crate::anime::trackers::{
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, write_an_progress,
}; };
@@ -29,6 +29,7 @@ pub struct App {
input_mode: InputMode, input_mode: InputMode,
/// History of recorded messages /// History of recorded messages
messages: StatefulList<String>, messages: StatefulList<String>,
episodes: (Vec<String>, Vec<String>),
title: String, title: String,
link: String, link: String,
ep: u64, ep: u64,
@@ -39,6 +40,39 @@ pub struct App {
pub cast: (bool, String), pub cast: (bool, String),
} }
impl<'a> App {
fn change_image(&mut self) {
//save as f32
let (width, height) = terminal_size().to_owned();
let width = width as f32;
let height = height as f32;
let sixel_support = viuer::is_sixel_supported();
let config = match sixel_support {
true => Config {
x: ((width / 2.0) + 1.0).round() as u16,
y: 2,
width: Some((width / 1.3).round() as u32),
height: Some((height * 1.5) as u32),
restore_cursor: true,
..Default::default()
},
false => Config {
x: ((width / 2.0) + 1.0).round() as u16,
y: 2,
width: Some(((width / 2.0) - 4.0).round() as u32),
height: Some((height / 1.3).round() as u32),
restore_cursor: true,
..Default::default()
},
};
let config_path = dirs::config_dir().unwrap().join("kami");
let image_path = config_path.join("tmp.jpg");
get_image(&self.image, &image_path.to_str().unwrap());
print_from_file(image_path, &config).expect("Image printing failed.");
}
}
impl<'a> KamiApp for App { impl<'a> KamiApp for App {
fn new() -> Self { fn new() -> Self {
App { App {
@@ -47,6 +81,7 @@ impl<'a> KamiApp for App {
image: String::new(), image: String::new(),
input_mode: InputMode::Normal, input_mode: InputMode::Normal,
messages: StatefulList::with_items(Vec::new()), messages: StatefulList::with_items(Vec::new()),
episodes: (Vec::new(), Vec::new()),
title: String::new(), title: String::new(),
link: String::new(), link: String::new(),
ep: 0, ep: 0,
@@ -60,36 +95,6 @@ impl<'a> KamiApp for App {
fn run<B: Backend>(&mut self, terminal: &mut Terminal<B>) -> io::Result<()> { fn run<B: Backend>(&mut self, terminal: &mut Terminal<B>) -> io::Result<()> {
let mut ep_select = false; let mut ep_select = false;
fn change_image(app: &App) {
//save as f32
let (width, height) = terminal_size().to_owned();
let width = width as f32;
let height = height as f32;
let sixel_support = viuer::is_sixel_supported();
let config = match sixel_support {
true => Config {
x: ((width / 2.0) + 1.0).round() as u16,
y: 2,
width: Some((width / 1.3).round() as u32),
height: Some((height * 1.5) as u32),
restore_cursor: true,
..Default::default()
},
false => Config {
x: ((width / 2.0) + 1.0).round() as u16,
y: 2,
width: Some(((width / 2.0) - 4.0).round() as u32),
height: Some((height / 1.3).round() as u32),
restore_cursor: true,
..Default::default()
},
};
let config_path = dirs::config_dir().unwrap().join("kami");
let image_path = config_path.join("tmp.jpg");
get_image(&app.image, &image_path.to_str().unwrap());
print_from_file(image_path, &config).expect("Image printing failed.");
}
self.messages.items.clear(); self.messages.items.clear();
for anime in &self.animes.1 { for anime in &self.animes.1 {
self.messages.push(anime.to_string()); self.messages.push(anime.to_string());
@@ -110,8 +115,6 @@ impl<'a> KamiApp for App {
} }
KeyCode::Left => self.messages.unselect(), KeyCode::Left => self.messages.unselect(),
KeyCode::Char('h') => self.messages.unselect(), KeyCode::Char('h') => self.messages.unselect(),
KeyCode::Char('g') => self.messages.begin(),
KeyCode::Char('G') => self.messages.end(),
KeyCode::Down => match ep_select { KeyCode::Down => match ep_select {
true => { true => {
self.messages.next(); self.messages.next();
@@ -120,7 +123,7 @@ impl<'a> KamiApp for App {
self.messages.next(); self.messages.next();
let selected = self.messages.state.selected(); let selected = self.messages.state.selected();
self.image = self.animes.2[selected.unwrap()].clone(); self.image = self.animes.2[selected.unwrap()].clone();
change_image(&self); self.change_image();
} }
}, },
KeyCode::Char('j') => match ep_select { KeyCode::Char('j') => match ep_select {
@@ -131,7 +134,7 @@ impl<'a> KamiApp for App {
self.messages.next(); self.messages.next();
let selected = self.messages.state.selected(); let selected = self.messages.state.selected();
self.image = self.animes.2[selected.unwrap()].clone(); self.image = self.animes.2[selected.unwrap()].clone();
change_image(&self); self.change_image();
} }
}, },
KeyCode::Up => match ep_select { KeyCode::Up => match ep_select {
@@ -142,7 +145,7 @@ impl<'a> KamiApp for App {
self.messages.previous(); self.messages.previous();
let selected = self.messages.state.selected(); let selected = self.messages.state.selected();
self.image = self.animes.2[selected.unwrap()].clone(); self.image = self.animes.2[selected.unwrap()].clone();
change_image(&self); self.change_image();
} }
}, },
KeyCode::Char('k') => match ep_select { KeyCode::Char('k') => match ep_select {
@@ -153,7 +156,7 @@ impl<'a> KamiApp for App {
self.messages.previous(); self.messages.previous();
let selected = self.messages.state.selected(); let selected = self.messages.state.selected();
self.image = self.animes.2[selected.unwrap()].clone(); self.image = self.animes.2[selected.unwrap()].clone();
change_image(&self); self.change_image();
} }
}, },
//if KeyCode::Enter => { //if KeyCode::Enter => {
@@ -162,9 +165,14 @@ impl<'a> KamiApp for App {
self.progress = 0; self.progress = 0;
let selected = self.messages.state.selected(); let selected = self.messages.state.selected();
self.title = self.messages.items[selected.unwrap()].clone(); self.title = self.messages.items[selected.unwrap()].clone();
self.link = self.animes.0[selected.unwrap()].clone(); self.anime_id = self.animes.0[selected.unwrap()]
let anime_info = get_anime_info(&self.animes.0[selected.unwrap()]); .clone()
self.anime_id = get_anime_id(anime_info.0); .parse::<i32>()
.unwrap();
self.episodes = get_episodes(
&self.animes.0[selected.unwrap()].parse::<i32>().unwrap(),
&self.provider,
);
self.messages.items.clear(); self.messages.items.clear();
if self.token == "local" || self.anime_id == 0 { if self.token == "local" || self.anime_id == 0 {
self.progress = get_an_progress(&self.title) as i32; self.progress = get_an_progress(&self.title) as i32;
@@ -174,13 +182,18 @@ impl<'a> KamiApp for App {
get_user_anime_progress(self.anime_id, self.token.as_str()); get_user_anime_progress(self.anime_id, self.token.as_str());
self.messages.state.select(Some(self.progress as usize)); self.messages.state.select(Some(self.progress as usize));
} }
if anime_info.1 == 1 { if self.episodes.0.len() == 1 {
let link = get_anime_link(&self.link, 1); let link =
get_episode_link(&self.episodes.1[0], &self.provider);
if !self.cast.0 { if !self.cast.0 {
open_video((link, format!("{} Episode 1", &self.title))); open_video((
link.0,
format!("{} Episode 1", &self.title),
link.1,
));
} else { } else {
open_cast( open_cast(
(link, format!("{} Episode 1", &self.title)), (link.1, format!("{} Episode 1", &self.title)),
&self.cast.1, &self.cast.1,
) )
} }
@@ -188,7 +201,7 @@ impl<'a> KamiApp for App {
let image_url = self.animes.2[selected.unwrap()].clone(); let image_url = self.animes.2[selected.unwrap()].clone();
if self.token == "local" || self.anime_id == 0 { if self.token == "local" || self.anime_id == 0 {
write_an_progress( write_an_progress(
(&self.title, &self.link, &image_url), (&self.title, &self.anime_id.to_string(), &image_url),
&1, &1,
); );
} else { } else {
@@ -198,13 +211,17 @@ impl<'a> KamiApp for App {
self.token.as_str(), self.token.as_str(),
); );
write_an_progress( write_an_progress(
(&self.title, &self.link, &image_url), (&self.title, &self.anime_id.to_string(), &image_url),
&1, &1,
); );
} }
} else { } else {
for ep in 1..anime_info.1 + 1 { for ep in 1..self.episodes.1.len() + 1 {
self.messages.push(format!("Episode {}", ep)); self.messages.push(format!(
"Episode {}: {}",
ep,
self.episodes.0[ep - 1]
));
} }
ep_select = true; ep_select = true;
} }
@@ -216,17 +233,23 @@ impl<'a> KamiApp for App {
.nth(selected.unwrap()) .nth(selected.unwrap())
.unwrap() .unwrap()
.replace("Episode ", "") .replace("Episode ", "")
.split(":")
.collect::<Vec<&str>>()[0]
.parse::<u64>() .parse::<u64>()
.unwrap(); .unwrap();
let link = get_anime_link(&self.link, self.ep); let link = get_episode_link(
&self.episodes.1[self.ep as usize - 1],
&self.provider,
);
if !self.cast.0 { if !self.cast.0 {
open_video(( open_video((
link, link.0,
format!("{} Episode {}", &self.title, self.ep), format!("{} Episode {}", &self.title, self.ep),
link.1,
)); ));
} else { } else {
open_cast( open_cast(
(link, format!("{} Episode {}", &self.title, self.ep)), (link.0, format!("{} Episode {}", &self.title, self.ep)),
&self.cast.1, &self.cast.1,
) )
} }
@@ -234,7 +257,7 @@ impl<'a> KamiApp for App {
if self.ep > self.progress as u64 { if self.ep > self.progress as u64 {
if self.token == "local" || self.anime_id == 0 { if self.token == "local" || self.anime_id == 0 {
write_an_progress( write_an_progress(
(&self.title, &self.link, &image_url), (&self.title, &self.anime_id.to_string(), &image_url),
&self.ep, &self.ep,
); );
} else { } else {
@@ -244,7 +267,7 @@ impl<'a> KamiApp for App {
self.token.as_str(), self.token.as_str(),
); );
write_an_progress( write_an_progress(
(&self.title, &self.link, &image_url), (&self.title, &self.anime_id.to_string(), &image_url),
&self.ep, &self.ep,
); );
} }
@@ -257,7 +280,7 @@ impl<'a> KamiApp for App {
InputMode::Editing => match key.code { InputMode::Editing => match key.code {
KeyCode::Enter => { KeyCode::Enter => {
//push self.input into self.messages with ' //push self.input into self.messages with '
self.animes = get_animes(self.input.drain(..).collect()); self.animes = search_anime(self.input.drain(..).collect());
self.messages.items.clear(); self.messages.items.clear();
for anime in &self.animes.1 { for anime in &self.animes.1 {
self.messages.push(anime.to_string()); self.messages.push(anime.to_string());