Detect subtitle language from the old filename

This commit is contained in:
Sayantan Santra 2022-12-10 21:00:07 -06:00
parent 17300e0362
commit c52643033f
3 changed files with 20 additions and 9 deletions

View file

@ -6,7 +6,7 @@ use std::{collections::HashMap, fs, path::Path, process::exit};
use tmdb::{model::*, themoviedb::*}; use tmdb::{model::*, themoviedb::*};
use torrent_name_parser::Metadata; use torrent_name_parser::Metadata;
use crate::structs::{Language, MovieEntry}; use crate::structs::{get_long_lang, Language, MovieEntry};
// Get the version from Cargo.toml // Get the version from Cargo.toml
const VERSION: &str = env!("CARGO_PKG_VERSION"); const VERSION: &str = env!("CARGO_PKG_VERSION");
@ -89,6 +89,15 @@ pub fn process_file(
// Handle the case for subtitle files // Handle the case for subtitle files
let mut is_subtitle = false; let mut is_subtitle = false;
if ["srt", "ssa"].contains(&extension.as_str()) { if ["srt", "ssa"].contains(&extension.as_str()) {
// Try to detect if there's already language info in the filename, else ask user to choose
let filename_parts: Vec<&str> = filename.rsplit(".").collect();
if filename_parts.len() >= 3 && filename_parts[1].len() == 2 {
println!(
"Keeping language {} as detected in the subtitle file's extension...",
get_long_lang(filename_parts[1])
);
extension = format!("{}.{}", filename_parts[1], extension);
} else {
let lang_list = Language::generate_list(); let lang_list = Language::generate_list();
let lang_choice = Select::new("Choose the language for the subtitle file:", lang_list) let lang_choice = Select::new("Choose the language for the subtitle file:", lang_list)
.prompt() .prompt()
@ -96,6 +105,7 @@ pub fn process_file(
if lang_choice.short != "none".to_string() { if lang_choice.short != "none".to_string() {
extension = format!("{}.{}", lang_choice.short, extension); extension = format!("{}.{}", lang_choice.short, extension);
} }
}
is_subtitle = true; is_subtitle = true;
} }

View file

@ -38,7 +38,8 @@ fn main() {
// Iterate over entries // Iterate over entries
for entry in entries { for entry in entries {
// Check if the file/directory exists on disk // Check if the file/directory exists on disk and run necessary commands
// TODO: Detect subtitle files with same name/metadata and process them automatically without repeated input
match settings["directory"] { match settings["directory"] {
// Normal file // Normal file
false => { false => {

View file

@ -80,7 +80,7 @@ impl fmt::Display for Language {
} }
// Get long name of a language // Get long name of a language
fn get_long_lang(short: &str) -> String { pub fn get_long_lang(short: &str) -> String {
let long = match short { let long = match short {
"en" => "English", "en" => "English",
"hi" => "Hindi", "hi" => "Hindi",