Handle numeric genres in MP3 files

This commit is contained in:
Craig Drummond 2022-02-19 22:41:07 +00:00
parent 5ba4a7b62d
commit eec7a24f25
3 changed files with 55 additions and 0 deletions

11
Cargo.lock generated
View File

@ -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"

View File

@ -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"

View File

@ -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::<u8>();
match test {
Ok(val) => {
let idx:usize = *val as usize;
if idx<MAX_GENRE_VAL {
meta.genre=lofty::id3::v1::GENRES[idx].to_string();
}
},
Err(_) => {
// 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::<u8>();
match test {
Ok(val) => {
let idx:usize = *val as usize;
if idx<MAX_GENRE_VAL {
meta.genre=lofty::id3::v1::GENRES[idx].to_string();
}
},
Err(_) => { }
}
},
None => { }
}
}
}
}
},
None => {}
}
}
meta.duration=file.properties().duration().as_secs() as u32;
},
Err(_) => { }