Add comments

This commit is contained in:
Sayantan Santra 2022-12-08 00:54:55 -06:00
parent 3cfe9de455
commit 7d64b90e6b

View file

@ -4,6 +4,7 @@ use tmdb::{model::*, themoviedb::*};
use torrent_name_parser::Metadata;
use youchoose;
// Struct for movie entries
struct MovieEntry {
title: String,
id: u64,
@ -14,6 +15,7 @@ struct MovieEntry {
}
impl MovieEntry {
// Create movie entry from results
fn from(movie: SearchMovie) -> MovieEntry {
MovieEntry {
title: movie.title,
@ -25,6 +27,7 @@ impl MovieEntry {
}
}
// Generate desired filename from movie entry
fn rename_format(&self, mut format: String) -> String {
format = format.replace("{title}", self.title.as_str());
format = format.replace("{year}", self.year.as_str());
@ -33,6 +36,7 @@ impl MovieEntry {
}
}
// Implement display trait for movie entries
impl fmt::Display for MovieEntry {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} ({})", self.title, self.year)
@ -40,10 +44,12 @@ impl fmt::Display for MovieEntry {
}
fn main() {
// Read arguments from commandline
let mut args = env::args();
args.next();
let filenames: Vec<String> = args.collect();
// If --help is passed, show help and exit
if filenames.contains(&"--help".to_string()) {
println!("The expected syntax is:");
println!("movie_rename <filename(s)> [--dry-run]");
@ -59,6 +65,7 @@ fn main() {
exit(0);
}
// Try to read config file, or display error
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();
@ -73,24 +80,30 @@ fn main() {
exit(1);
}
// Create TMDb object for API calls
let tmdb = TMDb {
api_key: api_key,
language: "en",
};
// Check if --dry-run is passed
let mut dry_run = false;
if filenames.contains(&"--dry-run".to_string()) {
println!("Doing a dry run.");
dry_run = true;
}
// Iterate over filenames
for filename in filenames {
// Skip if it's the --dry-run tag
if filename == "--dry-run".to_string() {
continue;
}
let metadata = Metadata::from(filename.as_str()).unwrap();
// Parse the filename for metadata
let metadata = Metadata::from(filename.as_str()).expect("Could not parse filename");
// Search using the TMDb API
let results = tmdb
.search()
.title(metadata.title())
@ -101,30 +114,36 @@ fn main() {
let mut movie_list: Vec<MovieEntry> = Vec::new();
// Create movie entry from the result
for result in results {
let mut movie_details = MovieEntry::from(result);
let with_credits: Result<Movie, _> =
tmdb.fetch().id(movie_details.id).append_credits().execute();
if let Ok(movie) = with_credits {
match movie.credits {
Some(cre) => {
let mut directors = cre.crew;
directors.retain(|x| x.job == "Director");
for person in directors {
movie_details.director = person.name;
// Get director's name, if needed
if pattern.contains("{director}") {
let with_credits: Result<Movie, _> =
tmdb.fetch().id(movie_details.id).append_credits().execute();
if let Ok(movie) = with_credits {
match movie.credits {
Some(cre) => {
let mut directors = cre.crew;
directors.retain(|x| x.job == "Director");
for person in directors {
movie_details.director = person.name;
}
}
None => {}
}
None => {}
}
}
movie_list.push(movie_details);
}
// Choose from the possible entries
let mut menu = youchoose::Menu::new(movie_list.iter())
.preview(display)
.preview_label(filename.to_string());
let choice = menu.show()[0];
// Handle the case for subtitle files
let mut extension = metadata.extension().unwrap_or("").to_string();
if ["srt", "ssa"].contains(&extension.as_str()) {
let languages = Vec::from(["en", "hi", "bn", "de", "fr", "sp", "ja", "n/a"]);
@ -135,6 +154,7 @@ fn main() {
}
}
// Create the new name
let mut new_name_vec = vec![
movie_list[choice].rename_format(pattern.to_string()),
extension,
@ -145,6 +165,7 @@ fn main() {
println!("{} already has correct name.", filename);
} else {
println!("{} -> {}", filename, new_name);
// Only do the rename of --dry-run isn't passed
if dry_run == false {
println!("Doing the actual rename.");
fs::rename(filename, new_name).expect("Unable to rename file.");
@ -153,6 +174,7 @@ fn main() {
}
}
// Display function for preview in menu
fn display(movie: &MovieEntry) -> String {
let mut buffer = String::new();
buffer.push_str(&format!("Title: {}\n", movie.title));