From 51aabd87ab79f0fc9c366ef9705a4b128d8a3224 Mon Sep 17 00:00:00 2001 From: Jakub Trzeciak Date: Sat, 10 Feb 2024 21:59:19 +0100 Subject: add autodetecting music folder location --- src/main.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 50 insertions(+), 11 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 2bf1274..78081a4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,21 @@ -use std::{fs::{self, File}, path::Path, io::{Error, BufReader, Read}, fmt::format, os::fd::IntoRawFd}; +use std::{env, fs::{self, File}, path::Path, io::{Error, BufReader, Read}, fmt::format, os::fd::IntoRawFd}; use lofty::{Probe, TaggedFileExt, LoftyError, TagExt, Tag, Picture, Accessor, PictureType}; +use clap::Parser; + +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] +struct Args { + /// Path to your music folder + #[arg(short, long)] + path: Option, + + /// Print more logs while running TODO + #[arg(short, long, default_value_t = false)] + verbose: bool, +} + /* Title Artist - Title @@ -24,13 +38,13 @@ impl InnerTag { pub fn from(name: &str) -> Result{ let mut inner_tag: InnerTag = InnerTag::empty(); - + let mut tags: Vec = Vec::new(); { // TODO make it faster and cleaner let mut text: String = "".into(); let mut connected = false; - + for s in name.split("-") { if text == "" { text = s.into(); @@ -76,7 +90,7 @@ impl InnerTag { inner_tag.track = Some(tags.remove(0).parse().unwrap()); inner_tag.title = tags.remove(1); }, - _ => panic!("FUCK to many tags {}", tags.len()) + _ => panic!("🔥 To many tags in {}", name) } return Ok(inner_tag); @@ -112,7 +126,7 @@ fn tag_ogg_file(path: &Path) -> Result<(), LoftyError> { } else { let tag_type = tagged_file.primary_tag_type(); - eprintln!("WARN: No tags found, creating a new tag of type `{tag_type:?}`"); + eprintln!("👻 No tags found, creating a new tag of type `{tag_type:?}`"); tagged_file.insert_tag(Tag::new(tag_type)); tagged_file.primary_tag_mut().unwrap() @@ -122,7 +136,7 @@ fn tag_ogg_file(path: &Path) -> Result<(), LoftyError> { let path_without_ext = path.with_extension(""); let name = path_without_ext.file_name().unwrap().to_str().unwrap(); - let inner_tag = InnerTag::from(name).unwrap(); + let inner_tag = InnerTag::from(name).unwrap(); tag.clear(); tag.set_title(inner_tag.title); @@ -138,13 +152,13 @@ fn tag_ogg_file(path: &Path) -> Result<(), LoftyError> { if image_path.exists() { let image_file = &mut File::open(image_path.clone()).unwrap(); - + let mut picture = Picture::from_reader(image_file).unwrap(); picture.set_pic_type(PictureType::CoverFront); - + tag.set_picture(0, picture) } else { - println!("⚠️ Can't found image {}", image_path.to_str().unwrap()); + eprintln!("👻 Can't found image {}", image_path.to_str().unwrap()); } } } @@ -158,7 +172,30 @@ fn tag_ogg_file(path: &Path) -> Result<(), LoftyError> { } fn main() { - let music_dir = fs::read_dir("/home/jp3/Music").unwrap(); + let args = Args::parse(); + + let music_path: String; + + if let Some(path) = args.path { + music_path = path; + } else if let Ok(home) = env::var("HOME") { + println!("💡 Trying to find the music folder automatically"); + music_path = home + "/Music" + } else { + eprintln!("🔥 Couldn't find a music folder automatically"); + std::process::exit(1); + } + + let music_dir: fs::ReadDir; + + if let Ok(dir) = fs::read_dir(music_path.as_str()) { + music_dir = dir; + } else { + eprintln!("🔥 Dir \"{}\" doesn't exits", music_path.as_str()); + std::process::exit(1); + } + + println!("💡 Start scanning \"{}\" dir for ogg file", music_path.as_str()); for path in music_dir { // println!("Name: {}", path.unwrap().file_name().to_str().unwrap()) @@ -166,4 +203,6 @@ fn main() { tag_ogg_file(path.unwrap().path().as_path()); } } -} \ No newline at end of file + + println!("💡 Ended successfully"); +} -- cgit v1.2.3