From 30af411c075d84f45ee034283a832b7867b99752 Mon Sep 17 00:00:00 2001 From: Snowflake107 Date: Thu, 13 May 2021 13:55:11 +0545 Subject: [PATCH] fetchBeforeQueued --- src/Player.ts | 64 ++++++++++++++++++++++++++++++++++++---------- src/types/types.ts | 1 + src/utils/Util.ts | 15 +++++++++++ 3 files changed, 66 insertions(+), 14 deletions(-) diff --git a/src/Player.ts b/src/Player.ts index 29e4d87..7018fb4 100644 --- a/src/Player.ts +++ b/src/Player.ts @@ -203,6 +203,19 @@ export class Player extends EventEmitter { source: 'spotify' }); + if (this.options.fetchBeforeQueued) { + const searchQueryString = this.options.disableArtistSearch + ? spotifyTrack.title + : `${spotifyTrack.title}${' - ' + spotifyTrack.author}`; + const ytv = await YouTube.search(searchQueryString, { limit: 1, type: "video" }).catch(e => { }); + + if (ytv && ytv[0]) Util.define({ + target: spotifyTrack, + prop: 'backupLink', + value: ytv[0].url + }); + } + tracks = [spotifyTrack]; } } @@ -219,7 +232,7 @@ export class Player extends EventEmitter { let tracks: Track[] = []; if (playlist.type !== 'playlist') - tracks = playlist.tracks.items.map((m: any) => { + tracks = await Promise.all(playlist.tracks.items.map(async (m: any) => { const data = new Track(this, { title: m.name ?? '', description: m.description ?? '', @@ -235,10 +248,24 @@ export class Player extends EventEmitter { fromPlaylist: true, source: 'spotify' }); + + if (this.options.fetchBeforeQueued) { + const searchQueryString = this.options.disableArtistSearch + ? data.title + : `${data.title}${' - ' + data.author}`; + const ytv = await YouTube.search(searchQueryString, { limit: 1, type: "video" }).catch(e => { }); + + if (ytv && ytv[0]) Util.define({ + target: data, + prop: 'backupLink', + value: ytv[0].url + }); + } + return data; - }); + })); else { - tracks = playlist.tracks.items.map((m: any) => { + tracks = await Promise.all(playlist.tracks.items.map(async (m: any) => { const data = new Track(this, { title: m.track.name ?? '', description: m.track.description ?? '', @@ -255,8 +282,21 @@ export class Player extends EventEmitter { source: 'spotify' }); + if (this.options.fetchBeforeQueued) { + const searchQueryString = this.options.disableArtistSearch + ? data.title + : `${data.title}${' - ' + data.author}`; + const ytv = await YouTube.search(searchQueryString, { limit: 1, type: "video" }).catch(e => {}); + + if (ytv && ytv[0]) Util.define({ + target: data, + prop: 'backupLink', + value: ytv[0].url + }); + } + return data; - }); + })); } if (!tracks.length) return void this.emit(PlayerEvents.NO_RESULTS, message, query); @@ -1285,11 +1325,7 @@ export class Player extends EventEmitter { const searchQueryString = this.options.disableArtistSearch ? queue.playing.title : `${queue.playing.title}${' - ' + queue.playing.author}`; - const yteqv = await Util.ytSearch(searchQueryString, { - player: this, - limit: 1, - user: queue.playing.requestedBy - }).catch(() => {}); + const yteqv = await YouTube.search(searchQueryString, { type: "video", limit: 1 }).catch(() => {}); if (!yteqv || !yteqv.length) return void this.emit( @@ -1300,11 +1336,10 @@ export class Player extends EventEmitter { new PlayerError('Could not find alternative track on youtube!', 'SpotifyTrackError') ); - Object.defineProperty(queue.playing, 'backupLink', { - value: yteqv[0].url, - writable: true, - enumerable: false, - configurable: true + Util.define({ + target: queue.playing, + prop: 'backupLink', + value: yteqv[0].url }); } @@ -1496,6 +1531,7 @@ export default Player; * @property {Boolean} [useSafeSearch=false] If it should use `safe search` method for youtube searches * @property {Boolean} [disableAutoRegister=false] If it should disable auto-registeration of `@discord-player/extractor` * @property {Boolean} [disableArtistSearch=false] If it should disable artist search for spotify + * @property {Boolean} [fetchBeforeQueued=false] If it should fetch all songs loaded from spotify before playing */ /** diff --git a/src/types/types.ts b/src/types/types.ts index 91e32e3..11d48b2 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -14,6 +14,7 @@ export interface PlayerOptions { useSafeSearch?: boolean; disableAutoRegister?: boolean; disableArtistSearch?: boolean; + fetchBeforeQueued?: boolean; } export type FiltersName = keyof QueueFilters; diff --git a/src/utils/Util.ts b/src/utils/Util.ts index 56e313f..68d74f4 100644 --- a/src/utils/Util.ts +++ b/src/utils/Util.ts @@ -204,6 +204,21 @@ export class Util { return null; } } + + /** + * + * @param target + * @param prop + * @param value + */ + static define(ops: { target: any, prop: any, value: any, enumerate?: boolean }) { + Object.defineProperty(ops.target, ops.prop, { + value: ops.value, + writable: true, + enumerable: Boolean(ops.enumerate), + configurable: true + }); + } } export default Util;