Cleanup tags module

Signed-off-by: Serial <69764315+Serial-ATA@users.noreply.github.com>
This commit is contained in:
Serial 2022-03-22 17:44:40 -04:00
parent 37d9f627ad
commit 625248fbc6
2 changed files with 48 additions and 61 deletions

View File

@ -25,6 +25,7 @@ pub struct FileMetadata {
pub duration: u32,
}
#[derive(Default)]
pub struct Metadata {
pub title: String,
pub artist: String,

View File

@ -15,80 +15,66 @@ const MAX_GENRE_VAL: usize = 192;
pub fn read(track: &String) -> db::Metadata {
let mut meta = db::Metadata {
title: String::new(),
artist: String::new(),
album: String::new(),
album_artist: String::new(),
genre: String::new(),
duration: 180,
..db::Metadata::default()
};
let path = Path::new(track);
match Probe::open(path) {
Ok(probe) => {
match probe.read(true) {
Ok(file) => {
let tag = match file.primary_tag() {
Some(primary_tag) => primary_tag,
None => file.first_tag().expect("Error: No tags found!"),
};
meta.title = tag.title().unwrap_or("").to_string();
meta.artist = tag.artist().unwrap_or("").to_string();
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 => {}
if let Ok(file) = lofty::read_from_path(Path::new(track), true) {
let tag = match file.primary_tag() {
Some(primary_tag) => primary_tag,
None => file.first_tag().expect("Error: No tags found!"),
};
meta.title = tag.title().unwrap_or_default().to_string();
meta.artist = tag.artist().unwrap_or_default().to_string();
meta.album = tag.album().unwrap_or_default().to_string();
meta.album_artist = tag
.get_string(&ItemKey::AlbumArtist)
.unwrap_or_default()
.to_string();
meta.genre = tag.genre().unwrap_or_default().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>();
if let Ok(val) = test {
let idx: usize = val as usize;
if idx < MAX_GENRE_VAL {
meta.genre =
lofty::id3::v1::GENRES[idx].to_string();
}
}
}
None => {}
}
}
None => {}
}
}
meta.duration = file.properties().duration().as_secs() as u32;
}
Err(_) => {}
None => {}
}
}
Err(_) => {}
meta.duration = file.properties().duration().as_secs() as u32;
}
meta
}