refactor: better file hierarchy

This commit is contained in:
justchokingaround
2022-07-18 04:05:27 +02:00
13 changed files with 449 additions and 126 deletions

86
src/anime/anime.rs Normal file
View File

@@ -0,0 +1,86 @@
use crate::{string_input,int_input};
use crate::{anime_names,anime_ep_range,anime_link};
use crate::open_video;
use crate::main;
use colored::Colorize;
//use crate
pub fn anime_stream(first_run: bool) {
let query = if std::env::args().len() > 2 && first_run {
std::env::args().nth(2).unwrap()
} else {
string_input("Search anime: ")
};
let anime_list = anime_names(&query);
let mut count = 0;
print!("\x1B[2J\x1B[1;1H");
anime_list.iter().for_each(|anime| {
if count % 2 == 0 {
println!(
"({})\t{}",
format_args!("{}", count.to_string().blue()),
format_args!("{}", anime.blue())
);
} else {
println!(
"({})\t{}",
format_args!("{}", count.to_string().yellow()),
format_args!("{}", anime.yellow())
);
}
count += 1;
});
let mut anime_num: usize = usize::MAX;
while anime_num == usize::max_value() || anime_num > anime_list.len() {
anime_num = int_input("Enter anime number: ");
if anime_num > anime_list.len() {
println!("Invalid anime number");
}
}
let title = &anime_list[anime_num];
let ep_range = anime_ep_range(title);
// if there is only one episode, then don't ask user to choose episode
if ep_range == 1 {
let link = anime_link(title, 1);
open_video(link);
main();
} else {
println!("select episode 1-{}: ", ep_range);
let mut ep_num: usize = usize::MAX;
while ep_num == usize::max_value() || ep_num > ep_range as usize {
ep_num = int_input("Enter episode number: ");
if ep_num > ep_range as usize {
println!("Invalid episode number");
}
}
loop{
let link = anime_link(title, ep_num as u64);
open_video(link);
println!("{}","n: next episode".green());
println!("{}","p: previous episode".yellow());
println!("{}","s: search another anime".green());
println!("{}","q: quit".red());
let input = string_input("Enter command: ");
if input == "n" {
if ep_num == ep_range as usize {
println!("No more episodes");
} else {
ep_num += 1;
}
} else if input == "p" {
if ep_num == 1 {
println!("No previous episodes");
} else {
ep_num -= 1;
}
} else if input == "s" {
//remove all the arguments
anime_stream(false);
} else if input == "q" {
std::process::exit(0);
}
else{
println!("Invalid command");
}
}
}
}

View File

@@ -1,2 +1,3 @@
pub mod player;
pub mod scraper;
pub mod anime;

View File

