Skip to content

Commit

Permalink
Filter out podcast episodes when importing playlists. Fix #1679
Browse files Browse the repository at this point in the history
  • Loading branch information
nukeop committed Aug 19, 2024
1 parent 7ceca53 commit d661b98
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions packages/core/src/rest/Spotify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ export type SpotifyTrack = {
popularity: number;
track_number: number;
duration_ms: number;
type: 'track';
}

export type SpotifyEpisode = {
// We only want to filter this out
type: 'episode';
}

export type SpotifyPlaylistResponse = {
Expand All @@ -84,11 +90,15 @@ export type SpotifyPlaylist = {
}

export type SpotifyPlaylistTrackObject = {
track: SpotifyTrack;
track: SpotifyTrack | SpotifyEpisode;
}

export type SpotifyPlaylistTracksResponse = SpotifyPaginatedResponse<SpotifyPlaylistTrackObject>;

export const isTrack = (track: SpotifyTrack | SpotifyEpisode): track is SpotifyTrack => {
return track.type === 'track';
};

class SpotifyClient {
private _token: string | undefined;

Expand Down Expand Up @@ -203,12 +213,12 @@ class SpotifyClient {

async getPlaylist(id: string): Promise<SpotifyPlaylist> {
const playlistResponse: SpotifyPlaylistResponse = await this.get(`${SPOTIFY_API_URL}/playlists/${id}`);
let tracks = playlistResponse.tracks.items.map(item => item.track);
let tracks = playlistResponse.tracks.items.map(item => item.track).filter(isTrack) as SpotifyTrack[];
let data = playlistResponse.tracks;

while (data.next) {
const nextData: SpotifyPaginatedResponse<SpotifyPlaylistTrackObject> = await this.get(data.next);
tracks = [...tracks, ...(nextData.items.map(item => item.track))];
const nextData: SpotifyPaginatedResponse<SpotifyPlaylistTrackObject> = await this.get(data.next);
tracks = [...tracks, ...(nextData.items.map(item => item.track).filter(isTrack))] as SpotifyTrack[];
data = nextData;
}

Expand Down Expand Up @@ -236,7 +246,6 @@ export const getImageSet = (images: SpotifyImage[]): { thumb?: string; coverImag
export const mapSpotifyTrack = (track: SpotifyTrack): PlaylistTrack | null => {
const { thumb } = getImageSet(track.album?.images ?? []);
try {

return {
uuid: track.id,
artist: track.artists[0].name,
Expand Down

0 comments on commit d661b98

Please sign in to comment.