feat: search, home landing, chart, genres and podcasts

- ym-api: search by kind with paging and best match, suggestions, landing
  (personal playlists, new releases and playlists) plus the full chart
  with movement, genres, tag playlists and featured podcasts
- Tag playlist ids are parsed from raw JSON: yamuse types them as strings
  but the API sends {uid, kind} objects
- Featured podcasts capped at 48 (the API lists thousands)
- Search screen: debounced suggestions floating under the field, tabs with
  totals, top result card, Show more paging, genre browsing when empty
- Home with personal playlists, top-10 chart, new releases and playlists,
  genre tiles; Chart page; Genre pages with sub-genres; Podcasts page
- Chart lists show position movement; top-N lists are built once so
  cached rows survive playback ticks
- Snapshot mode can sign in live; api_probe example for discovery calls
This commit is contained in:
Yuriy Panov committed 2026-10-15 15:04:37 +06:00
1 parent 3b934df8cc
commit e7726aac3a
19 files changed
+1520 -59

No files matched your search

+94
View File
@@ -5,9 +5,29 @@ use crate::library::{
AlbumPage, AlbumSummary, ArtistPage, ArtistSummary, LibraryPlaylists, PlaylistId, PlaylistPage,
PlaylistSummary,
};
use crate::discover::{ChartEntry, Genre, Home, SearchKind, SearchResults};
use crate::models::{Stream, Track, best_variant};
use crate::{Error, Result};
/// How many featured podcasts to resolve.
pub const FEATURED_PODCASTS: usize = 48;
/// Playlist ids from `/tags/{id}/playlist-ids`: `{"ids":[{"uid":1,"kind":2}]}`,
/// tolerating the `"uid:kind"` string form too.
fn tag_playlist_ids(raw: &serde_json::Value) -> Vec<String> {
let ids = raw.get("result").unwrap_or(raw).get("ids").and_then(|v| v.as_array());
ids.into_iter()
.flatten()
.filter_map(|id| match id {
serde_json::Value::String(s) if s.contains(':') => Some(s.clone()),
serde_json::Value::Object(o) => {
Some(format!("{}:{}", o.get("uid")?.as_i64()?, o.get("kind")?.as_i64()?))
}
_ => None,
})
.collect()
}
/// The signed-in user as the UI needs them.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Account {
@@ -211,6 +231,72 @@ impl YmClient {
})
}
/// One page of search results. `page` starts at 0.
pub async fn search(&self, text: &str, kind: SearchKind, page: u32) -> Result<SearchResults> {
let mut query = yamuse::SearchQuery::default().of_type(kind.to_yamuse());
query.page = page;
Ok(SearchResults::from_yamuse(self.inner.search(text, query).await?))
}
/// Completions for a partial query, best first.
pub async fn suggest(&self, part: &str) -> Result<Vec<String>> {
Ok(self.inner.search_suggest(part).await?.suggestions)
}
/// The home page: personal playlists, new releases and playlists, the chart.
pub async fn home(&self) -> Result<Home> {
let landing = self
.inner
.landing(["personalplaylists", "new-releases", "new-playlists"])
.await?;
let mut home = Home::from_landing(landing);
// The chart block on landing3 holds a handful of items; the chart endpoint has them all.
match self.chart().await {
Ok(chart) if !chart.is_empty() => home.chart = chart,
Ok(_) => {}
Err(e) => eprintln!("chart: {e}"),
}
Ok(home)
}
pub async fn chart(&self) -> Result<Vec<ChartEntry>> {
let info = self.inner.chart().await?;
Ok(info
.chart
.map(|p| p.tracks.into_iter().filter_map(ChartEntry::from_short).collect())
.unwrap_or_default())
}
pub async fn genres(&self) -> Result<Vec<Genre>> {
Ok(self.inner.genres().await?.into_iter().filter_map(Genre::from_yamuse).collect())
}
/// Editorial playlists filed under a genre or mood tag.
pub async fn tag_playlists(&self, tag: &str) -> Result<Vec<PlaylistSummary>> {
// yamuse types these ids as strings; the API sends `{uid, kind}` objects.
let raw = self
.inner
.get_raw(&format!("/tags/{tag}/playlist-ids"), yamuse::Params::new())
.await?;
let ids = tag_playlist_ids(&raw);
if ids.is_empty() {
return Ok(Vec::new());
}
let playlists = self.inner.playlists_list(&ids).await?;
Ok(playlists.iter().filter_map(PlaylistSummary::from_yamuse).collect())
}
/// Podcasts Yandex currently features (the first [`FEATURED_PODCASTS`]; the list runs to thousands).
pub async fn featured_podcasts(&self) -> Result<Vec<AlbumSummary>> {
let mut ids = self.inner.podcasts().await?.podcasts;
ids.truncate(FEATURED_PODCASTS);
if ids.is_empty() {
return Ok(Vec::new());
}
let albums = self.inner.albums(&ids).await?;
Ok(albums.into_iter().filter_map(AlbumSummary::from_yamuse).collect())
}
/// Downloads an image (covers are a few kilobytes).
pub async fn image(&self, url: &str) -> Result<Vec<u8>> {
Ok(yamuse::download::Downloader::new(self.inner.transport(), [url.to_owned()])
@@ -259,6 +345,14 @@ mod tests {
assert_eq!(a.subscription_end, None);
}
#[test]
fn tag_ids_accept_objects_and_strings() {
let raw = serde_json::json!({"ids": [{"uid": 1, "kind": 2}, "3:4", null, {"uid": 5}]});
assert_eq!(tag_playlist_ids(&raw), vec!["1:2", "3:4"]);
let wrapped = serde_json::json!({"result": {"ids": [{"uid": 9, "kind": 8}]}});
assert_eq!(tag_playlist_ids(&wrapped), vec!["9:8"]);
}
#[test]
fn anonymous_status_is_not_an_account() {
assert!(Account::from_status(status(r#"{"account":{"now":"2026-10-15"}}"#)).is_none());