diff options
| author | Stefano <[email protected]> | 2024-11-04 14:51:25 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-11-04 14:51:25 +0100 |
| commit | 250c7d316bd951d279262daa121ce72bf36484b0 (patch) | |
| tree | d673350998a6aa4a1078984cb3c7c62657735189 /src/services | |
| parent | b2565a3a92f941028284fa014c622213db863bad (diff) | |
| parent | 07bfd32cb33029c0cd8413656c636c56def2f892 (diff) | |
| download | muse-250c7d316bd951d279262daa121ce72bf36484b0.tar.xz muse-250c7d316bd951d279262daa121ce72bf36484b0.zip | |
Merge pull request #1137 from museofficial/develop
Develop
Diffstat (limited to 'src/services')
| -rw-r--r-- | src/services/add-query-to-queue.ts | 70 | ||||
| -rw-r--r-- | src/services/config.ts | 4 | ||||
| -rw-r--r-- | src/services/get-songs.ts | 102 | ||||
| -rw-r--r-- | src/services/youtube-api.ts | 2 |
4 files changed, 98 insertions, 80 deletions
diff --git a/src/services/add-query-to-queue.ts b/src/services/add-query-to-queue.ts index 401ad90..95e16ff 100644 --- a/src/services/add-query-to-queue.ts +++ b/src/services/add-query-to-queue.ts @@ -1,6 +1,5 @@ /* eslint-disable complexity */ import {ChatInputCommandInteraction, GuildMember} from 'discord.js'; -import {URL} from 'node:url'; import {inject, injectable} from 'inversify'; import shuffle from 'array-shuffle'; import {TYPES} from '../types.js'; @@ -60,74 +59,7 @@ export default class AddQueryToQueue { await interaction.deferReply({ephemeral: queueAddResponseEphemeral}); - let newSongs: SongMetadata[] = []; - let extraMsg = ''; - - // Test if it's a complete URL - try { - const url = new URL(query); - - const YOUTUBE_HOSTS = [ - 'www.youtube.com', - 'youtu.be', - 'youtube.com', - 'music.youtube.com', - 'www.music.youtube.com', - ]; - - if (YOUTUBE_HOSTS.includes(url.host)) { - // YouTube source - if (url.searchParams.get('list')) { - // YouTube playlist - newSongs.push(...await this.getSongs.youtubePlaylist(url.searchParams.get('list')!, shouldSplitChapters)); - } else { - const songs = await this.getSongs.youtubeVideo(url.href, shouldSplitChapters); - - if (songs) { - newSongs.push(...songs); - } else { - throw new Error('that doesn\'t exist'); - } - } - } else if (url.protocol === 'spotify:' || url.host === 'open.spotify.com') { - const [convertedSongs, nSongsNotFound, totalSongs] = await this.getSongs.spotifySource(query, playlistLimit, shouldSplitChapters); - - if (totalSongs > playlistLimit) { - extraMsg = `a random sample of ${playlistLimit} songs was taken`; - } - - if (totalSongs > playlistLimit && nSongsNotFound !== 0) { - extraMsg += ' and '; - } - - if (nSongsNotFound !== 0) { - if (nSongsNotFound === 1) { - extraMsg += '1 song was not found'; - } else { - extraMsg += `${nSongsNotFound.toString()} songs were not found`; - } - } - - newSongs.push(...convertedSongs); - } else { - const song = await this.getSongs.httpLiveStream(query); - - if (song) { - newSongs.push(song); - } else { - throw new Error('that doesn\'t exist'); - } - } - } catch (_: unknown) { - // Not a URL, must search YouTube - const songs = await this.getSongs.youtubeVideoSearch(query, shouldSplitChapters); - - if (songs) { - newSongs.push(...songs); - } else { - throw new Error('that doesn\'t exist'); - } - } + let [newSongs, extraMsg] = await this.getSongs.getSongs(query, playlistLimit, shouldSplitChapters); if (newSongs.length === 0) { throw new Error('no songs found'); diff --git a/src/services/config.ts b/src/services/config.ts index 019df07..3941a3c 100644 --- a/src/services/config.ts +++ b/src/services/config.ts @@ -12,8 +12,8 @@ export const DATA_DIR = path.resolve(process.env.DATA_DIR ? process.env.DATA_DIR const CONFIG_MAP = { DISCORD_TOKEN: process.env.DISCORD_TOKEN, YOUTUBE_API_KEY: process.env.YOUTUBE_API_KEY, - SPOTIFY_CLIENT_ID: process.env.SPOTIFY_CLIENT_ID, - SPOTIFY_CLIENT_SECRET: process.env.SPOTIFY_CLIENT_SECRET, + SPOTIFY_CLIENT_ID: process.env.SPOTIFY_CLIENT_ID ?? '', + SPOTIFY_CLIENT_SECRET: process.env.SPOTIFY_CLIENT_SECRET ?? '', REGISTER_COMMANDS_ON_BOT: process.env.REGISTER_COMMANDS_ON_BOT === 'true', DATA_DIR, CACHE_DIR: path.join(DATA_DIR, 'cache'), diff --git a/src/services/get-songs.ts b/src/services/get-songs.ts index b957734..c48d87d 100644 --- a/src/services/get-songs.ts +++ b/src/services/get-songs.ts @@ -1,34 +1,120 @@ -import {inject, injectable} from 'inversify'; +import {inject, injectable, optional} from 'inversify'; import * as spotifyURI from 'spotify-uri'; import {SongMetadata, QueuedPlaylist, MediaSource} from './player.js'; import {TYPES} from '../types.js'; import ffmpeg from 'fluent-ffmpeg'; import YoutubeAPI from './youtube-api.js'; import SpotifyAPI, {SpotifyTrack} from './spotify-api.js'; +import {URL} from 'node:url'; @injectable() export default class { private readonly youtubeAPI: YoutubeAPI; - private readonly spotifyAPI: SpotifyAPI; + private readonly spotifyAPI?: SpotifyAPI; - constructor(@inject(TYPES.Services.YoutubeAPI) youtubeAPI: YoutubeAPI, @inject(TYPES.Services.SpotifyAPI) spotifyAPI: SpotifyAPI) { + constructor(@inject(TYPES.Services.YoutubeAPI) youtubeAPI: YoutubeAPI, @inject(TYPES.Services.SpotifyAPI) @optional() spotifyAPI?: SpotifyAPI) { this.youtubeAPI = youtubeAPI; this.spotifyAPI = spotifyAPI; } - async youtubeVideoSearch(query: string, shouldSplitChapters: boolean): Promise<SongMetadata[]> { + async getSongs(query: string, playlistLimit: number, shouldSplitChapters: boolean): Promise<[SongMetadata[], string]> { + const newSongs: SongMetadata[] = []; + let extraMsg = ''; + + // Test if it's a complete URL + try { + const url = new URL(query); + + const YOUTUBE_HOSTS = [ + 'www.youtube.com', + 'youtu.be', + 'youtube.com', + 'music.youtube.com', + 'www.music.youtube.com', + ]; + + if (YOUTUBE_HOSTS.includes(url.host)) { + // YouTube source + if (url.searchParams.get('list')) { + // YouTube playlist + newSongs.push(...await this.youtubePlaylist(url.searchParams.get('list')!, shouldSplitChapters)); + } else { + const songs = await this.youtubeVideo(url.href, shouldSplitChapters); + + if (songs) { + newSongs.push(...songs); + } else { + throw new Error('that doesn\'t exist'); + } + } + } else if (url.protocol === 'spotify:' || url.host === 'open.spotify.com') { + if (this.spotifyAPI === undefined) { + throw new Error('Spotify is not enabled!'); + } + + const [convertedSongs, nSongsNotFound, totalSongs] = await this.spotifySource(query, playlistLimit, shouldSplitChapters); + + if (totalSongs > playlistLimit) { + extraMsg = `a random sample of ${playlistLimit} songs was taken`; + } + + if (totalSongs > playlistLimit && nSongsNotFound !== 0) { + extraMsg += ' and '; + } + + if (nSongsNotFound !== 0) { + if (nSongsNotFound === 1) { + extraMsg += '1 song was not found'; + } else { + extraMsg += `${nSongsNotFound.toString()} songs were not found`; + } + } + + newSongs.push(...convertedSongs); + } else { + const song = await this.httpLiveStream(query); + + if (song) { + newSongs.push(song); + } else { + throw new Error('that doesn\'t exist'); + } + } + } catch (err: any) { + if (err instanceof Error && err.message === 'Spotify is not enabled!') { + throw err; + } + + // Not a URL, must search YouTube + const songs = await this.youtubeVideoSearch(query, shouldSplitChapters); + + if (songs) { + newSongs.push(...songs); + } else { + throw new Error('that doesn\'t exist'); + } + } + + return [newSongs, extraMsg]; + } + + private async youtubeVideoSearch(query: string, shouldSplitChapters: boolean): Promise<SongMetadata[]> { return this.youtubeAPI.search(query, shouldSplitChapters); } - async youtubeVideo(url: string, shouldSplitChapters: boolean): Promise<SongMetadata[]> { + private async youtubeVideo(url: string, shouldSplitChapters: boolean): Promise<SongMetadata[]> { return this.youtubeAPI.getVideo(url, shouldSplitChapters); } - async youtubePlaylist(listId: string, shouldSplitChapters: boolean): Promise<SongMetadata[]> { + private async youtubePlaylist(listId: string, shouldSplitChapters: boolean): Promise<SongMetadata[]> { return this.youtubeAPI.getPlaylist(listId, shouldSplitChapters); } - async spotifySource(url: string, playlistLimit: number, shouldSplitChapters: boolean): Promise<[SongMetadata[], number, number]> { + private async spotifySource(url: string, playlistLimit: number, shouldSplitChapters: boolean): Promise<[SongMetadata[], number, number]> { + if (this.spotifyAPI === undefined) { + return [[], 0, 0]; + } + const parsed = spotifyURI.parse(url); switch (parsed.type) { @@ -58,7 +144,7 @@ export default class { } } - async httpLiveStream(url: string): Promise<SongMetadata> { + private async httpLiveStream(url: string): Promise<SongMetadata> { return new Promise((resolve, reject) => { ffmpeg(url).ffprobe((err, _) => { if (err) { diff --git a/src/services/youtube-api.ts b/src/services/youtube-api.ts index 143033a..216a2c0 100644 --- a/src/services/youtube-api.ts +++ b/src/services/youtube-api.ts @@ -95,7 +95,7 @@ export default class { } if (!firstVideo) { - throw new Error('No video found.'); + return []; } return this.getVideo(firstVideo.url, shouldSplitChapters); |
