Add track lookup by name/artist/album

This commit is contained in:
claude Bot committed 2024-03-17 16:53:07 +00:00
1 parent 08cc752932
commit 3a196153ba
24 files changed
+931 -2010

No files matched your search

+141
View File
@@ -0,0 +1,141 @@
use crate::track::Track;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use uuid::Uuid;
pub struct Subsonic {
username: String,
salt: String,
hash: String,
base: String,
client: Client,
}
#[derive(Serialize, Deserialize)]
pub struct SubsonicResponse<T> {
#[serde(rename = "subsonic-response")]
subsonic_response: T,
}
#[derive(Serialize, Deserialize)]
struct SearchResponse {
#[serde(rename = "searchResult3")]
search_results: SearchResponseSongs,
}
#[derive(Serialize, Deserialize)]
struct SearchResponseSongs {
song: Vec<Track>,
}
impl Subsonic {
pub async fn song_search(
&self,
query: String,
) -> Result<Vec<Track>, Box<dyn std::error::Error>> {
Ok(self
.client
.get(format!("{}/search3", self.base))
.query(&[
("query", query.as_str()),
("artistCount", "0"),
("albumCount", "0"),
("u", self.username.as_str()),
("s", self.salt.as_str()),
("t", self.hash.as_str()),
("v", "16"),
("c", "playlistd"),
("f", "json"),
])
.send()
.await?
.json::<SubsonicResponse<SearchResponse>>()
.await?
.subsonic_response
.search_results
.song)
}
pub async fn update_playlist(
&self,
playlist_id: String,
add_ids: Vec<String>,
remove_indexes: Vec<usize>,
) -> Result<Vec<Track>, Box<dyn std::error::Error>> {
Ok(self
.client
.get(format!("{}/search3", self.base))
.query(&[
("u", self.username.as_str()),
("s", self.salt.as_str()),
("t", self.hash.as_str()),
("v", "16"),
("c", "playlistd"),
("f", "json"),
])
.send()
.await?
.json::<Vec<Track>>()
.await?)
}
}
#[derive(Default)]
pub struct SubsonicBuilder {
username: Option<String>,
password: Option<String>,
base: Option<String>,
}
#[derive(Debug, Error)]
enum SubsonicBuilderError {
#[error("Field not provided")]
MissingField,
}
impl SubsonicBuilder {
pub fn new() -> Self {
Self {
..Default::default()
}
}
pub fn username(mut self, username: String) -> Self {
self.username = Some(username);
self
}
pub fn password(mut self, password: String) -> Self {
self.password = Some(password);
self
}
pub fn base(mut self, base: String) -> Self {
self.base = Some(base);
self
}
pub fn build(mut self) -> Result<Subsonic, Box<dyn std::error::Error>> {
let salt = Uuid::new_v4().simple().to_string();
let hash = format!(
"{:?}",
md5::compute(
format!(
"{}{}",
self.password.ok_or(SubsonicBuilderError::MissingField)?,
salt
)
.as_bytes()
)
);
Ok(Subsonic {
username: self.username.ok_or(SubsonicBuilderError::MissingField)?,
salt,
hash,
base: self.base.ok_or(SubsonicBuilderError::MissingField)?,
client: Default::default(),
})
}
}