From cf75afd4bc1d54223ffa59ec9ddb8a7c255facf6 Mon Sep 17 00:00:00 2001 From: Max Isom Date: Sun, 30 May 2021 15:01:11 -0500 Subject: [PATCH] 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. --- package.json | 3 +-- src/services/get-songs.ts | 9 +++------ tsconfig.json | 5 +++-- yarn.lock | 23 ++++++++++------------- 4 files changed, 17 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index 3fbf223..18c3bef 100644 --- a/package.json +++ b/package.json @@ -89,8 +89,7 @@ "spotify-uri": "^2.2.0", "spotify-web-api-node": "^5.0.2", "sqlite3": "^5.0.2", - "unique-random-array": "^2.0.0", "youtube.ts": "^0.2.0", - "ytdl-core": "^4.5.0" + "ytdl-core": "^4.8.2" } } 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 diff --git a/tsconfig.json b/tsconfig.json index 219c7c7..f8546fb 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,8 +1,9 @@ { "compilerOptions": { - "lib": ["ES2018"], - "target": "es6", + "lib": ["ES2019"], + "target": "es2018", "module": "commonjs", + "moduleResolution": "node", "declaration": true, "outDir": "dist", "declarationDir": "dts", diff --git a/yarn.lock b/yarn.lock index 11df1a8..7b33368 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2979,18 +2979,6 @@ undefsafe@^2.0.3: dependencies: debug "^2.2.0" -unique-random-array@^2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/unique-random-array/-/unique-random-array-2.0.0.tgz#9e639b1a9dc141e97350a6fc6f17da4b0717b1ad" - integrity sha512-xR87O95fZ7hljw84J8r1YDXrvffPLWN513BNOP4Bv0KcgG5dyEUrHwsvP7mVAOKg4Y80uqRbpUk0GKr8il70qg== - dependencies: - unique-random "^2.1.0" - -unique-random@^2.1.0: - version "2.1.0" - resolved "https://registry.yarnpkg.com/unique-random/-/unique-random-2.1.0.tgz#7a8413da5176d028567168b57125ac5c0cec5c25" - integrity sha512-iQ1ZgWac3b8YxGThecQFRQiqgk6xFERRwHZIWeVVsqlbmgCRl0PY13R4mUkodNgctmg5b5odG1nyW/IbOxQTqg== - unique-string@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/unique-string/-/unique-string-2.0.0.tgz#39c6451f81afb2749de2b233e3f7c5e8843bd89d" @@ -3163,7 +3151,7 @@ youtube.ts@^0.2.0: axios "^0.19.0" ytdl-core "^4.0.3" -ytdl-core@^4.0.3, ytdl-core@^4.5.0: +ytdl-core@^4.0.3: version "4.5.0" resolved "https://registry.yarnpkg.com/ytdl-core/-/ytdl-core-4.5.0.tgz#f07733387c548e5c3a5614c93ef55bde666eeaf4" integrity sha512-e8r6skrakWNixsVlNPBMoRM1HrdW1swE97If9nenDUjF65uogYk4DvxIuqlmqRfBWKe+6aIZwqedNxUU9XLYJA== @@ -3171,3 +3159,12 @@ ytdl-core@^4.0.3, ytdl-core@^4.5.0: m3u8stream "^0.8.3" miniget "^4.0.0" sax "^1.1.3" + +ytdl-core@^4.8.2: + version "4.8.2" + resolved "https://registry.yarnpkg.com/ytdl-core/-/ytdl-core-4.8.2.tgz#f034ad942c5d958f5987fc8ff0b0639664ae2fb7" + integrity sha512-O3n++YcgZawaXJwbPmnRDgfN6b4kU0DpNdkI9Na5yM3JAdfJmoq5UHc8v9Xjgjr1RilQUUh7mhDnRRPDtKr0Kg== + dependencies: + m3u8stream "^0.8.3" + miniget "^4.0.0" + sax "^1.1.3"