Skip to content

Commit

Permalink
Add support for playing Spotify URLs by searching same song on YouTube (
Browse files Browse the repository at this point in the history
#30)

Co-authored-by: sweep-ai[bot] <128439645+sweep-ai[bot]@users.noreply.github.com>
Co-authored-by: Lassi Säike <lassisaike11@gmail.com>
  • Loading branch information
sweep-ai[bot] and lasa01 authored Aug 12, 2023
1 parent 1972f8b commit 5781499
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 4 deletions.
52 changes: 52 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@
"ts-node": "^10.5.0",
"tslint": "^6.1.3",
"typedoc": "^0.22.11",
"typescript": "^4.5.5"
"typescript": "^4.5.5",
"spotify-uri": "^4.0.0",
"spotify-url-info": "^3.2.6"
}
}
}
33 changes: 32 additions & 1 deletion src_modules/player/commands/PlayCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import ytpl = require("@distube/ytpl");
import ytsr = require("@distube/ytsr");
import fetch from "node-fetch";
import { Readable } from "stream";
import * as spotifyUri from 'spotify-uri';
const { getPreview } = require('spotify-url-info')(fetch);

export class PlayCommand extends Command<[StringArgument<false>]> {
private player: PlayerModule;
Expand Down Expand Up @@ -90,6 +92,12 @@ export class PlayCommand extends Command<[StringArgument<false>]> {
return;
}
return [item];
} else if (this.isSpotifyUrl(query)) {
const item = await this.getQueueItemFromSpotifyUrl(query, context);
if (!item) {
return context.respond(musicNotFoundPhrase, { search: query });
}
return [item];
} else {
const item = await this.getQueueItemFromDirectUrl(query);
if (!item) {
Expand All @@ -99,6 +107,16 @@ export class PlayCommand extends Command<[StringArgument<false>]> {
}
}

private isSpotifyUrl(url: string): boolean {
try {
spotifyUri.parse(url);

return true;
} catch {
return false;
}
}

private async searchYoutube(query: string, context: ICommandContext): Promise<PlayerQueueItem | undefined> {
const respondPromise = context.respond(musicSearchingPhrase, { search: query });
let searchResult: ytsr.VideoResult;
Expand Down Expand Up @@ -192,6 +210,19 @@ export class PlayCommand extends Command<[StringArgument<false>]> {
return new PlayerQueueItem(itemDetails, ytdlResult);
}

private async getQueueItemFromSpotifyUrl(url: string, context: ICommandContext): Promise<PlayerQueueItem | undefined> {
try {
const parsed = spotifyUri.parse(url);
const openUrl = spotifyUri.formatOpenURL(parsed);

const data = await getPreview(openUrl);

return this.searchYoutube(`${data.artist} - ${data.title}`, context);
} catch {
return undefined;
}
}

private async getQueueItemFromDirectUrl(url: string): Promise<PlayerQueueItem | undefined> {
let itemDetails: IQueueItemDetails;
const urlObj = new URL(url);
Expand Down Expand Up @@ -226,4 +257,4 @@ export class PlayCommand extends Command<[StringArgument<false>]> {
};
return new PlayerQueueItem(itemDetails);
}
}
}
2 changes: 1 addition & 1 deletion src_modules/player/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// extcord module
// requires ffmpeg-static@^5.1.0 @distube/ytdl-core@^4.11.17 @distube/ytsr@^1.1.9 @distube/ytpl@^1.1.1
// requires ffmpeg-static@^5.1.0 @distube/ytdl-core@^4.11.17 @distube/ytsr@^1.1.9 @distube/ytpl@^1.1.1 spotify-uri@^4.0.0 spotify-url-info@^3.2.6

import {
AudioPlayerStatus, createAudioPlayer,
Expand Down

0 comments on commit 5781499

Please sign in to comment.