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.
This commit is contained in:
Max Isom 2021-05-30 15:01:11 -05:00
parent d61107aedd
commit cf75afd4bc
No known key found for this signature in database
GPG key ID: 25C9B1A7F6798880
4 changed files with 17 additions and 23 deletions

View file

@ -89,8 +89,7 @@
"spotify-uri": "^2.2.0", "spotify-uri": "^2.2.0",
"spotify-web-api-node": "^5.0.2", "spotify-web-api-node": "^5.0.2",
"sqlite3": "^5.0.2", "sqlite3": "^5.0.2",
"unique-random-array": "^2.0.0",
"youtube.ts": "^0.2.0", "youtube.ts": "^0.2.0",
"ytdl-core": "^4.5.0" "ytdl-core": "^4.8.2"
} }
} }

View file

@ -6,7 +6,7 @@ import spotifyURI from 'spotify-uri';
import Spotify from 'spotify-web-api-node'; import Spotify from 'spotify-web-api-node';
import YouTube, {YoutubePlaylistItem} from 'youtube.ts'; import YouTube, {YoutubePlaylistItem} from 'youtube.ts';
import pLimit from 'p-limit'; import pLimit from 'p-limit';
import uniqueRandomArray from 'unique-random-array'; import shuffle from 'array-shuffle';
import {QueuedSong, QueuedPlaylist} from '../services/player'; import {QueuedSong, QueuedPlaylist} from '../services/player';
import {TYPES} from '../types'; import {TYPES} from '../types';
@ -182,12 +182,9 @@ export default class {
const originalNSongs = tracks.length; const originalNSongs = tracks.length;
if (tracks.length > 50) { if (tracks.length > 50) {
const random = uniqueRandomArray(tracks); const shuffled = shuffle(tracks);
tracks = []; tracks = shuffled.slice(0, 50);
for (let i = 0; i < 50; i++) {
tracks.push(random());
}
} }
// Limit concurrency so hopefully we don't get banned for searching // Limit concurrency so hopefully we don't get banned for searching

View file

@ -1,8 +1,9 @@
{ {
"compilerOptions": { "compilerOptions": {
"lib": ["ES2018"], "lib": ["ES2019"],
"target": "es6", "target": "es2018",
"module": "commonjs", "module": "commonjs",
"moduleResolution": "node",
"declaration": true, "declaration": true,
"outDir": "dist", "outDir": "dist",
"declarationDir": "dts", "declarationDir": "dts",

View file

@ -2979,18 +2979,6 @@ undefsafe@^2.0.3:
dependencies: dependencies:
debug "^2.2.0" 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: unique-string@^2.0.0:
version "2.0.0" version "2.0.0"
resolved "https://registry.yarnpkg.com/unique-string/-/unique-string-2.0.0.tgz#39c6451f81afb2749de2b233e3f7c5e8843bd89d" 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" axios "^0.19.0"
ytdl-core "^4.0.3" ytdl-core "^4.0.3"
ytdl-core@^4.0.3, ytdl-core@^4.5.0: ytdl-core@^4.0.3:
version "4.5.0" version "4.5.0"
resolved "https://registry.yarnpkg.com/ytdl-core/-/ytdl-core-4.5.0.tgz#f07733387c548e5c3a5614c93ef55bde666eeaf4" resolved "https://registry.yarnpkg.com/ytdl-core/-/ytdl-core-4.5.0.tgz#f07733387c548e5c3a5614c93ef55bde666eeaf4"
integrity sha512-e8r6skrakWNixsVlNPBMoRM1HrdW1swE97If9nenDUjF65uogYk4DvxIuqlmqRfBWKe+6aIZwqedNxUU9XLYJA== integrity sha512-e8r6skrakWNixsVlNPBMoRM1HrdW1swE97If9nenDUjF65uogYk4DvxIuqlmqRfBWKe+6aIZwqedNxUU9XLYJA==
@ -3171,3 +3159,12 @@ ytdl-core@^4.0.3, ytdl-core@^4.5.0:
m3u8stream "^0.8.3" m3u8stream "^0.8.3"
miniget "^4.0.0" miniget "^4.0.0"
sax "^1.1.3" 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"