refactor: restructure

This commit is contained in:
justchokingaround
2022-07-10 19:00:23 +02:00
parent bbd99ad5f7
commit e99dc19ea3
12 changed files with 522 additions and 413 deletions

52
src/ln/menu.rs Normal file
View File

@@ -0,0 +1,52 @@
use colored::Colorize;
use crate::{
helpers::take_input::string_input,
ln::{
self,
scraper::{get_ln_chapters_urls, get_ln_id, get_ln_last_page},
search::get_ln_chapters,
},
main, page_selector,
};
pub fn chapter_selector(ln_url: &str, mut selected_page: u32) -> (String, u32) {
let exit = false;
while exit == false {
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);
let ln_page_html = page_selector(&ln_id, selected_page);
let ln_chapters = get_ln_chapters(&ln_page_html);
let ln_chapters_urls = get_ln_chapters_urls(&ln_page_html);
let mut count = 0;
ln_chapters.into_iter().for_each(|chaprer| {
if count % 2 == 0 {
println!("({})\t{}", count, format_args!("{}", chaprer.blue()));
} else {
println!("({})\t{}", count, 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());
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;
print!("\x1B[2J\x1B[1;1H");
} else if chapter_number == "b" && selected_page > 1 {
selected_page -= 1;
print!("\x1B[2J\x1B[1;1H");
} else if chapter_number == "q" {
main();
} 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 chapter_url = chapter_url.trim().to_string();
return (chapter_url, selected_page);
}
}
("".to_string(), 1)
}

11
src/ln/open_text.rs Normal file
View File

@@ -0,0 +1,11 @@
use std::io::Result;
use std::process::{Command, ExitStatus};
pub fn open_bat() -> Result<ExitStatus> {
Command::new("bat")
.arg("--paging")
.arg("always")
.arg("/tmp/log_e")
.spawn()?
.wait()
}

104
src/ln/scraper.rs Normal file
View File

