mirror of
https://github.com/SinTan1729/movie-rename.git
synced 2024-12-26 12:18:37 -06:00
Refactor movie processing
This commit is contained in:
parent
8e07db9156
commit
17754456ce
1 changed files with 42 additions and 37 deletions
79
src/main.rs
79
src/main.rs
|
@ -109,42 +109,8 @@ fn main() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse the filename for metadata
|
// Process the filename for movie entries
|
||||||
let metadata = Metadata::from(filename.as_str()).expect("Could not parse filename");
|
let (mut extension, movie_list) = process_movie(&filename, &tmdb, pattern);
|
||||||
|
|
||||||
// Search using the TMDb API
|
|
||||||
let results = tmdb
|
|
||||||
.search()
|
|
||||||
.title(metadata.title())
|
|
||||||
.year(metadata.year().unwrap() as u64)
|
|
||||||
.execute()
|
|
||||||
.unwrap()
|
|
||||||
.results;
|
|
||||||
|
|
||||||
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);
|
|
||||||
// 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 => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
movie_list.push(movie_details);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Choose from the possible entries
|
// Choose from the possible entries
|
||||||
let mut menu = youchoose::Menu::new(movie_list.iter())
|
let mut menu = youchoose::Menu::new(movie_list.iter())
|
||||||
|
@ -153,7 +119,6 @@ fn main() {
|
||||||
let choice = menu.show()[0];
|
let choice = menu.show()[0];
|
||||||
|
|
||||||
// Handle the case for subtitle files
|
// Handle the case for subtitle files
|
||||||
let mut extension = metadata.extension().unwrap_or("").to_string();
|
|
||||||
if ["srt", "ssa"].contains(&extension.as_str()) {
|
if ["srt", "ssa"].contains(&extension.as_str()) {
|
||||||
let languages = Vec::from(["en", "hi", "bn", "de", "fr", "sp", "ja", "n/a"]);
|
let languages = Vec::from(["en", "hi", "bn", "de", "fr", "sp", "ja", "n/a"]);
|
||||||
let mut lang_menu = youchoose::Menu::new(languages.iter());
|
let mut lang_menu = youchoose::Menu::new(languages.iter());
|
||||||
|
@ -170,6 +135,8 @@ fn main() {
|
||||||
];
|
];
|
||||||
new_name_vec.retain(|x| !x.is_empty());
|
new_name_vec.retain(|x| !x.is_empty());
|
||||||
let new_name = new_name_vec.join(".");
|
let new_name = new_name_vec.join(".");
|
||||||
|
|
||||||
|
// Process the renaming
|
||||||
if filename == new_name {
|
if filename == new_name {
|
||||||
println!("{} already has correct name.", filename);
|
println!("{} already has correct name.", filename);
|
||||||
} else {
|
} else {
|
||||||
|
@ -183,6 +150,44 @@ fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Function to process movie entries
|
||||||
|
fn process_movie(filename: &String, tmdb: &TMDb, pattern: &str) -> (String, Vec<MovieEntry>) {
|
||||||
|
// 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())
|
||||||
|
.year(metadata.year().unwrap() as u64)
|
||||||
|
.execute()
|
||||||
|
.unwrap()
|
||||||
|
.results;
|
||||||
|
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);
|
||||||
|
// 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 => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
movie_list.push(movie_details);
|
||||||
|
}
|
||||||
|
(metadata.extension().unwrap_or("").to_string(), movie_list)
|
||||||
|
}
|
||||||
|
|
||||||
// Display function for preview in menu
|
// Display function for preview in menu
|
||||||
fn display(movie: &MovieEntry) -> String {
|
fn display(movie: &MovieEntry) -> String {
|
||||||
let mut buffer = String::new();
|
let mut buffer = String::new();
|
||||||
|
|
Loading…
Reference in a new issue