diff --git a/Cargo.lock b/Cargo.lock index da62de6..876ac51 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7,6 +7,7 @@ name = "Hentai" version = "0.1.0" dependencies = [ "postgres", + "urlencoding", ] [[package]] @@ -668,6 +669,12 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e70f2a8b45122e719eb623c01822704c4e0907e7e426a05927e1a1cfff5b75d0" +[[package]] +name = "urlencoding" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da" + [[package]] name = "version_check" version = "0.9.5" diff --git a/Cargo.toml b/Cargo.toml index 2a516be..d891b71 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,3 +5,4 @@ edition = "2021" [dependencies] postgres = "0.19.9" +urlencoding = "2.1.3" diff --git a/src/main.rs b/src/main.rs index 3049a44..0c5ec14 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,12 @@ use postgres::{Client, NoTls}; use std::{ env::args, - fs::File, + fs::{self, File}, io::{BufRead, BufReader, Write}, }; +use urlencoding; + +const IS_DEV: bool = true; fn generate_file(website_name: &str) { let mut client = Client::connect("host=192.168.0.10 port=28945 user=hentai password=h99nqaNPhpfbuuhCDwQXLpZAnoVTjSQP7taoqmQhpzc2rPLVC4JUAKxAHfuuhuU9", NoTls).unwrap(); @@ -25,14 +28,51 @@ fn generate_file(website_name: &str) { } }; - let sqlstmt = format!("SELECT * FROM get_urls_and_paths('{}');", website_name); - let output = client.query(&sqlstmt, &[]).unwrap(); + 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); - write!(file, "{} # {}\n", url, folder_path).unwrap(); + writeln!(file, "{} # {}", url, folder_path).unwrap(); + } +} + +fn convert_folders(root_folder: &str) { + let mut client = Client::connect("host=192.168.0.10 port=28945 user=hentai password=h99nqaNPhpfbuuhCDwQXLpZAnoVTjSQP7taoqmQhpzc2rPLVC4JUAKxAHfuuhuU9", NoTls).unwrap(); + + let filename: &str = "convert_folder.log"; + let mut file = match File::create(filename) { + Ok(file) => file, + Err(error) => { + let message = error.to_string(); + panic!("Problem opening the file: {message}"); + } + }; + + 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!(file, "Converted {} => {}", old_path, new_path).unwrap(); } } @@ -41,30 +81,52 @@ fn import_file(filename: &str) { let reader_iter = BufReader::new(file).lines(); let mut client = Client::connect("host=192.168.0.10 port=28945 user=hentai password=h99nqaNPhpfbuuhCDwQXLpZAnoVTjSQP7taoqmQhpzc2rPLVC4JUAKxAHfuuhuU9", NoTls).unwrap(); + let mut other_file = match File::create("Hallo.txt") { + Ok(file) => file, + Err(error) => { + let message = error.to_string(); + panic!("Problem opening the file: {message}"); + } + }; + writeln!(other_file, "# Them are hella fucked").unwrap(); + + if IS_DEV { + let _ = client.execute("CALL create_tables();", &[]); + } + for line in reader_iter { let line = line.unwrap(); - if !line.contains("#") { - let output = client.query_one("SELECT insert_url($1)", &[&line]).unwrap(); + if line.contains("#") { + continue; + } - let result: i32 = output.get(0); - dbg!(result); + let output = client.query_one("SELECT insert_url($1)", &[&line]).unwrap(); + + let result: i32 = output.get(0); + if result == 1 { + dbg!(&line); + writeln!(other_file, "{}", line).unwrap(); } } } +fn add_artist(url: &str) { + dbg!(url); + todo!() +} + fn main() { let args: Vec = args().collect(); dbg!(&args); - if args.len() != 3 { - panic!("Too many or too few arguments"); - } + let option: &str = args[1].as_str(); + let value: &str = args[2].as_str(); - let operation: &str = args[1].as_str(); - - match operation { - "generate" => generate_file(&args[2]), - "import" => import_file(&args[2]), + match option { + "generate" => generate_file(value), + "convert" => convert_folders(value), + "import" => import_file(value), + "add" => add_artist(value), _ => panic!("Invalid argument"), } }