@@ -0,0 +1,104 @@
use isahc::{ReadResponseExt, Request, RequestExt};
use regex::Regex;
use crate::helpers::fixing_text::fix_html_encoding;
//gets the full html of the page
pub fn get_html(url: &str) -> String {
let req = Request::builder()
.uri(url)
.header(
"user-agent",
"Mozilla/5.0 (X11; Linux x86_64; rv:99.0) Gecko/20100101 Firefox/100.0",
)
.body(())
.unwrap();
req.send().unwrap().text().unwrap()
}
//using isahc::prelude::* make a php reqest to get the next page of the ln
pub fn get_ln_next_page(ln_id: &str, page: &str) -> String {
let url = "https://readlightnovels.net/wp-admin/admin-ajax.php".to_string();
let form = format!(
"action=tw_ajax&type=pagination&id={}.html&page={}",
ln_id, page
);
//let mut resp = isahc::post(&url,form).unwrap();
let req = Request::builder()
.method("POST")
.uri(url)
.header(
"user-agent",
"Mozilla/5.0 (X11; Linux x86_64; rv:99.0) Gecko/20100101 Firefox/100.0",
)
.body(form)
.unwrap();
req.send().unwrap().text().unwrap()
}
pub fn get_full_text(chapter_url: &str) -> String {
let ln_text = get_ln_text(chapter_url);
let mut full_text: String = String::new();
for line in ln_text {
let text = format!("{}\n\n", line);
full_text.push_str(&text);
}
full_text
}
//gets the chapter urls from the html and returns it as a vector of the chapter's href
pub fn get_ln_chapters_urls(html: &str) -> Vec<String> {
let re = Regex::new(r#"href=\\"(h.*?)""#).unwrap();
let mut ln_list: Vec<String> = Vec::new();
for cap in re.captures_iter(html) {
ln_list.push(cap.get(1).unwrap().as_str().trim().to_string());
}
ln_list = url_clean(&ln_list);
ln_list
} // take a vector of srings called ln_chapters_url and remove all the \
pub fn url_clean(ln_chapters_url: &Vec<String>) -> Vec<String> {
let mut ln_chapters_url_new: Vec<String> = Vec::new();
for ln in ln_chapters_url {
let ln = ln.replace('\\', "");
ln_chapters_url_new.push(ln);
}
ln_chapters_url_new
}
//take a html string and return the ln id
pub fn get_ln_last_page(html: &str) -> String {
let re =
Regex::new(r#"(?m)<a data-page="([0-9]+)" href="[^"]*" title="[0-9]+">Last</a>"#).unwrap();
let mut ln_last_page: String = String::new();
for cap in re.captures_iter(html) {
ln_last_page = cap.get(1).unwrap().as_str().to_string();
}
ln_last_page
}
//take a html string and return the ln id
pub fn get_ln_id(html: &str) -> String {
let re = Regex::new(r#"(?m)^\s*<input id="id_post" type="hidden" value="([0-9]+)">"#).unwrap();
let mut ln_id: String = String::new();
for cap in re.captures_iter(html) {
ln_id = cap.get(1).unwrap().as_str().to_string();
}
ln_id
}
pub fn get_ln_text(chapter_url: &str) -> Vec<String> {
let mut resp = isahc::get(chapter_url).unwrap();
let html = resp.text().unwrap();
let re = Regex::new(r#"(?m)<p>(.*?)</p>"#).unwrap();
let mut ln_text: Vec<String> = Vec::new();
for cap in re.captures_iter(&html) {
ln_text.push(cap.get(1).unwrap().as_str().trim().to_string());
}
// remove last 3 indexes of ln_text
ln_text.truncate(ln_text.len() - 3);
fix_html_encoding(&ln_text)
}

94
src/ln/search.rs Normal file
View File

@@ -0,0 +1,94 @@
use crate::helpers::{fixing_text::remove_after_dash, take_input::string_input};
use colored::Colorize;
use regex::Regex;
pub fn search_ln() -> String {
let mut _is_n = false;
print!("\x1B[2J\x1B[1;1H");
while !_is_n {
let search_path = string_input("What ln do you want to read? ");
let search_path = search_path.replace(' ', "+");
let url = "https://readlightnovels.net/?s=".to_string();
let url = format!("{}{}", url, search_path.trim()).trim().to_string();
let html = crate::ln::scraper::get_html(&url).trim().to_string();
let ln_list = get_ln_list(&html);
//remove first element of ln_list
let ln_list = ln_list
.iter()
.skip(1)
.map(|x| x.to_string())
.collect::<Vec<String>>();
let ln_titles = get_ln_titles(&ln_list);
let ln_urls = get_ln_urls(&ln_list);
let mut count = 0;
ln_titles.into_iter().for_each(|ln| {
if count % 2 == 0 {
println!("({})\t{}", count, format_args!("{}", ln.blue()));
} else {
println!("({})\t{}", count, 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" {
let ln_number = ln_number.trim().to_string();
let ln_number = ln_number.parse::<usize>().unwrap();
let ln_url = &ln_urls[ln_number];
let ln_url = ln_url.trim().to_string();
_is_n = true;
print!("\x1B[2J\x1B[1;1H");
return ln_url;
}
print!("\x1B[2J\x1B[1;1H");
}
"".to_string()
}
//gets the list of ln's from the html and returns it as a vector of the ln's name and href
fn get_ln_list(html: &str) -> Vec<String> {
let re = Regex::new(r#"(?m)^\s*(<a href="[^"]*" title="[^"]*")"#).unwrap();
let mut ln_list: Vec<String> = Vec::new();
for cap in re.captures_iter(html) {
ln_list.push(cap.get(1).unwrap().as_str().trim().to_string());
}
ln_list
}
//gets the titles of the ln's from the html and returns it as a vector of the ln's name
fn get_ln_titles(ln_list: &Vec<String>) -> Vec<String> {
let re = Regex::new(r#"(?m)^\s*<a href="[^"]*" title="([^"]*)""#).unwrap();
let mut ln_title: Vec<String> = Vec::new();
for ln in ln_list {
for cap in re.captures_iter(ln) {
ln_title.push(cap.get(1).unwrap().as_str().to_string());
}
}
ln_title
}
//gets the urls of the ln's from the html and returns it as a vector of the ln's href
fn get_ln_urls(ln_list: &Vec<String>) -> Vec<String> {
let re = Regex::new(r#"(?m)^\s*<a href="([^"]*)""#).unwrap();
let mut ln_url: Vec<String> = Vec::new();
for ln in ln_list {
for cap in re.captures_iter(ln) {
ln_url.push(cap.get(1).unwrap().as_str().to_string());
}
}
ln_url
}
//gets the chapter titles from the html and returns it as a vector of the chapter's name
pub fn get_ln_chapters(html: &str) -> Vec<String> {
let re = Regex::new(r#"title=(.*?)>"#).unwrap();
let mut ln_list: Vec<String> = Vec::new();
for cap in re.captures_iter(html) {
ln_list.push(cap.get(1).unwrap().as_str().trim().to_string());
}
ln_list = remove_after_dash(&ln_list);
ln_list
}