aboutsummaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorsofushn <[email protected]>2024-09-07 13:09:45 +0200
committersofushn <[email protected]>2024-09-07 13:09:45 +0200
commitaf639159d1647ffb7b4c5c5f7ad035fe8766b6dc (patch)
tree5cd5de1d9cc6618ba0b056ab3f005ea21ac4007d /src/utils
parent534d8fafaa7f09f7ba940d044b08e6c48f800c7a (diff)
downloadmuse-af639159d1647ffb7b4c5c5f7ad035fe8766b6dc.tar.xz
muse-af639159d1647ffb7b4c5c5f7ad035fe8766b6dc.zip
feat: allow running without spotify
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/get-youtube-and-spotify-suggestions-for.ts84
1 files changed, 48 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..c33cbee 100644
--- a/src/utils/get-youtube-and-spotify-suggestions-for.ts
+++ b/src/utils/get-youtube-and-spotify-suggestions-for.ts
@@ -14,28 +14,19 @@ 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
+ let spotifySuggestionPromise = spotify !== undefined
+ ? spotify.search(query, ['album', 'track'], {limit})
+ : undefined;
+
+ const youtubeSuggestions = await getYouTubeSuggestionsFor(query);
const totalYouTubeResults = youtubeSuggestions.length;
+ const numOfYouTubeSuggestions = Math.min(limit, totalYouTubeResults);
- 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: APIApplicationCommandOptionChoice[] = [];
+ let suggestions: APIApplicationCommandOptionChoice[] = [];
suggestions.push(
...youtubeSuggestions
@@ -45,24 +36,45 @@ const getYouTubeAndSpotifySuggestionsFor = async (query: string, spotify: Spotif
value: suggestion,
}),
));
+
+
+ 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}`,
+ })),
+ );
- 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;
};