From c7cd09f3ec0e4039efff5e92b6fa785d5ffffcd2 Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Mon, 12 Jan 2026 00:27:08 -0600 Subject: [PATCH] feat(artist): expand artist models for new sections --- src/lib/adapters/qobuzAdapters.ts | 145 ++++++++++++++++++++++++++---- src/lib/types/index.ts | 39 ++++++++ 2 files changed, 168 insertions(+), 16 deletions(-) diff --git a/src/lib/adapters/qobuzAdapters.ts b/src/lib/adapters/qobuzAdapters.ts index 15727f897..c4ea6585b 100644 --- a/src/lib/adapters/qobuzAdapters.ts +++ b/src/lib/adapters/qobuzAdapters.ts @@ -8,6 +8,8 @@ import type { QobuzAlbum, QobuzArtist, + QobuzPlaylist, + QobuzTrack, AlbumDetail, ArtistDetail } from '$lib/types'; @@ -63,6 +65,77 @@ export function formatAlbumQuality( return 'CD Quality'; } +type ArtistAlbumSummary = ArtistDetail['albums'][number]; +type ArtistPlaylistSummary = ArtistDetail['playlists'][number]; + +function isEpOrSingle(album: QobuzAlbum): boolean { + const trackCount = album.tracks_count ?? 0; + const duration = album.duration ?? 0; + const title = album.title?.toLowerCase() ?? ''; + + if (trackCount > 0 && trackCount <= 8) return true; + if (duration > 0 && duration <= 1800) return true; + if (/\b(ep|single)\b/.test(title)) return true; + + return false; +} + +function toArtistAlbumSummary(album: QobuzAlbum): ArtistAlbumSummary { + const artwork = getQobuzImage(album.image); + const quality = formatQuality( + album.hires_streamable, + album.maximum_bit_depth, + album.maximum_sampling_rate + ); + + return { + id: album.id, + title: album.title, + artwork, + year: album.release_date_original?.split('-')[0], + quality + }; +} + +function buildCompilationAlbums(tracks: QobuzTrack[] | undefined): ArtistAlbumSummary[] { + if (!tracks || tracks.length === 0) return []; + + const seen = new Set(); + const compilations: ArtistAlbumSummary[] = []; + + for (const track of tracks) { + const album = track.album; + if (!album?.id || seen.has(album.id)) continue; + + seen.add(album.id); + compilations.push({ + id: album.id, + title: album.title, + artwork: getQobuzImage(album.image), + year: undefined, + quality: formatQuality( + track.hires_streamable, + track.maximum_bit_depth, + track.maximum_sampling_rate + ) + }); + } + + return compilations; +} + +function toArtistPlaylists(playlists: QobuzPlaylist[] | undefined): ArtistPlaylistSummary[] { + if (!playlists) return []; + + return playlists.map(playlist => ({ + id: playlist.id, + title: playlist.name, + artwork: playlist.images?.[0], + trackCount: playlist.tracks_count, + owner: playlist.owner?.name + })); +} + // ============ Model Converters ============ /** @@ -111,6 +184,20 @@ export function convertQobuzAlbum(album: QobuzAlbum): AlbumDetail { */ export function convertQobuzArtist(artist: QobuzArtist): ArtistDetail { const image = getQobuzImage(artist.image); + const albumItems = artist.albums?.items || []; + const albumsFetched = (artist.albums?.offset || 0) + albumItems.length; + + const albums: ArtistAlbumSummary[] = []; + const epsSingles: ArtistAlbumSummary[] = []; + + for (const album of albumItems) { + const summary = toArtistAlbumSummary(album); + if (isEpOrSingle(album)) { + epsSingles.push(summary); + } else { + albums.push(summary); + } + } return { id: artist.id, @@ -118,21 +205,47 @@ export function convertQobuzArtist(artist: QobuzArtist): ArtistDetail { image, albumsCount: artist.albums_count, biography: artist.biography, - albums: artist.albums?.items?.map(album => { - const artwork = getQobuzImage(album.image); - const quality = formatQuality( - album.hires_streamable, - album.maximum_bit_depth, - album.maximum_sampling_rate - ); - return { - id: album.id, - title: album.title, - artwork, - year: album.release_date_original?.split('-')[0], - quality - }; - }) || [], - totalAlbums: artist.albums?.total || artist.albums_count || 0 + albums, + epsSingles, + compilations: buildCompilationAlbums(artist.tracks_appears_on?.items), + playlists: toArtistPlaylists(artist.playlists), + totalAlbums: artist.albums?.total || artist.albums_count || 0, + albumsFetched + }; +} + +export function appendArtistAlbums( + artist: ArtistDetail, + newAlbums: QobuzAlbum[], + totalAlbums?: number, + albumsFetched?: number +): ArtistDetail { + if (newAlbums.length === 0) return artist; + + const existingAlbumIds = new Set(artist.albums.map(album => album.id)); + const existingEpIds = new Set(artist.epsSingles.map(album => album.id)); + + const albums = [...artist.albums]; + const epsSingles = [...artist.epsSingles]; + + for (const album of newAlbums) { + const summary = toArtistAlbumSummary(album); + if (isEpOrSingle(album)) { + if (!existingEpIds.has(summary.id)) { + epsSingles.push(summary); + existingEpIds.add(summary.id); + } + } else if (!existingAlbumIds.has(summary.id)) { + albums.push(summary); + existingAlbumIds.add(summary.id); + } + } + + return { + ...artist, + albums, + epsSingles, + totalAlbums: totalAlbums ?? artist.totalAlbums, + albumsFetched: albumsFetched ?? artist.albumsFetched + newAlbums.length }; } diff --git a/src/lib/types/index.ts b/src/lib/types/index.ts index 91148c9ce..a15e867b0 100644 --- a/src/lib/types/index.ts +++ b/src/lib/types/index.ts @@ -45,6 +45,16 @@ export interface QobuzAlbum { tracks?: { items: QobuzTrack[] }; } +export interface QobuzPlaylist { + id: number; + name: string; + description?: string; + owner?: { id?: number; name: string }; + images?: string[]; + tracks_count?: number; + duration?: number; +} + export interface QobuzArtist { id: number; name: string; @@ -61,6 +71,13 @@ export interface QobuzArtist { offset: number; limit: number; }; + tracks_appears_on?: { + items: QobuzTrack[]; + total: number; + offset: number; + limit: number; + }; + playlists?: QobuzPlaylist[]; } // ============ UI Display Types ============ @@ -114,7 +131,29 @@ export interface ArtistDetail { year?: string; quality: string; }[]; + epsSingles: { + id: string; + title: string; + artwork: string; + year?: string; + quality: string; + }[]; + compilations: { + id: string; + title: string; + artwork: string; + year?: string; + quality: string; + }[]; + playlists: { + id: number; + title: string; + artwork?: string; + trackCount?: number; + owner?: string; + }[]; totalAlbums: number; + albumsFetched: number; } export interface PlaylistTrack {