feat(artist): expand artist models for new sections
This commit is contained in:
1 parent
4e2491a9a6
commit
c7cd09f3ec
2 files changed
+168
-16
No files matched your search
@@ -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<string>();
|
||||
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
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user