Search API #792
Replies: 3 comments 1 reply
-
Looks like it's not planned atm but if you have some code doing it I'd be interested. :) |
Beta Was this translation helpful? Give feedback.
-
Doesn't |
Beta Was this translation helpful? Give feedback.
-
I converted part of the code from librespot-golang's mercury API into Rust, just enough to cover my use-case. The search implementation in extern crate form_urlencoded;
use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
const SEARCH_URI: &str = "hm://searchview/km/v4/search";
pub async fn get_search(&self, query: &str, limit: i64) -> Result<SearchResponse, MercuryError> {
let query_enc = utf8_percent_encode(query, NON_ALPHANUMERIC).to_string();
let params_enc = form_urlencoded::Serializer::new(String::from("")).extend_pairs(&[
("entityVersion", "2"),
("limit", limit.to_string().as_str()),
("imageSize", "large"),
("catalogue", ""),
("country", self.session.country().as_str()),
("platform", "zelda"),
("username", self.session.username().as_str()),
]).finish();
let uri = format!("{}/{}?{}", SEARCH_URI, query_enc, params_enc);
let response = self.session.mercury().get(uri).await?;
let data = response.payload.first().expect("Empty payload");
let data = String::from_utf8(data.clone()).unwrap();
let search_response: SearchResponse = serde_json::from_str(&data).unwrap();
Ok(search_response)
} If someone wants to use this, please note that it contains And here are the types: #[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct SearchResponse {
pub results: SearchResult,
pub request_id: String,
pub categories_order: Vec<String>,
}
#[derive(Deserialize, Debug, Clone)]
#[allow(dead_code)]
pub struct SRArtist {
image: Option<String>,
name: String,
uri: String,
}
#[derive(Deserialize, Debug, Clone)]
#[allow(dead_code)]
pub struct SRAlbum {
artists: Option<Vec<SRArtist>>,
image: Option<String>,
name: String,
uri: String,
}
#[derive(Deserialize, Debug, Clone)]
#[allow(dead_code)]
pub struct SRTrack {
album: Option<SRAlbum>,
artists: Option<Vec<SRArtist>>,
image: Option<String>,
name: String,
uri: String,
duration: Option<i64>,
popularity: Option<f64>,
}
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
pub struct SRPlaylist {
image: Option<String>,
name: String,
uri: String,
followers_count: Option<i64>,
author: String,
}
#[derive(Deserialize, Debug, Clone)]
pub struct SearchResultCategory<T> {
pub hits: Vec<T>,
pub total: i64,
}
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
pub struct SearchResult {
tracks: SearchResultCategory<SRTrack>,
albums: SearchResultCategory<SRAlbum>,
artists: SearchResultCategory<SRArtist>,
playlists: SearchResultCategory<SRPlaylist>,
//shows: SearchResultCategory<SRShow>,
//profiles: SearchResultCategory<SRProfile>,
//genres: SearchResultCategory<SRGenre>,
//top_hit: SearchResultCategory<SRTopHit>,
//video_episodes: SearchResultCategory<SRVideoEpisodes>,
//top_recommendations: SearchResultCategory<SRTopRecommendations>,
// see https://github.com/librespot-org/librespot-golang/blob/master/librespot/metadata/metadata.go
} @gdesmott Let me know if this helps or if I forgot something 🙂 |
Beta Was this translation helpful? Give feedback.
-
Is there interest in receiving patches that add search functionality?
librespot-golang supports this, and after switching to this lib, I implemented some parts of it.
(This a duplicate of #644, but that thread was originally from the old repo.)
Beta Was this translation helpful? Give feedback.
All reactions