From cf75afd4bc1d54223ffa59ec9ddb8a7c255facf6 Mon Sep 17 00:00:00 2001 From: Max Isom Date: Sun, 30 May 2021 15:01:11 -0500 Subject: Fix shuffle bug for long Spotify playlists A random sample of 50 songs is taken for long playlists since mapping Spotify -> YouTube is expensive. However, the function used previously allowed for duplicates within that sample of 50. --- src/services/get-songs.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'src/services') diff --git a/src/services/get-songs.ts b/src/services/get-songs.ts index d3e951d..c7d37ea 100644 --- a/src/services/get-songs.ts +++ b/src/services/get-songs.ts @@ -6,7 +6,7 @@ import spotifyURI from 'spotify-uri'; import Spotify from 'spotify-web-api-node'; import YouTube, {YoutubePlaylistItem} from 'youtube.ts'; import pLimit from 'p-limit'; -import uniqueRandomArray from 'unique-random-array'; +import shuffle from 'array-shuffle'; import {QueuedSong, QueuedPlaylist} from '../services/player'; import {TYPES} from '../types'; @@ -182,12 +182,9 @@ export default class { const originalNSongs = tracks.length; if (tracks.length > 50) { - const random = uniqueRandomArray(tracks); + const shuffled = shuffle(tracks); - tracks = []; - for (let i = 0; i < 50; i++) { - tracks.push(random()); - } + tracks = shuffled.slice(0, 50); } // Limit concurrency so hopefully we don't get banned for searching -- cgit v1.2.3