diff options
| author | Stefano <[email protected]> | 2024-11-04 13:31:01 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-11-04 13:31:01 +0100 |
| commit | 07bfd32cb33029c0cd8413656c636c56def2f892 (patch) | |
| tree | 27aa806c89d787d7cf28f04e6bc79586c95a8a1a /src/utils | |
| parent | 418a7eccf3ad666df2f87c892ebce74e1fd0e70e (diff) | |
| parent | 66e022489ff8caaf1d9dcdbd34c93fe702dfa024 (diff) | |
| download | muse-07bfd32cb33029c0cd8413656c636c56def2f892.tar.xz muse-07bfd32cb33029c0cd8413656c636c56def2f892.zip | |
Merge pull request #1092 from sofushn/master
feat: allow running without spotify
Diffstat (limited to 'src/utils')
| -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; }; |