@@ -66,7 +66,7 @@ pub fn anime_ep_range(anime_name: &str) -> u16 {
.unwrap_or(0)
}
pub fn anime_link(title: &str, ep: u16) -> (String, String) {
pub fn anime_link(title: &str, ep: u64) -> (String, String) {
let url = format!("https://animixplay.to/v1/{}", title);
let html = get_anime_html(&url);
let re = Regex::new(r#"(?m)\?id=([^&]+)"#).unwrap();

View File

@@ -1,5 +1,4 @@
use std::io::{self, Write};
pub fn string_input(prompt: &str) -> String {
print!("{}", prompt);
let mut input = String::new();
@@ -17,15 +16,21 @@ pub fn int_input(prompt: &str) -> usize {
io::stdin()
.read_line(&mut input)
.expect("Error reading from STDIN");
input.trim().parse::<usize>().unwrap()
//try to parse the input as usize else return max usize
match input.trim().parse::<usize>() {
Ok(i) => i,
Err(_) => {
usize::max_value()
}
}
}
pub fn u16_input(prompt: &str) -> u16 {
print!("{}", prompt);
let mut input = String::new();
let _ = io::stdout().flush();
io::stdin()
.read_line(&mut input)
.expect("Error reading from STDIN");
input.trim().parse::<u16>().unwrap()
}
//pub fn u16_input(prompt: &str) -> u16 {
// print!("{}", prompt);
// let mut input = String::new();
// let _ = io::stdout().flush();
// io::stdin()
// .read_line(&mut input)
// .expect("Error reading from STDIN");
// input.trim().parse::<u16>().unwrap()
//}

31
src/ln/ln.rs Normal file
View File

@@ -0,0 +1,31 @@
use std::fs::File;
use std::io::Write;
use crate::{search_ln,chapter_selector,get_full_text,open_bat};
pub fn ln_read(){
let ln_url = search_ln();
let mut selected_page = 1;
loop {
//make empty tuple called chapter_url with (String, u32, u32)
let chapter_url = chapter_selector(&ln_url, selected_page);
selected_page = chapter_url.1;
let full_text = get_full_text(&chapter_url.0);
if cfg!(target_os = "windows"){
use dirs::home_dir;
let mut home = format!("{:?}",home_dir()).replace("\\\\","/");
home.drain(0..6);
home.drain(home.len()-2..home.len());
let mut file = File::create(format!("{}/AppData/Roaming/log_e",home)).expect("Unable to create file");
file.write_all(full_text.as_bytes())
.expect("Unable to write to file");
file.sync_all().expect("Unable to sync file");
}else{
let mut file = File::create("/tmp/log_e").expect("Unable to create file");
file.write_all(full_text.as_bytes())
.expect("Unable to write to file");
file.sync_all().expect("Unable to sync file");
};
//open temp.txt in cat for user to read
let _com = open_bat();
print!("\x1B[2J\x1B[1;1H");
}
}

View File

@@ -11,8 +11,7 @@ use crate::{
};
pub fn chapter_selector(ln_url: &str, mut selected_page: u32) -> (String, u32) {
let exit = false;
while exit == false {
loop {
let ln_html = ln::scraper::get_html(ln_url);
let ln_id = get_ln_id(&ln_html);
let ln_last_page = get_ln_last_page(&ln_html);
@@ -22,15 +21,16 @@ pub fn chapter_selector(ln_url: &str, mut selected_page: u32) -> (String, u32) {
let mut count = 0;
ln_chapters.into_iter().for_each(|chaprer| {
if count % 2 == 0 {
println!("({})\t{}", count, format_args!("{}", chaprer.blue()));
println!("({})\t{}", count.to_string().blue(), format_args!("{}", chaprer.blue()));
} else {
println!("({})\t{}", count, format_args!("{}", chaprer.yellow()));
println!("({})\t{}", count.to_string().yellow(), format_args!("{}", chaprer.yellow()));
}
count += 1;
});
println!("(n)\t{}", "Go to next page".red());
println!("(b)\t{}", "Go to previous page".red());
println!("(q)\t{}", "go back to main menu".red());
println!("{}\t{}","n:".green(), "Go to next page".green());
println!("{}\t{}","b:".yellow(), "Go to previous page".yellow());
println!("{}\t{}","s:".green(), "Search another title".green());
println!("{}\t{}","q:".red(), "quit".red());
let chapter_number = string_input("Which chapter do you want to read? ");
if chapter_number == "n" && selected_page < ln_last_page.parse::<u32>().unwrap() {
selected_page += 1;
@@ -39,14 +39,23 @@ pub fn chapter_selector(ln_url: &str, mut selected_page: u32) -> (String, u32) {
selected_page -= 1;
print!("\x1B[2J\x1B[1;1H");
} else if chapter_number == "q" {
print!("\x1B[2J\x1B[1;1H");
std::process::exit(0);
} else if chapter_number == "s" {
main();
} else {
}
else {
let chapter_number = chapter_number.trim().to_string();
let chapter_number = chapter_number.parse::<usize>().unwrap();
let chapter_url = &ln_chapters_urls[chapter_number];
let mut _chapter_number_int = 0;
if chapter_number.parse::<u32>().is_ok() {
_chapter_number_int = chapter_number.parse::<u32>().unwrap();
} else {
println!("{}", "Invalid option".red());
continue;
}
let chapter_url = &ln_chapters_urls[_chapter_number_int as usize];
let chapter_url = chapter_url.trim().to_string();
return (chapter_url, selected_page);
}
}
("".to_string(), 1)
}

View File

@@ -2,3 +2,4 @@ pub mod menu;
pub mod open_text;
pub mod scraper;
pub mod search;
pub mod ln;

View File

@@ -1,11 +1,39 @@
use std::io::Result;
use std::process::{Command, ExitStatus};
use std::process::{Command, ExitStatus, Stdio};
#[allow(unused_assignments)]
pub fn open_bat() -> Result<ExitStatus> {
let termsize::Size {rows: _, cols} = termsize::get().unwrap();
let mut path = String::new();
if cfg!(target_os = "windows"){
use dirs::home_dir;
let mut home = format!("{:?}",home_dir()).replace("\\\\","/");
home.drain(0..6);
home.drain(home.len()-2..home.len());
path = format!("{}/AppData/Roaming/log_e",home).to_string();
}
else{
path = "/tmp/log_e".to_string();
}
let soft_wrap = match Command::new("fold")
.arg("-s")
.arg("-w")
.arg((cols - 9).to_string())
.arg(path)
.stdout(Stdio::piped())
.spawn()
{
Err(why) => panic!("couldn't spawn wc: {}", why),
Ok(soft_wrap) => soft_wrap,
};
Command::new("bat")
.arg("--paging")
.arg("always")
.arg("/tmp/log_e")
.arg("-l")
.arg("markdown")
.stdin(soft_wrap.stdout.unwrap())
.spawn()?
.wait()
}

View File

@@ -23,19 +23,15 @@ pub fn search_ln() -> String {
let mut count = 0;
ln_titles.into_iter().for_each(|ln| {
if count % 2 == 0 {
println!("({})\t{}", count, format_args!("{}", ln.blue()));
println!("({})\t{}", count.to_string().blue(), format_args!("{}", ln.blue()));
} else {
println!("({})\t{}", count, format_args!("{}", ln.yellow()));
println!("({})\t{}", count.to_string().yellow(), format_args!("{}", ln.yellow()));
}
count += 1;
});
println!("(n)\t{}", "Search another title".red());
let mut ln_number = String::new();
std::io::stdin()
.read_line(&mut ln_number)
.expect("Failed to read line");
ln_number = ln_number.trim().to_string();
if ln_number != "n" {
println!("{}\t{}","s:".green(), "Search another title".green());
let ln_number = string_input("Enter an option: ");
if ln_number != "s" && ln_number.parse::<usize>().is_ok() {
let ln_number = ln_number.trim().to_string();
let ln_number = ln_number.parse::<usize>().unwrap();
let ln_url = &ln_urls[ln_number];
@@ -43,6 +39,8 @@ pub fn search_ln() -> String {
_is_n = true;
print!("\x1B[2J\x1B[1;1H");
return ln_url;
} else {
print!("invalid input");
}
print!("\x1B[2J\x1B[1;1H");
}

View File

@@ -2,18 +2,16 @@ mod anime;
mod helpers;
mod ln;
use std::fs::File;
use std::io::Write;
use anime::anime::anime_stream;
use colored::Colorize;
use ln::scraper::get_ln_next_page;
use ln::{scraper::get_ln_next_page, ln::ln_read};
use ln::search::search_ln;
use crate::anime::{
player::open_video,
scraper::{anime_ep_range, anime_link, anime_names},
};
use crate::helpers::take_input::{int_input, string_input, u16_input};
use crate::helpers::take_input::{int_input, string_input};
use crate::ln::{menu::chapter_selector, open_text::open_bat, scraper::get_full_text};
fn main() {
@@ -31,63 +29,9 @@ fn main() {
std::process::exit(0);
}
if _arg == "l" {
let ln_url = search_ln();
let mut selected_page = 1;
loop {
//make empty tuple called chapter_url with (String, u32, u32)
let chapter_url = chapter_selector(&ln_url, selected_page);
selected_page = chapter_url.1;
let full_text = get_full_text(&chapter_url.0);
//write full_text to file called temp.txt
let mut file = File::create("/tmp/log_e").expect("Unable to create file");
file.write_all(full_text.as_bytes())
.expect("Unable to write to file");
//close file
file.sync_all().expect("Unable to sync file");
//open temp.txt in cat for user to read
let _com = open_bat();
print!("\x1B[2J\x1B[1;1H");
}
ln_read();
} else if _arg == "a" {
let query = if std::env::args().len() > 2 {
std::env::args().nth(2).unwrap()
} else {
string_input("Enter query: ")
};
let anime_list = anime_names(&query);
let mut count = 0;
print!("\x1B[2J\x1B[1;1H");
anime_list.iter().for_each(|anime| {
if count % 2 == 0 {
println!(
"({})\t{}",
format_args!("{}", count.to_string().blue()),
format_args!("{}", anime.blue())
);
} else {
println!(
"({})\t{}",
format_args!("{}", count.to_string().yellow()),
format_args!("{}", anime.yellow())
);
}
count += 1;
});
let anime_num = int_input("Enter anime number: ");
let title = &anime_list[anime_num];
let ep_range = anime_ep_range(title);
// if there is only one episode, then don't ask user to choose episode
if ep_range == 1 {
let link = anime_link(title, 1);
open_video(link);
main();
} else {
println!("select episode 1-{}: ", ep_range);
let ep_num = u16_input("Enter episode number: ");
let link = anime_link(title, ep_num);
open_video(link);
main();
}
anime_stream(true)
} else {
println!("Invalid argument");
}