diff options
| author | Bobby <[email protected]> | 2024-11-06 09:42:25 -0500 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-11-06 09:42:25 -0500 |
| commit | d92fd2a29796c9c5d6ddeb9718bf6fcd6ee5958a (patch) | |
| tree | 81961cc995661448794669617f1ed439cbe84a3d /src/utils/get-youtube-and-spotify-suggestions-for.ts | |
| parent | b605bf220859acd767533e0ab9436ced771bb8e2 (diff) | |
| parent | 716d6d9f4f2cd1a6872e463e9877203a259478a3 (diff) | |
| download | muse-master.tar.xz muse-master.zip | |
Diffstat (limited to 'src/utils/get-youtube-and-spotify-suggestions-for.ts')
| -rw-r--r-- | src/utils/get-youtube-and-spotify-suggestions-for.ts | 79 |
1 files changed, 43 insertions, 36 deletions
diff --git a/src/utils/get-youtube-and-spotify-suggestions-for.ts b/src/utils/get-youtube-and-spotify-suggestions-for.ts index 6594b52..bd6f1c8 100644 --- a/src/utils/get-youtube-and-spotify-suggestions-for.ts +++ b/src/utils/get-youtube-and-spotify-suggestions-for.ts @@ -14,28 +14,18 @@ const filterDuplicates = <T extends {name: string}>(items: T[]) => { return results; }; -const getYouTubeAndSpotifySuggestionsFor = async (query: string, spotify: SpotifyWebApi, limit = 10): Promise<APIApplicationCommandOptionChoice[]> => { - const [youtubeSuggestions, spotifyResults] = await Promise.all([ - getYouTubeSuggestionsFor(query), - spotify.search(query, ['track', 'album'], {limit: 5}), - ]); +const getYouTubeAndSpotifySuggestionsFor = async (query: string, spotify?: SpotifyWebApi, limit = 10): Promise<APIApplicationCommandOptionChoice[]> => { + // Only search Spotify if enabled + const spotifySuggestionPromise = spotify === undefined + ? undefined + : spotify.search(query, ['album', 'track'], {limit}); - const totalYouTubeResults = youtubeSuggestions.length; - - const spotifyAlbums = filterDuplicates(spotifyResults.body.albums?.items ?? []); - const spotifyTracks = filterDuplicates(spotifyResults.body.tracks?.items ?? []); - - const totalSpotifyResults = spotifyAlbums.length + spotifyTracks.length; + const youtubeSuggestions = await getYouTubeSuggestionsFor(query); - // Number of results for each source should be roughly the same. - // If we don't have enough Spotify suggestions, prioritize YouTube results. - const maxSpotifySuggestions = Math.floor(limit / 2); - const numOfSpotifySuggestions = Math.min(maxSpotifySuggestions, totalSpotifyResults); - - const maxYouTubeSuggestions = limit - numOfSpotifySuggestions; - const numOfYouTubeSuggestions = Math.min(maxYouTubeSuggestions, totalYouTubeResults); + const totalYouTubeResults = youtubeSuggestions.length; + const numOfYouTubeSuggestions = Math.min(limit, totalYouTubeResults); - const suggestions: APIApplicationCommandOptionChoice[] = []; + let suggestions: APIApplicationCommandOptionChoice[] = []; suggestions.push( ...youtubeSuggestions @@ -46,23 +36,40 @@ const getYouTubeAndSpotifySuggestionsFor = async (query: string, spotify: Spotif }), )); - const maxSpotifyAlbums = Math.floor(numOfSpotifySuggestions / 2); - const numOfSpotifyAlbums = Math.min(maxSpotifyAlbums, spotifyResults.body.albums?.items.length ?? 0); - const maxSpotifyTracks = numOfSpotifySuggestions - numOfSpotifyAlbums; - - suggestions.push( - ...spotifyAlbums.slice(0, maxSpotifyAlbums).map(album => ({ - name: `Spotify: 💿 ${album.name}${album.artists.length > 0 ? ` - ${album.artists[0].name}` : ''}`, - value: `spotify:album:${album.id}`, - })), - ); - - suggestions.push( - ...spotifyTracks.slice(0, maxSpotifyTracks).map(track => ({ - name: `Spotify: 🎵 ${track.name}${track.artists.length > 0 ? ` - ${track.artists[0].name}` : ''}`, - value: `spotify:track:${track.id}`, - })), - ); + if (spotify !== undefined && spotifySuggestionPromise !== undefined) { + const spotifyResponse = (await spotifySuggestionPromise).body; + const spotifyAlbums = filterDuplicates(spotifyResponse.albums?.items ?? []); + const spotifyTracks = filterDuplicates(spotifyResponse.tracks?.items ?? []); + + const totalSpotifyResults = spotifyAlbums.length + spotifyTracks.length; + + // Number of results for each source should be roughly the same. + // If we don't have enough Spotify suggestions, prioritize YouTube results. + const maxSpotifySuggestions = Math.floor(limit / 2); + const numOfSpotifySuggestions = Math.min(maxSpotifySuggestions, totalSpotifyResults); + + const maxSpotifyAlbums = Math.floor(numOfSpotifySuggestions / 2); + const numOfSpotifyAlbums = Math.min(maxSpotifyAlbums, spotifyResponse.albums?.items.length ?? 0); + const maxSpotifyTracks = numOfSpotifySuggestions - numOfSpotifyAlbums; + + // Make room for spotify results + const maxYouTubeSuggestions = limit - numOfSpotifySuggestions; + suggestions = suggestions.slice(0, maxYouTubeSuggestions); + + suggestions.push( + ...spotifyAlbums.slice(0, maxSpotifyAlbums).map(album => ({ + name: `Spotify: 💿 ${album.name}${album.artists.length > 0 ? ` - ${album.artists[0].name}` : ''}`, + value: `spotify:album:${album.id}`, + })), + ); + + suggestions.push( + ...spotifyTracks.slice(0, maxSpotifyTracks).map(track => ({ + name: `Spotify: 🎵 ${track.name}${track.artists.length > 0 ? ` - ${track.artists[0].name}` : ''}`, + value: `spotify:track:${track.id}`, + })), + ); + } return suggestions; }; |
