feat: move files by artist tag
This commit is contained in:
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
+35
-6
@@ -1,20 +1,26 @@
|
|||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use lofty::file::TaggedFileExt;
|
use lofty::file::TaggedFileExt;
|
||||||
use lofty::probe::Probe;
|
use lofty::probe::Probe;
|
||||||
use std::fs::rename;
|
use lofty::tag::Accessor;
|
||||||
|
use lofty::tag::Tag;
|
||||||
|
use std::fs::{create_dir_all, rename};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[command(version, about, long_about = None)]
|
#[command(version, about, long_about = None)]
|
||||||
struct Args {
|
struct Args {
|
||||||
file: PathBuf,
|
file: PathBuf,
|
||||||
|
|
||||||
|
#[arg(short)]
|
||||||
|
import: bool,
|
||||||
|
|
||||||
|
#[arg(short)]
|
||||||
|
show: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
let file_path = args.file;
|
let file_path = args.file;
|
||||||
let target_dir = PathBuf::from("./test");
|
|
||||||
let target = target_dir.join(&file_path);
|
|
||||||
|
|
||||||
let tagged_file = Probe::open(&file_path)
|
let tagged_file = Probe::open(&file_path)
|
||||||
.expect("ERROR: Bad path provided!")
|
.expect("ERROR: Bad path provided!")
|
||||||
@@ -22,6 +28,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
.expect("ERROR: Failed to read file!");
|
.expect("ERROR: Failed to read file!");
|
||||||
|
|
||||||
let id3v2 = tagged_file.primary_tag();
|
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 {
|
if let Some(tag) = id3v2 {
|
||||||
for item in tag.items() {
|
for item in tag.items() {
|
||||||
@@ -29,12 +44,26 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let _ = move_files(target, file_path);
|
if args.import {
|
||||||
|
let _ = move_files(file_path, tag.clone());
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn move_files(target: PathBuf, file_path: PathBuf) -> Result<(), Box<dyn std::error::Error>> {
|
fn move_files(file_path: PathBuf, tag: Tag) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
rename(file_path, target)?;
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user