Very good progress

This commit is contained in:
2024-11-05 23:40:33 +01:00
parent 7d8c27047a
commit 987d25cb42
3 changed files with 86 additions and 16 deletions

View File

@ -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<String> = 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"),
}
}