Get the daemon to actually work
This commit is contained in:
1 parent
587f82932b
commit
19345e771c
9 files changed
+175
-92
No files matched your search
+80
-41
@@ -1,3 +1,4 @@
|
||||
use crate::playlist::Playlist;
|
||||
use crate::track::Track;
|
||||
use reqwest::Client;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -6,8 +7,7 @@ use uuid::Uuid;
|
||||
|
||||
pub struct Subsonic {
|
||||
username: String,
|
||||
salt: String,
|
||||
hash: String,
|
||||
password: String,
|
||||
base: String,
|
||||
client: Client,
|
||||
}
|
||||
@@ -29,25 +29,48 @@ struct SearchResponseSongs {
|
||||
song: Vec<Track>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct GetPlaylistResponse {
|
||||
playlist: Playlist,
|
||||
}
|
||||
|
||||
struct AuthParams {
|
||||
salt: String,
|
||||
hash: String,
|
||||
}
|
||||
|
||||
impl Subsonic {
|
||||
fn auth_params(&self) -> AuthParams {
|
||||
let salt = Uuid::new_v4().simple().to_string();
|
||||
let hash = format!(
|
||||
"{:?}",
|
||||
md5::compute(format!("{}{}", self.password, salt).into_bytes())
|
||||
);
|
||||
|
||||
AuthParams { salt, hash }
|
||||
}
|
||||
|
||||
pub async fn song_search(
|
||||
&self,
|
||||
query: String,
|
||||
) -> Result<Vec<Track>, Box<dyn std::error::Error>> {
|
||||
let auth_params = self.auth_params();
|
||||
let query_params = [
|
||||
("query", query.as_str()),
|
||||
("artistCount", "0"),
|
||||
("albumCount", "0"),
|
||||
("u", self.username.as_str()),
|
||||
("s", auth_params.salt.as_str()),
|
||||
("t", auth_params.hash.as_str()),
|
||||
("v", "16"),
|
||||
("c", "playlistd"),
|
||||
("f", "json"),
|
||||
];
|
||||
|
||||
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"),
|
||||
])
|
||||
.query(&query_params)
|
||||
.send()
|
||||
.await?
|
||||
.json::<SubsonicResponse<SearchResponse>>()
|
||||
@@ -57,37 +80,67 @@ impl Subsonic {
|
||||
.song)
|
||||
}
|
||||
|
||||
pub async fn get_playlist(
|
||||
&self,
|
||||
playlist_id: String,
|
||||
) -> Result<Playlist, Box<dyn std::error::Error>> {
|
||||
let auth_params = self.auth_params();
|
||||
|
||||
let get_params = [
|
||||
("u", self.username.to_string()),
|
||||
("s", auth_params.salt.to_string()),
|
||||
("t", auth_params.hash.to_string()),
|
||||
("v", String::from("16")),
|
||||
("c", String::from("playlistd")),
|
||||
("f", String::from("json")),
|
||||
("id", playlist_id),
|
||||
];
|
||||
|
||||
Ok(self
|
||||
.client
|
||||
.get(format!("{}/getPlaylist", self.base))
|
||||
.query(&get_params)
|
||||
.send()
|
||||
.await?
|
||||
.json::<SubsonicResponse<GetPlaylistResponse>>()
|
||||
.await?
|
||||
.subsonic_response
|
||||
.playlist)
|
||||
}
|
||||
|
||||
pub async fn update_playlist(
|
||||
&self,
|
||||
playlist_id: String,
|
||||
add: Vec<String>,
|
||||
remove: Vec<usize>,
|
||||
) -> Result<Vec<Track>, Box<dyn std::error::Error>> {
|
||||
let mut query_params = vec![
|
||||
) -> Result<Playlist, Box<dyn std::error::Error>> {
|
||||
let auth_params = self.auth_params();
|
||||
|
||||
let mut update_query = vec![
|
||||
("u", self.username.to_string()),
|
||||
("s", self.salt.to_string()),
|
||||
("t", self.hash.to_string()),
|
||||
("s", auth_params.salt.to_string()),
|
||||
("t", auth_params.hash.to_string()),
|
||||
("v", String::from("16")),
|
||||
("c", String::from("playlistd")),
|
||||
("f", String::from("json")),
|
||||
("playlistId", playlist_id.clone()),
|
||||
];
|
||||
|
||||
for id in add {
|
||||
query_params.push(("songIdToAdd", id));
|
||||
update_query.push(("songIdToAdd", id));
|
||||
}
|
||||
|
||||
for index in remove {
|
||||
query_params.push(("songIndexToRemove", index.to_string()));
|
||||
update_query.push(("songIndexToRemove", index.to_string()));
|
||||
}
|
||||
|
||||
Ok(self
|
||||
.client
|
||||
.get(format!("{}/search3", self.base))
|
||||
.query(&query_params)
|
||||
self.client
|
||||
.get(format!("{}/updatePlaylist", self.base))
|
||||
.query(&update_query)
|
||||
.send()
|
||||
.await?
|
||||
.json::<Vec<Track>>()
|
||||
.await?)
|
||||
.await?;
|
||||
|
||||
self.get_playlist(playlist_id).await
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,23 +180,9 @@ impl SubsonicBuilder {
|
||||
}
|
||||
|
||||
pub fn build(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,
|
||||
password: self.password.ok_or(SubsonicBuilderError::MissingField)?,
|
||||
base: self.base.ok_or(SubsonicBuilderError::MissingField)?,
|
||||
client: Default::default(),
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user