1- import { ADD_TRACKS_TO_PLAYLIST_URL , GET_LIKED_SONGS_LIST_URL , GET_PLAYLIST_TRACKS_URL , LIKED_SONGS_PLAYLIST_FACADE } from '../constants' ;
2- import { PlaylistInfo , PlaylistRowsResponse , SpotifyCollectionCallResponse } from '../types' ;
1+ import { ADD_TRACKS_TO_PLAYLIST_URL , LIKED_SONGS_PLAYLIST_FACADE } from '../constants' ;
2+ import { PlaylistInfo , PlaylistContentsResponse } from '../types' ;
33import { splitArrayInChunks } from './' ;
44
55// Cache used when fetching playlist tracks to avoid unnecessary API calls
@@ -8,8 +8,8 @@ export const playlistCache = new Map<string, string[]>();
88export async function combinePlaylists ( sourcePlaylists : PlaylistInfo [ ] , targetPlaylist : PlaylistInfo , autoSync = false ) {
99 const sourceUris = await Promise . all ( sourcePlaylists . map ( async ( playlist ) : Promise < string [ ] > => {
1010 if ( playlist . id === LIKED_SONGS_PLAYLIST_FACADE . id ) {
11- return Spicetify . CosmosAsync . get ( GET_LIKED_SONGS_LIST_URL )
12- . then ( ( res : SpotifyCollectionCallResponse ) => res . item . map ( item => item . trackMetadata . link ) ) ;
11+ const res : PlaylistContentsResponse = await Spicetify . Platform . LibraryAPI . getTracks ( { limit : 99999 } ) ;
12+ return extractTrackUris ( res . items ) ;
1313 } else {
1414 return getPlaylistTracksWithCache ( playlist . uri ) ;
1515 }
@@ -46,9 +46,15 @@ async function getPlaylistTracksWithCache(uri: string) {
4646 if ( cachedPlaylistTracks ) {
4747 return Promise . resolve ( cachedPlaylistTracks ) ;
4848 } else {
49- const tracks = await Spicetify . CosmosAsync . get ( GET_PLAYLIST_TRACKS_URL ( uri ) )
50- . then ( ( res : PlaylistRowsResponse ) => res . rows . map ( ( row ) => row . link ) ) ;
49+ const res : PlaylistContentsResponse = await Spicetify . Platform . PlaylistAPI . getContents ( uri ) ;
50+ const tracks = extractTrackUris ( res . items ) ;
5151 playlistCache . set ( uri , tracks ) ;
5252 return tracks ;
5353 }
5454}
55+
56+ function extractTrackUris ( items : PlaylistContentsResponse [ 'items' ] ) : string [ ] {
57+ return items
58+ . filter ( ( item ) => item . uri ?. startsWith ( "spotify:track:" ) && item . isPlayable )
59+ . map ( ( item ) => item . uri ) ;
60+ }
0 commit comments