From 78114ec36d34bda8157ea8be537a20495ae31cc3 Mon Sep 17 00:00:00 2001 From: Androz2091 Date: Fri, 14 Aug 2020 14:57:32 +0200 Subject: [PATCH] :sparkles: Add support for soundcloud --- package.json | 2 +- src/Player.js | 27 +++++++++++++++++---------- src/Util.js | 4 ++-- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index e61ab92..f6c18ba 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "merge-options": "^2.0.0", "moment": "^2.27.0", "node-fetch": "^2.6.0", - "soundcloud-scraper": "^1.0.1", + "soundcloud-scraper": "^1.0.2", "spotify-url-info": "^1.3.1", "ytpl": "^0.2.4", "ytsr": "^0.1.19" diff --git a/src/Player.js b/src/Player.js index 2f5756d..70a6193 100644 --- a/src/Player.js +++ b/src/Player.js @@ -3,6 +3,7 @@ const Discord = require('discord.js') const ytsr = require('ytsr') const ytpl = require('ytpl') const spotify = require('spotify-url-info') +const soundcloud = require('soundcloud-scraper') const moment = require('moment') const Queue = require('./Queue') const Track = require('./Track') @@ -120,6 +121,8 @@ class Player extends EventEmitter { return 'youtube-playlist' } else if (this.util.isYTVideoLink(query)) { return 'youtube-video' + } else if (this.util.isSoundcloudLink(query)) { + return 'soundcloud-song' } else { return 'youtube-video-keywords' } @@ -133,25 +136,29 @@ class Player extends EventEmitter { _searchTracks (message, query) { return new Promise(async (resolve) => { const tracks = [] + let updatedQuery = null - const queryType = this.resolveQueryType(query) + let queryType = this.resolveQueryType(query) if (queryType === 'spotify-song') { const matchSpotifyURL = query.match(/https?:\/\/(?:embed\.|open\.)(?:spotify\.com\/)(?:track\/|\?uri=spotify:track:)((\w|-){22})/) if (matchSpotifyURL) { const spotifyData = await spotify.getPreview(query).catch(() => {}) if (spotifyData) { - const YTQuery = `${spotifyData.artist} - ${spotifyData.track}` - const results = await ytsr(YTQuery) - - if (results.items.length !== 0) { - const resultsVideo = results.items.filter((i) => i.type === 'video') - tracks.push(...resultsVideo.map((r) => new Track(r, message.author, null))) - } + updatedQuery = `${spotifyData.artist} - ${spotifyData.track}` + queryType = 'youtube-video-keywords' } } - } else if (queryType === 'youtube-video-keywords') { - await ytsr(query).then((results) => { + } else if (queryType === 'soundcloud-song') { + const soundcloudData = await soundcloud.getSongInfo(query).catch(() => {}) + if (soundcloudData) { + updatedQuery = `${soundcloudData.author} - ${soundcloudData.title}` + queryType = 'youtube-video-keywords' + } + } + + if (queryType === 'youtube-video-keywords') { + await ytsr(updatedQuery || query).then((results) => { if (results.items.length !== 0) { const resultsVideo = results.items.filter((i) => i.type === 'video') tracks.push(...resultsVideo.map((r) => new Track(r, message.author, null))) diff --git a/src/Util.js b/src/Util.js index 04af5f1..c3c9bfa 100644 --- a/src/Util.js +++ b/src/Util.js @@ -1,5 +1,5 @@ const ytpl = require('ytpl') -const scraper = require('soundcloud-scraper') +const soundcloud = require('soundcloud-scraper') const Discord = require('discord.js') const youtubeVideoRegex = (/(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/\s]{11})/) @@ -15,7 +15,7 @@ module.exports = class Util { } static isSoundcloudLink (query) { - return scraper.validateURL(query) + return soundcloud.validateURL(query) } static isSpotifyLink (query) {