Added the playlist generator
This commit is contained in:
@@ -21,14 +21,21 @@ pub struct MusicBrainzFetcher {
|
||||
|
||||
impl MusicBrainzFetcher {
|
||||
pub fn new() -> DataResult<Self> {
|
||||
Self::with_limiter(RateLimiter::new(RATE_LIMIT))
|
||||
}
|
||||
|
||||
/// Create a fetcher that shares a rate limiter with other MB clients.
|
||||
pub fn with_limiter(limiter: RateLimiter) -> DataResult<Self> {
|
||||
let client = reqwest::Client::builder()
|
||||
.user_agent(USER_AGENT)
|
||||
.timeout(Duration::from_secs(30))
|
||||
.build()?;
|
||||
Ok(Self {
|
||||
client,
|
||||
limiter: RateLimiter::new(RATE_LIMIT),
|
||||
})
|
||||
Ok(Self { client, limiter })
|
||||
}
|
||||
|
||||
/// Get a clone of the rate limiter for sharing with other MB clients.
|
||||
pub fn limiter(&self) -> RateLimiter {
|
||||
self.limiter.clone()
|
||||
}
|
||||
|
||||
async fn get_json<T: serde::de::DeserializeOwned>(&self, url: &str) -> DataResult<T> {
|
||||
@@ -84,6 +91,24 @@ impl MusicBrainzFetcher {
|
||||
urls,
|
||||
})
|
||||
}
|
||||
|
||||
/// Resolve a release-group MBID to a release MBID (first release in the group).
|
||||
pub async fn resolve_release_from_group(&self, release_group_mbid: &str) -> DataResult<String> {
|
||||
let url = format!("{BASE_URL}/release?release-group={release_group_mbid}&fmt=json&limit=1");
|
||||
let resp: serde_json::Value = self.get_json(&url).await?;
|
||||
|
||||
resp.get("releases")
|
||||
.and_then(|r| r.as_array())
|
||||
.and_then(|arr| arr.first())
|
||||
.and_then(|r| r.get("id"))
|
||||
.and_then(|id| id.as_str())
|
||||
.map(String::from)
|
||||
.ok_or_else(|| {
|
||||
DataError::Other(format!(
|
||||
"no releases for release-group {release_group_mbid}"
|
||||
))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl MetadataFetcher for MusicBrainzFetcher {
|
||||
|
||||
Reference in New Issue
Block a user