use postgres::{Client, NoTls}; use std::{ env::args, fs::{self, File}, io::{self, BufRead, BufReader, Write}, }; fn show_usage() { println!("Available options:"); println!("usage"); println!("generate"); println!("convert"); println!("import"); println!("add_url"); println!("add_custom_name"); } fn run_as_cli() -> i32 { let mut result: i32; let mut client = Client::connect("host=192.168.0.10 port=28945 user=hentai password=h99nqaNPhpfbuuhCDwQXLpZAnoVTjSQP7taoqmQhpzc2rPLVC4JUAKxAHfuuhuU9", NoTls).unwrap(); let mut option: String = String::new(); show_usage(); println!("Please input any of these options or type \'exit\' to leave"); loop { result = 0; io::stdin() .read_line(&mut option) .expect("Failed to read input"); let option: &str = option.trim_end(); // to get rid of '\n' match option { "usage" => show_usage(), "generate" => result = generate_file(&mut client, None), "convert" => result = convert_folders(&mut client, None), "import" => result = import_file(&mut client, None), "add_url" => result = add_url(&mut client, None), "add_custom_name" => result = add_artist_with_custom_name(&mut client, None), "exit" => break, _ => { eprintln!("Invalid option!"); } } if result != 0 { return result; } } result } fn run_with_input(arguments: Vec) -> i32 { let mut result: i32 = 0; if arguments[1] == "usage" { show_usage(); return result; } let mut client = Client::connect("host=192.168.0.10 port=28945 user=hentai password=h99nqaNPhpfbuuhCDwQXLpZAnoVTjSQP7taoqmQhpzc2rPLVC4JUAKxAHfuuhuU9", NoTls).unwrap(); let value: String = arguments[2].clone(); match arguments[1].as_str() { "generate" => result = generate_file(&mut client, Option::Some(value)), "convert" => result = convert_folders(&mut client, Option::Some(value)), "import" => result = import_file(&mut client, Option::Some(value)), "add_url" => result = add_url(&mut client, Option::Some(value)), "add_custom_name" => { let values: Vec = vec![arguments[2].clone(), arguments[3].clone()]; result = add_artist_with_custom_name(&mut client, Option::Some(values)); } _ => { eprintln!("Invalid argument"); result = 1; } } result } fn generate_file(client: &mut postgres::Client, website: Option) -> i32 { let mut website_name: String = String::new(); match website { Some(val) => website_name = val, None => { io::stdin() .read_line(&mut website_name) .expect("Failed to read input"); } }; let output = client .query_one("SELECT website_exists($1);", &[&website_name]) .unwrap(); let found: bool = output.get(0); if !found { eprintln!("Invalid website_name"); return 1; } let filename = format!("{website_name}_generated.txt"); let mut file = match File::create_new(filename) { Ok(file) => file, Err(error) => { let message = error.to_string(); eprintln!("Problem creating the file: {message}"); return 1; } }; let output = client .query("SELECT * FROM get_urls_and_paths($1);", &[&website_name]) .unwrap(); for row in output { let url: String = row.get(0); let folder_path: String = row.get(1); writeln!(file, "{} # {}", url, folder_path).unwrap(); } 0 } fn convert_folders(client: &mut postgres::Client, path: Option) -> i32 { let mut root_folder: String = String::new(); match path { Some(val) => root_folder = val, None => { io::stdin() .read_line(&mut root_folder) .expect("Failed to read input"); } }; let filename: &str = "convert_folders.log"; let mut log_file = match File::create_new(filename) { Ok(file) => file, Err(error) => { let message = error.to_string(); eprintln!("Problem creating the file: {message}"); return 1; } }; let output = client .query("SELECT * FROM get_all_convertable_paths();", &[]) .unwrap(); for row in output { let old_path: String = row.get(0); let new_path: String = row.get(1); let old_path = format!("{}/{}", root_folder, old_path); let new_path = format!("{}/{}", root_folder, new_path); let old_path = urlencoding::decode(&old_path).unwrap().to_string(); dbg!(&old_path); dbg!(&new_path); if !fs::exists(&old_path).unwrap() { continue; } fs::rename(&old_path, &new_path).unwrap(); writeln!(log_file, "Converted {} => {}", old_path, new_path).unwrap(); } 0 } fn import_file(client: &mut postgres::Client, file: Option) -> i32 { let mut filename: String = String::new(); match file { Some(val) => filename = val, None => { io::stdin() .read_line(&mut filename) .expect("Failed to read input"); } }; let input_file = File::open(filename).unwrap(); let reader_iter = BufReader::new(input_file).lines(); if File::open("Fucked.txt").is_ok() { if File::open("Fucked.txt.bak").is_ok() { fs::remove_file("Fucked.txt.bak").unwrap(); } fs::rename("Fucked.txt", "Fucked.txt.bak").unwrap(); } let mut fucked_file = match File::create_new("Fucked.txt") { Ok(file) => file, Err(err) => { eprintln!("Problem creating the file: {}", err); return 1; } }; writeln!(fucked_file, "Them are hella fucked").unwrap(); for line in reader_iter { let line = line.unwrap(); if line.contains("#") { writeln!(fucked_file, "{}", line).unwrap(); continue; } match client.query_one("SELECT insert_url($1);", &[&line]) { Ok(_) => (), Err(_) => { writeln!(fucked_file, "{}", line).unwrap(); } }; } 0 } fn add_url(client: &mut postgres::Client, uri: Option) -> i32 { let mut url: String = String::new(); match uri { Some(val) => url = val, None => { io::stdin() .read_line(&mut url) .expect("Failed to read input"); } }; let url: &str = url.trim(); let output = match client.query_one("SELECT insert_url($1);", &[&url]) { Ok(val) => val, Err(val) => { eprintln!("{}", val); return 1; } }; let something: String = output.get(0); println!("{}", something); 0 } fn add_artist_with_custom_name( client: &mut postgres::Client, uri_and_artist: Option>, ) -> i32 { let mut url: String = String::new(); let mut artist_name: String = String::new(); match uri_and_artist { Some(vec) => { url = vec[0].clone(); artist_name = vec[1].clone(); } None => { io::stdin() .read_line(&mut url) .expect("Failed to read input"); io::stdin() .read_line(&mut artist_name) .expect("Failed to read input"); } }; let url: &str = url.trim(); let artist_name: &str = artist_name.trim(); let output = match client.query_one( "SELECT insert_url_with_custom_artist($1, $2);", &[&url, &artist_name], ) { Ok(val) => val, Err(val) => { eprintln!("{}", val); return 1; } }; let something: String = output.get(0); println!("{}", something); 0 } fn main() -> Result<(), i32> { let args: Vec = args().collect(); let result = if args.len() == 1 { run_as_cli() } else { run_with_input(args) }; if result != 0 { return Err(result); } Ok(()) }