diff options
| author | Max Isom <[email protected]> | 2022-01-21 19:57:51 -0600 |
|---|---|---|
| committer | Max Isom <[email protected]> | 2022-01-21 19:57:51 -0600 |
| commit | 09665af53ee1b1903fc9ea719722aa5dfdc26325 (patch) | |
| tree | 736b0a3ee648e54e6e518fbca150a4cd27ee226b /src/utils | |
| parent | 7901fcce3d7d0ce2ec91ba36a3ba9107e8709bff (diff) | |
| download | muse-09665af53ee1b1903fc9ea719722aa5dfdc26325.tar.xz muse-09665af53ee1b1903fc9ea719722aa5dfdc26325.zip | |
Refine autocomplete
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/constants.ts | 2 | ||||
| -rw-r--r-- | src/utils/get-youtube-and-spotify-suggestions-for.ts | 70 |
2 files changed, 72 insertions, 0 deletions
diff --git a/src/utils/constants.ts b/src/utils/constants.ts new file mode 100644 index 0000000..70d9e2c --- /dev/null +++ b/src/utils/constants.ts @@ -0,0 +1,2 @@ +export const ONE_HOUR_IN_SECONDS = 60 * 60; +export const ONE_MINUTE_IN_SECONDS = 1 * 60; diff --git a/src/utils/get-youtube-and-spotify-suggestions-for.ts b/src/utils/get-youtube-and-spotify-suggestions-for.ts new file mode 100644 index 0000000..10f9394 --- /dev/null +++ b/src/utils/get-youtube-and-spotify-suggestions-for.ts @@ -0,0 +1,70 @@ +import {ApplicationCommandOptionChoice} from 'discord.js'; +import SpotifyWebApi from 'spotify-web-api-node'; +import getYouTubeSuggestionsFor from './get-youtube-suggestions-for.js'; + +const filterDuplicates = <T extends {name: string}>(items: T[]) => { + const results: T[] = []; + + for (const item of items) { + if (!results.some(result => result.name === item.name)) { + results.push(item); + } + } + + return results; +}; + +const getYouTubeAndSpotifySuggestionsFor = async (query: string, spotify: SpotifyWebApi, limit = 10): Promise<ApplicationCommandOptionChoice[]> => { + const [youtubeSuggestions, spotifyResults] = await Promise.all([ + getYouTubeSuggestionsFor(query), + spotify.search(query, ['track', 'album'], {limit: 5}), + ]); + + const totalYouTubeResults = youtubeSuggestions.length; + + const spotifyAlbums = filterDuplicates(spotifyResults.body.albums?.items ?? []); + const spotifyTracks = filterDuplicates(spotifyResults.body.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 maxYouTubeSuggestions = limit - numOfSpotifySuggestions; + const numOfYouTubeSuggestions = Math.min(maxYouTubeSuggestions, totalYouTubeResults); + + const suggestions: ApplicationCommandOptionChoice[] = []; + + suggestions.push( + ...youtubeSuggestions + .slice(0, numOfYouTubeSuggestions) + .map(suggestion => ({ + name: `YouTube: ${suggestion}`, + value: suggestion, + }), + )); + + 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}`, + })), + ); + + return suggestions; +}; + +export default getYouTubeAndSpotifySuggestionsFor; |
