Handle numeric genres in MP3 files

This commit is contained in:
Craig Drummond committed 2022-02-19 22:41:07 +00:00
1 parent 5ba4a7b62d
commit eec7a24f25
3 files changed
+55

No files matched your search

+42
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(_) => { }