2024-07-24 09:07:24 +00:00
|
|
|
use std::ops::Deref;
|
|
|
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
|
|
|
pub struct AudioMetadata(pub serde_json::Value);
|
|
|
|
|
2024-08-12 16:48:33 +00:00
|
|
|
impl From<AudioMetadata> for mongodb::bson::Bson {
|
|
|
|
fn from(val: AudioMetadata) -> Self {
|
|
|
|
mongodb::bson::to_bson(&val.0).unwrap()
|
2024-07-24 09:07:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AudioMetadata {
|
|
|
|
fn get_key(&self, key: &str) -> Option<&str> {
|
2024-08-12 16:48:33 +00:00
|
|
|
self.0.as_object()?.get(key)?.as_str()
|
2024-07-24 09:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn title(&self) -> Option<&str> {
|
|
|
|
self.get_key("title")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn artist(&self) -> Option<&str> {
|
|
|
|
self.get_key("artist")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn album(&self) -> Option<&str> {
|
|
|
|
self.get_key("album")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn track_number(&self) -> Option<usize> {
|
|
|
|
self.get_key("tracknumber").map(|x| x.parse().ok())?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_metadata(file_path: &str) -> Option<AudioMetadata> {
|
|
|
|
Some(AudioMetadata(get_metadata_json(file_path)?))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_metadata_json(file_path: &str) -> Option<serde_json::Value> {
|
|
|
|
let output = std::process::Command::new("python3")
|
2024-08-02 15:23:03 +00:00
|
|
|
.arg("./extract_metadata.py")
|
2024-07-24 09:07:24 +00:00
|
|
|
.arg(file_path)
|
|
|
|
.output()
|
|
|
|
.ok()?;
|
|
|
|
|
|
|
|
let stdout = String::from_utf8_lossy(&output.stdout);
|
|
|
|
serde_json::from_str(&stdout).ok()?
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for AudioMetadata {
|
|
|
|
type Target = serde_json::Value;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|