Read config from file

This commit is contained in:
Sayantan Santra 2022-12-08 00:03:44 -06:00
parent dec60f9d6b
commit d0a937bc52

View file

@ -1,5 +1,5 @@
use load_file::{self, load_str}; use load_file::{self, load_str};
use std::{env, fmt}; use std::{env, fmt, fs};
use tmdb::{model::*, themoviedb::*}; use tmdb::{model::*, themoviedb::*};
use torrent_name_parser::Metadata; use torrent_name_parser::Metadata;
use youchoose; use youchoose;
@ -25,8 +25,11 @@ impl MovieEntry {
} }
} }
fn rename_format(&self) -> String { fn rename_format(&self, mut format: String) -> String {
format!("{} ({}) - {}", self.title, self.year, self.director) format = format.replace("{title}", self.title.as_str());
format = format.replace("{year}", self.year.as_str());
format = format.replace("{director}", self.director.as_str());
format
} }
} }
@ -37,17 +40,24 @@ impl fmt::Display for MovieEntry {
} }
fn main() { fn main() {
let args: Vec<String> = env::args().collect(); let mut args = env::args();
let key_file = &args[1]; args.next();
let filename = &args[2]; let filenames: Vec<String> = args.collect();
let api_key = load_str!(key_file); let mut config_file = env::var("XDG_CONFIG_HOME").unwrap_or("$HOME".to_string());
if config_file == String::from("$HOME") {
config_file = env::var("$HOME").unwrap();
}
config_file.push_str("/movie_rename.conf");
let mut config = load_str!(config_file.as_str()).lines();
let api_key = config.next().unwrap();
let pattern = config.next().unwrap();
let tmdb = TMDb { let tmdb = TMDb {
api_key: api_key, api_key: api_key,
language: "en", language: "en",
}; };
for filename in filenames {
let metadata = Metadata::from(filename).unwrap(); let metadata = Metadata::from(filename.as_str()).unwrap();
let results = tmdb let results = tmdb
.search() .search()
.title(metadata.title()) .title(metadata.title())
@ -81,12 +91,19 @@ fn main() {
.preview(display) .preview(display)
.preview_label(filename.to_string()); .preview_label(filename.to_string());
let choice = menu.show()[0]; let choice = menu.show()[0];
let mut new_name = vec![ let mut new_name_vec = vec![
movie_list[choice].rename_format(), movie_list[choice].rename_format(pattern.to_string()),
metadata.extension().unwrap_or("").to_string(), metadata.extension().unwrap_or("").to_string(),
]; ];
new_name.retain(|x| !x.is_empty()); new_name_vec.retain(|x| !x.is_empty());
println!("{}", new_name.join(".")); let new_name = new_name_vec.join(".");
if filename == new_name {
println!("{} already has correct name.", filename);
} else {
println!("{} -> {}", filename, new_name);
fs::rename(filename, new_name).expect("Unable to rename file.");
}
}
} }
fn display(movie: &MovieEntry) -> String { fn display(movie: &MovieEntry) -> String {