Limit playlist adds to 50 songs

This commit is contained in:
Max Isom 2020-03-18 19:17:47 -05:00
parent a0c5875ee4
commit 0357373123
4 changed files with 47 additions and 8 deletions

View file

@ -85,6 +85,7 @@
"spotify-uri": "^2.0.0", "spotify-uri": "^2.0.0",
"spotify-web-api-node": "^4.0.0", "spotify-web-api-node": "^4.0.0",
"sqlite3": "^4.1.1", "sqlite3": "^4.1.1",
"unique-random-array": "^2.0.0",
"youtube.ts": "^0.1.1", "youtube.ts": "^0.1.1",
"ytdl-core": "^2.0.0", "ytdl-core": "^2.0.0",
"ytsr": "^0.1.11" "ytsr": "^0.1.11"

View file

@ -71,6 +71,7 @@ export default class implements Command {
} }
const newSongs: QueuedSong[] = []; const newSongs: QueuedSong[] = [];
let extraMsg = '';
// Test if it's a complete URL // Test if it's a complete URL
try { try {
@ -95,7 +96,15 @@ export default class implements Command {
} }
} }
} else if (url.protocol === 'spotify:' || url.host === 'open.spotify.com') { } else if (url.protocol === 'spotify:' || url.host === 'open.spotify.com') {
const [convertedSongs] = await this.getSongs.spotifySource(args[0]); const [convertedSongs, nSongsNotFound, totalSongs] = await this.getSongs.spotifySource(args[0]);
if (totalSongs > 50) {
extraMsg = 'a random sample of 50 songs was taken';
}
if (nSongsNotFound !== 0) {
extraMsg += `and ${nSongsNotFound.toString()} songs were not found`;
}
newSongs.push(...convertedSongs); newSongs.push(...convertedSongs);
} }
@ -122,10 +131,14 @@ export default class implements Command {
const firstSong = newSongs[0]; const firstSong = newSongs[0];
if (extraMsg !== '') {
extraMsg = ` (${extraMsg})`;
}
if (newSongs.length === 1) { if (newSongs.length === 1) {
await res.stop(`u betcha, **${firstSong.title}** added to the queue`); await res.stop(`u betcha, **${firstSong.title}** added to the queue${extraMsg}`);
} else { } else {
await res.stop(`u betcha, **${firstSong.title}** and ${newSongs.length - 1} other songs were added to the queue`); await res.stop(`u betcha, **${firstSong.title}** and ${newSongs.length - 1} other songs were added to the queue${extraMsg}`);
} }
if (this.playerManager.get(msg.guild!.id).voiceConnection === null) { if (this.playerManager.get(msg.guild!.id).voiceConnection === null) {

View file

@ -7,6 +7,7 @@ import Spotify from 'spotify-web-api-node';
import ytsr from 'ytsr'; import ytsr from 'ytsr';
import YouTube from 'youtube.ts'; import YouTube from 'youtube.ts';
import pLimit from 'p-limit'; import pLimit from 'p-limit';
import uniqueRandomArray from 'unique-random-array';
import {QueuedSong, QueuedPlaylist} from '../services/queue'; import {QueuedSong, QueuedPlaylist} from '../services/queue';
import {TYPES} from '../types'; import {TYPES} from '../types';
@ -77,10 +78,10 @@ export default class {
}); });
} }
async spotifySource(url: string): Promise<[QueuedSong[], number]> { async spotifySource(url: string): Promise<[QueuedSong[], number, number]> {
const parsed = spotifyURI.parse(url); const parsed = spotifyURI.parse(url);
const tracks: SpotifyApi.TrackObjectSimplified[] = []; let tracks: SpotifyApi.TrackObjectSimplified[] = [];
let playlist: QueuedPlaylist | null = null; let playlist: QueuedPlaylist | null = null;
@ -137,12 +138,24 @@ export default class {
} }
default: { default: {
return [[], 0]; return [[], 0, 0];
}
}
// Get 50 random songs if many
const originalNSongs = tracks.length;
if (tracks.length > 50) {
const random = uniqueRandomArray(tracks);
tracks = [];
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
const limit = pLimit(3); const limit = pLimit(5);
let songs = await Promise.all(tracks.map(async track => limit(async () => this.spotifyToYouTube(track, playlist)))); let songs = await Promise.all(tracks.map(async track => limit(async () => this.spotifyToYouTube(track, playlist))));
let nSongsNotFound = 0; let nSongsNotFound = 0;
@ -158,7 +171,7 @@ export default class {
return accum; return accum;
}, []); }, []);
return [songs as QueuedSong[], nSongsNotFound]; return [songs as QueuedSong[], nSongsNotFound, originalNSongs];
} }
private async spotifyToYouTube(track: SpotifyApi.TrackObjectSimplified, playlist: QueuedPlaylist | null): Promise<QueuedSong | null> { private async spotifyToYouTube(track: SpotifyApi.TrackObjectSimplified, playlist: QueuedPlaylist | null): Promise<QueuedSong | null> {

View file

@ -2805,6 +2805,18 @@ undefsafe@^2.0.2:
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@^1.0.0: unique-string@^1.0.0:
version "1.0.0" version "1.0.0"
resolved "https://registry.yarnpkg.com/unique-string/-/unique-string-1.0.0.tgz#9e1057cca851abb93398f8b33ae187b99caec11a" resolved "https://registry.yarnpkg.com/unique-string/-/unique-string-1.0.0.tgz#9e1057cca851abb93398f8b33ae187b99caec11a"