refactor: minor refactoring
This commit is contained in:
parent
a4693e3da9
commit
f9ea3ae1c8
1 changed files with 49 additions and 37 deletions
86
src/main.rs
86
src/main.rs
|
@ -65,49 +65,61 @@ fn rename_files(directory: &str, base: &str, args: &Arguments) -> Result<i32> {
|
|||
if let Some(sub_dir) = path.to_str() {
|
||||
count += rename_files(sub_dir, base, args).context("Failed to rename files")?;
|
||||
}
|
||||
}
|
||||
if !path.is_file() {
|
||||
continue;
|
||||
}
|
||||
let Ok(file) = taglib::File::new(&path) else {
|
||||
continue;
|
||||
};
|
||||
let Ok(tag) = file.tag() else {
|
||||
continue;
|
||||
};
|
||||
let (Some(title), Some(artist)) = (tag.title(), tag.artist()) else {
|
||||
continue;
|
||||
};
|
||||
let (bucket, _) = artist.split_at(1);
|
||||
let album = parse_album(&title);
|
||||
count += 1;
|
||||
let new_name = if album.is_empty() {
|
||||
format!("{bucket}/{artist}/{title}/{title}.m4b")
|
||||
} else {
|
||||
build_series_name(bucket, &artist, &album, &title)
|
||||
};
|
||||
println!("==============================");
|
||||
println!("- artist: {artist}");
|
||||
println!("- album : {album}");
|
||||
println!("- title : {title}");
|
||||
println!("> {new_name}");
|
||||
if args.not_dry_run() {
|
||||
let target_path = Path::new(&base).join(new_name);
|
||||
if !target_path.exists() {
|
||||
let dir = target_path
|
||||
.parent()
|
||||
.with_context(|| format!("Failed to get parent: {:#?}", target_path))?;
|
||||
create_dir_all(dir)
|
||||
.with_context(|| format!("Failed to create directory: {:#?}", dir))?;
|
||||
} else if path.is_file() {
|
||||
let Some((title, artist)) = find_tag_info(&path) else {
|
||||
continue;
|
||||
};
|
||||
let (bucket, _) = artist.split_at(1);
|
||||
let album = parse_album(&title);
|
||||
count += 1;
|
||||
let new_name = if album.is_empty() {
|
||||
format!("{bucket}/{artist}/{title}/{title}.m4b")
|
||||
} else {
|
||||
build_series_name(bucket, &artist, &album, &title)
|
||||
};
|
||||
println!("==============================");
|
||||
println!("- artist: {artist}");
|
||||
println!("- album : {album}");
|
||||
println!("- title : {title}");
|
||||
println!("> {new_name}");
|
||||
if args.not_dry_run() {
|
||||
rename_file(base, new_name, path)?;
|
||||
}
|
||||
|
||||
rename(&path, &target_path)
|
||||
.with_context(|| format!("Failed to rename file: {:#?}", target_path))?;
|
||||
}
|
||||
}
|
||||
Ok(count)
|
||||
}
|
||||
|
||||
fn find_tag_info(path: &std::path::PathBuf) -> Option<(String, String)> {
|
||||
let Ok(file) = taglib::File::new(path) else {
|
||||
return None;
|
||||
};
|
||||
let Ok(tag) = file.tag() else {
|
||||
return None;
|
||||
};
|
||||
let (Some(title), Some(artist)) = (tag.title(), tag.artist()) else {
|
||||
return None;
|
||||
};
|
||||
Some((title, artist))
|
||||
}
|
||||
|
||||
fn rename_file(
|
||||
base: &str,
|
||||
new_name: String,
|
||||
path: std::path::PathBuf,
|
||||
) -> Result<(), anyhow::Error> {
|
||||
let target_path = Path::new(&base).join(new_name);
|
||||
if !target_path.exists() {
|
||||
let dir = target_path
|
||||
.parent()
|
||||
.with_context(|| format!("Failed to get parent: {:#?}", target_path))?;
|
||||
create_dir_all(dir).with_context(|| format!("Failed to create directory: {:#?}", dir))?;
|
||||
}
|
||||
rename(&path, &target_path)
|
||||
.with_context(|| format!("Failed to rename file: {:#?}", target_path))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn build_series_name(bucket: &str, artist: &str, album: &str, title: &str) -> String {
|
||||
let index = parse_index(title);
|
||||
let title = parse_title(title);
|
||||
|
|
Loading…
Reference in a new issue