feat: add subcommands for importing and showing tags
This commit is contained in:
+33
-29
@@ -1,4 +1,4 @@
|
||||
use clap::Parser;
|
||||
use clap::{Parser, Subcommand};
|
||||
use lofty::file::TaggedFileExt;
|
||||
use lofty::probe::Probe;
|
||||
use lofty::tag::Accessor;
|
||||
@@ -9,60 +9,64 @@ use std::path::PathBuf;
|
||||
#[derive(Parser)]
|
||||
#[command(version, about, long_about = None)]
|
||||
struct Args {
|
||||
file: PathBuf,
|
||||
#[command(subcommand)]
|
||||
command: Commands,
|
||||
}
|
||||
|
||||
#[arg(short)]
|
||||
import: bool,
|
||||
|
||||
#[arg(short)]
|
||||
show: bool,
|
||||
#[derive(Subcommand)]
|
||||
enum Commands {
|
||||
Import { file: PathBuf },
|
||||
Show { file: PathBuf },
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let args = Args::parse();
|
||||
let file_path = args.file;
|
||||
|
||||
match &args.command {
|
||||
Commands::Import { file } => {
|
||||
println!("Importing file: {:?}", file);
|
||||
let tag = get_metadata(file.to_path_buf());
|
||||
let _ = move_files(file.clone(), tag.clone());
|
||||
}
|
||||
Commands::Show { file } => {
|
||||
println!("Show file: {:?}", file);
|
||||
let tag = get_metadata(file.to_path_buf());
|
||||
println!("{}", tag.title().as_deref().unwrap_or("None"));
|
||||
|
||||
for item in tag.items() {
|
||||
println!("{:?}", item)
|
||||
}
|
||||
|
||||
let album_artist = tag.get_string(lofty::tag::ItemKey::AlbumArtist);
|
||||
println!("AlbumArtist: {:?}", album_artist);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_metadata(file_path: PathBuf) -> lofty::tag::Tag {
|
||||
let tagged_file = Probe::open(&file_path)
|
||||
.expect("ERROR: Bad path provided!")
|
||||
.read()
|
||||
.expect("ERROR: Failed to read file!");
|
||||
|
||||
let id3v2 = tagged_file.primary_tag();
|
||||
let tag = match tagged_file.primary_tag() {
|
||||
Some(primary_tag) => primary_tag,
|
||||
None => tagged_file.first_tag().expect("ERROR: No tags found!"),
|
||||
};
|
||||
|
||||
println!("{}", tag.title().as_deref().unwrap_or("None"));
|
||||
|
||||
let album_artist = tag.get_string(lofty::tag::ItemKey::AlbumArtist);
|
||||
println!("AlbumArtist: {:?}", album_artist);
|
||||
|
||||
if let Some(tag) = id3v2 {
|
||||
for item in tag.items() {
|
||||
println!("{:?}", item);
|
||||
}
|
||||
}
|
||||
|
||||
if args.import {
|
||||
let _ = move_files(file_path, tag.clone());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
tag.clone()
|
||||
}
|
||||
|
||||
fn move_files(file_path: PathBuf, tag: Tag) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let target_dir = PathBuf::from("./test");
|
||||
let artist = tag.artist();
|
||||
let mut final_target = PathBuf::new();
|
||||
|
||||
if let Some(artist_path) = artist {
|
||||
final_target = target_dir.join(artist_path.as_ref()).join(&file_path);
|
||||
let dir_to_create = target_dir.join(artist_path.as_ref());
|
||||
|
||||
let _ = create_dir_all(dir_to_create);
|
||||
}
|
||||
|
||||
println!("{:?}", final_target);
|
||||
rename(file_path, final_target)?;
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user