feat: music video clips on artist pages and in Collection

Artist pages list the artist's clips and Collection gets a Clips tab with
the liked ones. A clip opens on the Yandex Music website, since there's
no video player in the app; the heart on each card likes it. Clips are
read from raw JSON because the artist block calls the id `id` where
/clips says `clipId`. Snapshot runs can open `artist:<id>`.
This commit is contained in:
Yuriy Panov committed 2026-10-15 18:50:50 +06:00
1 parent 2ef7aa9da9
commit 51e7a4f465
13 files changed
+396 -18

No files matched your search

+71 -1
View File
@@ -1,6 +1,7 @@
use std::sync::Arc;
use crate::auth::Credentials;
use crate::clips::{self, Clip, LikedEntry};
use crate::library::{
AlbumPage, AlbumSummary, ArtistPage, ArtistSummary, LibraryPlaylists, PlaylistId, PlaylistPage,
PlaylistSummary,
@@ -235,7 +236,13 @@ impl YmClient {
pub async fn artist(&self, id: i64) -> Result<ArtistPage> {
let brief = self.inner.artist_brief_info(id).await?;
ArtistPage::from_yamuse(brief).ok_or_else(|| Error::Api(format!("artist {id} not found")))
let mut page = ArtistPage::from_yamuse(brief).ok_or_else(|| Error::Api(format!("artist {id} not found")))?;
// The page is still worth showing without its videos.
match self.artist_clips(id).await {
Ok(clips) => page.clips = clips,
Err(e) => eprintln!("artist clips: {e}"),
}
Ok(page)
}
/// A playlist with all its tracks. Tracks the response didn't embed are looked up.
@@ -444,6 +451,59 @@ impl YmClient {
Ok(())
}
// ---- Clips ----
/// An artist's music videos, newest first (the first 50).
pub async fn artist_clips(&self, artist_id: i64) -> Result<Vec<Clip>> {
let raw = self
.inner
.get_raw(&format!("/artists/{artist_id}/blocks/artist-clips"), yamuse::Params::new())
.await?;
Ok(clips::artist_block(&raw))
}
/// Every clip the user liked, newest first. Bare references are looked up.
pub async fn liked_clips(&self) -> Result<Vec<Clip>> {
const PAGE: u64 = 100;
let uid = self.inner.account_uid().ok_or_else(|| Error::Unauthorized("not signed in".into()))?;
let mut entries = Vec::new();
for page in 0..20 {
let params = yamuse::Params::new().add("page", page).add("pageSize", PAGE);
let raw = self.inner.get_raw(&format!("/users/{uid}/likes/clips"), params).await?;
entries.extend(clips::liked_page(&raw));
if !clips::has_more(&raw, page) {
break;
}
}
let missing: Vec<i64> = entries.iter().filter_map(|e| if let LikedEntry::Id(id) = e { Some(*id) } else { None }).collect();
let mut found: std::collections::HashMap<i64, Clip> = std::collections::HashMap::new();
for chunk in missing.chunks(PAGE as usize) {
let ids = chunk.iter().map(i64::to_string).collect::<Vec<_>>().join(",");
let raw = self.inner.get_raw("/clips", yamuse::Params::new().add("clipIds", ids)).await?;
for v in raw.as_array().into_iter().flatten() {
if let Some(clip) = Clip::from_value(v, None) {
found.insert(clip.id, clip);
}
}
}
Ok(entries
.into_iter()
.filter_map(|e| match e {
LikedEntry::Full(clip) => Some(clip),
LikedEntry::Id(id) => found.remove(&id),
})
.collect())
}
pub async fn set_clip_liked(&self, id: i64, liked: bool) -> Result<()> {
if liked {
self.inner.like_clip(id).await?;
} else {
self.inner.unlike_clip(id).await?;
}
Ok(())
}
// ---- Likes ----
/// Track ids are `full_id`s or plain ids; Yandex accepts both.
@@ -480,7 +540,17 @@ impl YmClient {
let artists = self.inner.liked_artists(None).await?;
let playlists = self.inner.liked_playlists(None).await?;
let disliked = self.inner.disliked_tracks(None).await?;
// Clips are optional: a failure here shouldn't hide every other heart.
let clips = match self.liked_clips().await {
Ok(clips) => clips.into_iter().map(|c| c.id).collect(),
Err(e @ Error::Unauthorized(_)) => return Err(e),
Err(e) => {
eprintln!("liked clips: {e}");
Default::default()
}
};
Ok(LikedIds {
clips,
albums: albums
.into_iter()
.filter_map(|l| l.album.and_then(|a| a.id).or_else(|| l.id.and_then(|i| i.as_number())))