From eec7a24f256b8a80642dd533efc9d0d512d4b29b Mon Sep 17 00:00:00 2001 From: Craig Drummond Date: Sat, 19 Feb 2022 22:41:07 +0000 Subject: [PATCH] Handle numeric genres in MP3 files --- Cargo.lock | 11 +++++++++++ Cargo.toml | 2 ++ src/tags.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 133c381..278510a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -125,7 +125,9 @@ dependencies = [ "indicatif", "lofty", "log", + "regex", "rusqlite", + "substring", ] [[package]] @@ -1169,6 +1171,15 @@ dependencies = [ "syn", ] +[[package]] +name = "substring" +version = "1.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ee6433ecef213b2e72f587ef64a2f5943e7cd16fbd82dbe8bc07486c534c86" +dependencies = [ + "autocfg", +] + [[package]] name = "syn" version = "1.0.86" diff --git a/Cargo.toml b/Cargo.toml index c1922e8..ade3e5d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,3 +20,5 @@ indicatif = "0.16.2" lofty = { git = "https://github.com/Serial-ATA/lofty-rs", rev = "4c0b7c2" } dirs = "1" chrono = "0.4.19" +regex = "1" +substring = "1.4.5" \ No newline at end of file diff --git a/src/tags.rs b/src/tags.rs index fad9335..102ae1d 100644 --- a/src/tags.rs +++ b/src/tags.rs @@ -7,9 +7,13 @@ **/ use lofty::{Accessor, ItemKey, Probe}; +use regex::Regex; use std::path::Path; +use substring::Substring; use crate::db; +const MAX_GENRE_VAL:usize = 192; + pub fn read(track:&String) -> db::Metadata { let mut meta = db::Metadata{ title:String::new(), @@ -34,6 +38,44 @@ pub fn read(track:&String) -> db::Metadata { meta.album=tag.album().unwrap_or("").to_string(); meta.album_artist=tag.get_string(&ItemKey::AlbumArtist).unwrap_or("").to_string(); meta.genre=tag.genre().unwrap_or("").to_string(); + // Check whether MP3 as numeric genre, and if so covert to text + if file.file_type().eq(&lofty::FileType::MP3) { + match tag.genre() { + Some(genre) => { + let test = &genre.parse::(); + match test { + Ok(val) => { + let idx:usize = *val as usize; + if idx { + // Check for "(number)text" + let re = Regex::new(r"^\([0-9]+\)").unwrap(); + if re.is_match(&genre) { + match genre.find(")") { + Some(end) => { + let test = &genre.to_string().substring(1, end).parse::(); + match test { + Ok(val) => { + let idx:usize = *val as usize; + if idx { } + } + }, + None => { } + } + } + } + } + }, + None => {} + } + } meta.duration=file.properties().duration().as_secs() as u32; }, Err(_) => { }