added provider switching and soft subs

This commit is contained in:
Zastian Pretorius
2023-01-31 16:47:08 +00:00
parent 634643657e
commit edbebf4b4e
4 changed files with 73 additions and 33 deletions

View File

@@ -261,6 +261,7 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
.unwrap(); .unwrap();
app.episodes = get_episodes( app.episodes = get_episodes(
&app.animes.0[selected.unwrap()].parse::<i32>().unwrap(), &app.animes.0[selected.unwrap()].parse::<i32>().unwrap(),
&app.provider,
); );
app.messages.items.clear(); app.messages.items.clear();
if app.token == "local" || app.anime_id == 0 { if app.token == "local" || app.anime_id == 0 {
@@ -272,12 +273,16 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
app.messages.state.select(Some(app.progress as usize)); app.messages.state.select(Some(app.progress as usize));
} }
if app.episodes.0.len() == 1 { if app.episodes.0.len() == 1 {
let link = get_episode_link(&app.episodes.1[0]); let link = get_episode_link(&app.episodes.1[0], &app.provider);
if !app.cast.0 { if !app.cast.0 {
open_video((link, format!("{} Episode 1", &app.title))); open_video((
link.0,
format!("{} Episode 1", &app.title),
link.1,
));
} else { } else {
open_cast( open_cast(
(link, format!("{} Episode 1", &app.title)), (link.1, format!("{} Episode 1", &app.title)),
&app.cast.1, &app.cast.1,
) )
} }
@@ -317,12 +322,19 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
.collect::<Vec<&str>>()[0] .collect::<Vec<&str>>()[0]
.parse::<u64>() .parse::<u64>()
.unwrap(); .unwrap();
let link = get_episode_link(&app.episodes.1[app.ep as usize - 1]); let link = get_episode_link(
&app.episodes.1[app.ep as usize - 1],
&app.provider,
);
if !app.cast.0 { if !app.cast.0 {
open_video((link, format!("{} Episode {}", &app.title, app.ep))); open_video((
link.0,
format!("{} Episode {}", &app.title, app.ep),
link.1,
));
} else { } else {
open_cast( open_cast(
(link, format!("{} Episode {}", &app.title, app.ep)), (link.0, format!("{} Episode {}", &app.title, app.ep)),
&app.cast.1, &app.cast.1,
) )
} }

View File

@@ -8,7 +8,8 @@ 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)) {
if link.2 == "null" {
let title = link.1; let title = link.1;
let title = title.replace("-", " "); let title = title.replace("-", " ");
let arg: String = format!("--force-media-title={}", title); let arg: String = format!("--force-media-title={}", title);
@@ -17,7 +18,18 @@ pub fn open_video(link: (String, String)) {
.arg(arg) .arg(arg)
.output() .output()
.expect("failed to open mpv"); .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

@@ -43,11 +43,11 @@ pub fn search_anime(query: String) -> (Vec<String>, Vec<String>, Vec<String>) {
(ids, titles, images) (ids, titles, images)
} }
pub fn get_episodes(id: &i32) -> (Vec<String>, Vec<String>) { pub fn get_episodes(id: &i32, provider: &str) -> (Vec<String>, Vec<String>) {
let req = Request::builder() let req = Request::builder()
.uri(format!( .uri(format!(
"https://api.consumet.org/meta/anilist/info/{}?provider=gogoanime", "https://api.consumet.org/meta/anilist/info/{}?provider={}",
id id, provider
)) ))
.redirect_policy(isahc::config::RedirectPolicy::Follow) .redirect_policy(isahc::config::RedirectPolicy::Follow)
.header( .header(
@@ -67,11 +67,11 @@ pub fn get_episodes(id: &i32) -> (Vec<String>, Vec<String>) {
(titles, ids) (titles, ids)
} }
pub fn get_episode_link(ep_id: &str) -> String { pub fn get_episode_link(ep_id: &str, provider: &str) -> (String, String) {
let req = Request::builder() let req = Request::builder()
.uri(format!( .uri(format!(
"https://api.consumet.org/meta/anilist/watch/{}?provider=gogoanime", "https://api.consumet.org/meta/anilist/watch/{}?provider={}",
ep_id ep_id, provider
)) ))
.redirect_policy(isahc::config::RedirectPolicy::Follow) .redirect_policy(isahc::config::RedirectPolicy::Follow)
.header( .header(
@@ -84,17 +84,33 @@ pub fn get_episode_link(ep_id: &str) -> String {
let json: serde_json::Value = serde_json::from_str(&json).unwrap(); let json: serde_json::Value = serde_json::from_str(&json).unwrap();
let url = ""; let url = "";
std::fs::write("test.json", json.to_string()).unwrap(); 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(":", "\\:");
}
}
for i in 0..json["sources"].as_array().unwrap().len() { 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"] if json["sources"][i]["quality"]
.as_str() .as_str()
.unwrap() .unwrap()
.contains("1080") .contains("1080")
{ {
return json["sources"][i]["url"].as_str().unwrap().to_string(); return (
json["sources"][i]["url"].as_str().unwrap().to_string(),
subtitle,
);
} }
} }
url.to_string() (url.to_string(), subtitle)
} }
pub fn get_image(url: &str, path: &str) { pub fn get_image(url: &str, path: &str) {

View File

@@ -29,17 +29,17 @@ fn main() {
if arg == "--provider" || arg == "-r" { if arg == "--provider" || arg == "-r" {
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
if arg == "vrv" { if arg == "zoro" {
provider = "vrv".to_string(); provider = "zoro".to_string();
count += 1; count += 1;
} else if arg == "gogo" { } else if arg == "gogo" {
provider = "gogo".to_string(); provider = "gogoanime".to_string();
count += 1; count += 1;
} else { } else {
provider = "gogo".to_string(); provider = "zoro".to_string();
} }
} else { } else {
provider = "vrv".to_string(); provider = "zoro".to_string();
} }
} }
if arg == "--cast" || arg == "-C" { if arg == "--cast" || arg == "-C" {
@@ -122,13 +122,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 {}", "gogo".green(), "zoro".green());
println!(""); println!("");
println!("help:\t\t{}", format_args!("{}", "-h --help".red())); println!("help:\t\t{}", format_args!("{}", "-h --help".red()));
//kill the program //kill the program