mirror of
https://github.com/CDrummond/bliss-analyser.git
synced 2025-04-08 05:00:02 +03:00
Cleanup tags
module
Signed-off-by: Serial <69764315+Serial-ATA@users.noreply.github.com>
This commit is contained in:
parent
37d9f627ad
commit
625248fbc6
@ -25,6 +25,7 @@ pub struct FileMetadata {
|
||||
pub duration: u32,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Metadata {
|
||||
pub title: String,
|
||||
pub artist: String,
|
||||
|
108
src/tags.rs
108
src/tags.rs
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user