mirror of
https://github.com/SinTan1729/movie-rename.git
synced 2024-12-27 04:38:35 -06:00
Add comments
This commit is contained in:
parent
3cfe9de455
commit
7d64b90e6b
1 changed files with 34 additions and 12 deletions
26
src/main.rs
26
src/main.rs
|
@ -4,6 +4,7 @@ use tmdb::{model::*, themoviedb::*};
|
||||||
use torrent_name_parser::Metadata;
|
use torrent_name_parser::Metadata;
|
||||||
use youchoose;
|
use youchoose;
|
||||||
|
|
||||||
|
// Struct for movie entries
|
||||||
struct MovieEntry {
|
struct MovieEntry {
|
||||||
title: String,
|
title: String,
|
||||||
id: u64,
|
id: u64,
|
||||||
|
@ -14,6 +15,7 @@ struct MovieEntry {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MovieEntry {
|
impl MovieEntry {
|
||||||
|
// Create movie entry from results
|
||||||
fn from(movie: SearchMovie) -> MovieEntry {
|
fn from(movie: SearchMovie) -> MovieEntry {
|
||||||
MovieEntry {
|
MovieEntry {
|
||||||
title: movie.title,
|
title: movie.title,
|
||||||
|
@ -25,6 +27,7 @@ impl MovieEntry {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Generate desired filename from movie entry
|
||||||
fn rename_format(&self, mut format: String) -> String {
|
fn rename_format(&self, mut format: String) -> String {
|
||||||
format = format.replace("{title}", self.title.as_str());
|
format = format.replace("{title}", self.title.as_str());
|
||||||
format = format.replace("{year}", self.year.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 {
|
impl fmt::Display for MovieEntry {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(f, "{} ({})", self.title, self.year)
|
write!(f, "{} ({})", self.title, self.year)
|
||||||
|
@ -40,10 +44,12 @@ impl fmt::Display for MovieEntry {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
// Read arguments from commandline
|
||||||
let mut args = env::args();
|
let mut args = env::args();
|
||||||
args.next();
|
args.next();
|
||||||
let filenames: Vec<String> = args.collect();
|
let filenames: Vec<String> = args.collect();
|
||||||
|
|
||||||
|
// If --help is passed, show help and exit
|
||||||
if filenames.contains(&"--help".to_string()) {
|
if filenames.contains(&"--help".to_string()) {
|
||||||
println!("The expected syntax is:");
|
println!("The expected syntax is:");
|
||||||
println!("movie_rename <filename(s)> [--dry-run]");
|
println!("movie_rename <filename(s)> [--dry-run]");
|
||||||
|
@ -59,6 +65,7 @@ fn main() {
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Try to read config file, or display error
|
||||||
let mut config_file = env::var("XDG_CONFIG_HOME").unwrap_or("$HOME".to_string());
|
let mut config_file = env::var("XDG_CONFIG_HOME").unwrap_or("$HOME".to_string());
|
||||||
if config_file == String::from("$HOME") {
|
if config_file == String::from("$HOME") {
|
||||||
config_file = env::var("$HOME").unwrap();
|
config_file = env::var("$HOME").unwrap();
|
||||||
|
@ -73,24 +80,30 @@ fn main() {
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create TMDb object for API calls
|
||||||
let tmdb = TMDb {
|
let tmdb = TMDb {
|
||||||
api_key: api_key,
|
api_key: api_key,
|
||||||
language: "en",
|
language: "en",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Check if --dry-run is passed
|
||||||
let mut dry_run = false;
|
let mut dry_run = false;
|
||||||
|
|
||||||
if filenames.contains(&"--dry-run".to_string()) {
|
if filenames.contains(&"--dry-run".to_string()) {
|
||||||
println!("Doing a dry run.");
|
println!("Doing a dry run.");
|
||||||
dry_run = true;
|
dry_run = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Iterate over filenames
|
||||||
for filename in filenames {
|
for filename in filenames {
|
||||||
|
// Skip if it's the --dry-run tag
|
||||||
if filename == "--dry-run".to_string() {
|
if filename == "--dry-run".to_string() {
|
||||||
continue;
|
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
|
let results = tmdb
|
||||||
.search()
|
.search()
|
||||||
.title(metadata.title())
|
.title(metadata.title())
|
||||||
|
@ -101,8 +114,11 @@ fn main() {
|
||||||
|
|
||||||
let mut movie_list: Vec<MovieEntry> = Vec::new();
|
let mut movie_list: Vec<MovieEntry> = Vec::new();
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
// Get director's name, if needed
|
||||||
|
if pattern.contains("{director}") {
|
||||||
let with_credits: Result<Movie, _> =
|
let with_credits: Result<Movie, _> =
|
||||||
tmdb.fetch().id(movie_details.id).append_credits().execute();
|
tmdb.fetch().id(movie_details.id).append_credits().execute();
|
||||||
if let Ok(movie) = with_credits {
|
if let Ok(movie) = with_credits {
|
||||||
|
@ -117,14 +133,17 @@ fn main() {
|
||||||
None => {}
|
None => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
movie_list.push(movie_details);
|
movie_list.push(movie_details);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Choose from the possible entries
|
||||||
let mut menu = youchoose::Menu::new(movie_list.iter())
|
let mut menu = youchoose::Menu::new(movie_list.iter())
|
||||||
.preview(display)
|
.preview(display)
|
||||||
.preview_label(filename.to_string());
|
.preview_label(filename.to_string());
|
||||||
let choice = menu.show()[0];
|
let choice = menu.show()[0];
|
||||||
|
|
||||||
|
// Handle the case for subtitle files
|
||||||
let mut extension = metadata.extension().unwrap_or("").to_string();
|
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"]);
|
||||||
|
@ -135,6 +154,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create the new name
|
||||||
let mut new_name_vec = vec![
|
let mut new_name_vec = vec![
|
||||||
movie_list[choice].rename_format(pattern.to_string()),
|
movie_list[choice].rename_format(pattern.to_string()),
|
||||||
extension,
|
extension,
|
||||||
|
@ -145,6 +165,7 @@ fn main() {
|
||||||
println!("{} already has correct name.", filename);
|
println!("{} already has correct name.", filename);
|
||||||
} else {
|
} else {
|
||||||
println!("{} -> {}", filename, new_name);
|
println!("{} -> {}", filename, new_name);
|
||||||
|
// Only do the rename of --dry-run isn't passed
|
||||||
if dry_run == false {
|
if dry_run == false {
|
||||||
println!("Doing the actual rename.");
|
println!("Doing the actual rename.");
|
||||||
fs::rename(filename, new_name).expect("Unable to rename file.");
|
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 {
|
fn display(movie: &MovieEntry) -> String {
|
||||||
let mut buffer = String::new();
|
let mut buffer = String::new();
|
||||||
buffer.push_str(&format!("Title: {}\n", movie.title));
|
buffer.push_str(&format!("Title: {}\n", movie.title));
|
||||||
|
|
Loading…
Reference in a new issue