-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(combine-playlists): optimize and remove duplicate code
- Loading branch information
Showing
4 changed files
with
51 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,42 @@ | ||
import { ADD_TRACKS_TO_PLAYLIST_URL, LIKED_SONGS_PLAYLIST_FACADE, TRACKS_FROM_PLAYLIST_URL_FILTER } from '../constants'; | ||
import { SpotifyCollectionCallResponse } from '../types'; | ||
import { getPaginatedSpotifyData, splitArrayInChunks } from './'; | ||
import { ADD_TRACKS_TO_PLAYLIST_URL, GET_LIKED_SONGS_LIST_URL, GET_PLAYLIST_TRACKS_URL, LIKED_SONGS_PLAYLIST_FACADE } from '../constants'; | ||
import { PlaylistInfo, PlaylistRowsResponse, SpotifyCollectionCallResponse } from '../types'; | ||
import { splitArrayInChunks } from './'; | ||
|
||
export async function combinePlaylists(sourcePlaylists: SpotifyApi.PlaylistObjectSimplified[], targetPlaylist: SpotifyApi.PlaylistObjectSimplified) { | ||
const allTrackUris = await Promise.all(sourcePlaylists.map(async (playlist): Promise<string[]> => { | ||
export async function combinePlaylists(sourcePlaylists: PlaylistInfo[], targetPlaylist: PlaylistInfo, autoSync = false) { | ||
const sourceUris = await Promise.all(sourcePlaylists.map(async (playlist): Promise<string[]> => { | ||
if (playlist.id === LIKED_SONGS_PLAYLIST_FACADE.id) { | ||
return await Spicetify.CosmosAsync.get('sp://core-collection/unstable/@/list/tracks/all?responseFormat=protobufJson') | ||
return Spicetify.CosmosAsync.get(GET_LIKED_SONGS_LIST_URL) | ||
.then((res: SpotifyCollectionCallResponse) => res.item.map(item => item.trackMetadata.link)); | ||
} else { | ||
return await getPaginatedSpotifyData<{ track: { uri: string } }>(playlist.tracks.href + TRACKS_FROM_PLAYLIST_URL_FILTER) | ||
.then(items => items.map(item => item.track.uri)); | ||
return Spicetify.CosmosAsync.get(GET_PLAYLIST_TRACKS_URL(playlist.uri)) | ||
.then((res: PlaylistRowsResponse) => res.rows.map((row) => row.link)); | ||
} | ||
// Flatten responses and remove duplicates | ||
})).then(arrays => Array.from(new Set(arrays.flat()))); | ||
|
||
const targetTrackUris = await getPaginatedSpotifyData<{ track: { uri: string } }>(targetPlaylist.tracks.href + TRACKS_FROM_PLAYLIST_URL_FILTER) | ||
.then(items => items.map(item => item.track.uri)); | ||
const targetUris = await Spicetify.CosmosAsync.get(GET_PLAYLIST_TRACKS_URL(targetPlaylist.uri)) | ||
.then((res: PlaylistRowsResponse) => res.rows.map(({ link }) => link)); | ||
|
||
// Filter duplicates from souces usig new Set, then filter duplicates from targetPlaylist using .filter | ||
const sourcesTrackUris = allTrackUris.filter((sourceUri) => !targetTrackUris.includes(sourceUri)); | ||
const splittedTrackUris = splitArrayInChunks(sourcesTrackUris); | ||
const missingUris = sourceUris.filter((sourceUri) => !targetUris.includes(sourceUri)); | ||
const uriChunks = splitArrayInChunks(missingUris); | ||
|
||
await Promise.all(splittedTrackUris.map((trackUris) => { | ||
if (missingUris.length > 0 && autoSync) { | ||
Spicetify.showNotification(`Auto-syncing ${missingUris.length} missing tracks to playlist ${targetPlaylist.name}`); | ||
} | ||
|
||
await Promise.all(uriChunks.map((trackUris) => { | ||
return addTracksToPlaylist(targetPlaylist.id, trackUris); | ||
})); | ||
|
||
Spicetify.showNotification(`Added ${sourcesTrackUris.length} tracks to playlist: ${targetPlaylist.name}`); | ||
if (missingUris.length > 0) { | ||
const msg = autoSync | ||
? `Auto-synced ${missingUris.length} missing tracks to playlist ${targetPlaylist.name} 🔥` | ||
: `Added ${missingUris.length} tracks to playlist: ${targetPlaylist.name}`; | ||
Spicetify.showNotification(msg); | ||
} | ||
} | ||
|
||
export function addTracksToPlaylist(playlistId: string, trackUris: string[]) { | ||
return Spicetify.CosmosAsync.post(ADD_TRACKS_TO_PLAYLIST_URL(playlistId), { uris: trackUris }); | ||
} | ||
} |