Fix whole playlist fails when one song is unavailable (#489)

Co-authored-by: Max Isom <hi@maxisom.me>
This commit is contained in:
Thongrapee Panyapatiphan 2022-01-26 08:18:01 +07:00 committed by GitHub
parent c89bd278d3
commit 9daf126680
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 7 deletions

View file

@ -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

View file

@ -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<SongMetadata> {
private async spotifyToYouTube(track: SpotifyApi.TrackObjectSimplified): Promise<SongMetadata> {
return this.youtubeVideoSearch(`"${track.name}" "${track.artists[0].name}"`);
}
}