Refactor movie processing

This commit is contained in:
Sayantan Santra 2022-12-08 16:49:33 -06:00
parent 8e07db9156
commit 17754456ce

View file

@ -109,9 +109,51 @@ fn main() {
continue; continue;
} }
// Process the filename for movie entries
let (mut extension, movie_list) = process_movie(&filename, &tmdb, pattern);
// 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
if ["srt", "ssa"].contains(&extension.as_str()) {
let languages = Vec::from(["en", "hi", "bn", "de", "fr", "sp", "ja", "n/a"]);
let mut lang_menu = youchoose::Menu::new(languages.iter());
let lang_choice = lang_menu.show()[0];
if languages[lang_choice] != "none" {
extension = format!("{}.{}", languages[lang_choice], extension);
}
}
// Create the new name
let mut new_name_vec = vec![
movie_list[choice].rename_format(pattern.to_string()),
extension,
];
new_name_vec.retain(|x| !x.is_empty());
let new_name = new_name_vec.join(".");
// Process the renaming
if filename == new_name {
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!("Renaming...");
fs::rename(filename, new_name).expect("Unable to rename file.");
}
}
}
}
// Function to process movie entries
fn process_movie(filename: &String, tmdb: &TMDb, pattern: &str) -> (String, Vec<MovieEntry>) {
// Parse the filename for metadata // Parse the filename for metadata
let metadata = Metadata::from(filename.as_str()).expect("Could not parse filename"); let metadata = Metadata::from(filename.as_str()).expect("Could not parse filename");
// Search using the TMDb API // Search using the TMDb API
let results = tmdb let results = tmdb
.search() .search()
@ -120,9 +162,7 @@ fn main() {
.execute() .execute()
.unwrap() .unwrap()
.results; .results;
let mut movie_list: Vec<MovieEntry> = Vec::new(); let mut movie_list: Vec<MovieEntry> = Vec::new();
// Create movie entry from the result // Create movie entry from the result
for result in results { for result in results {
let mut movie_details = MovieEntry::from(result); let mut movie_details = MovieEntry::from(result);
@ -145,42 +185,7 @@ fn main() {
} }
movie_list.push(movie_details); movie_list.push(movie_details);
} }
(metadata.extension().unwrap_or("").to_string(), movie_list)
// 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"]);
let mut lang_menu = youchoose::Menu::new(languages.iter());
let lang_choice = lang_menu.show()[0];
if languages[lang_choice] != "none" {
extension = format!("{}.{}", languages[lang_choice], extension);
}
}
// Create the new name
let mut new_name_vec = vec![
movie_list[choice].rename_format(pattern.to_string()),
extension,
];
new_name_vec.retain(|x| !x.is_empty());
let new_name = new_name_vec.join(".");
if filename == new_name {
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!("Renaming...");
fs::rename(filename, new_name).expect("Unable to rename file.");
}
}
}
} }
// Display function for preview in menu // Display function for preview in menu