From 9daf12668023c8902fe7aea4f610c3d51fff4880 Mon Sep 17 00:00:00 2001 From: Thongrapee Panyapatiphan Date: Wed, 26 Jan 2022 08:18:01 +0700 Subject: [PATCH] Fix whole playlist fails when one song is unavailable (#489) Co-authored-by: Max Isom --- CHANGELOG.md | 2 ++ src/services/get-songs.ts | 17 ++++++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a1e9c2c..0282b6d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed +- Queueing Spotify playlists could sometimes fail when a song wasn't found on YouTube ## [0.5.0] - 2022-01-21 ### Changed diff --git a/src/services/get-songs.ts b/src/services/get-songs.ts index fb1b4ea..e132d7b 100644 --- a/src/services/get-songs.ts +++ b/src/services/get-songs.ts @@ -255,14 +255,17 @@ export default class { tracks = shuffled.slice(0, playlistLimit); } - let songs = await Promise.all(tracks.map(async track => this.spotifyToYouTube(track, playlist))); + const searchResults = await Promise.allSettled(tracks.map(async track => this.spotifyToYouTube(track))); let nSongsNotFound = 0; - // Get rid of null values - songs = songs.reduce((accum: SongMetadata[], song) => { - if (song) { - accum.push(song); + // Count songs that couldn't be found + const songs: SongMetadata[] = searchResults.reduce((accum: SongMetadata[], result) => { + if (result.status === 'fulfilled') { + accum.push({ + ...result.value, + ...(playlist ? {playlist} : {}), + }); } else { nSongsNotFound++; } @@ -270,10 +273,10 @@ export default class { return accum; }, []); - return [songs as SongMetadata[], nSongsNotFound, originalNSongs]; + return [songs, nSongsNotFound, originalNSongs]; } - private async spotifyToYouTube(track: SpotifyApi.TrackObjectSimplified, _: QueuedPlaylist | null): Promise { + private async spotifyToYouTube(track: SpotifyApi.TrackObjectSimplified): Promise { return this.youtubeVideoSearch(`"${track.name}" "${track.artists[0].name}"`); } }