chore: make linter parse

This commit is contained in:
sofushn 2024-09-07 21:49:10 +02:00
parent af639159d1
commit 107464e222
2 changed files with 13 additions and 18 deletions

View file

@ -18,7 +18,7 @@ export default class {
} }
async getSongs(query: string, playlistLimit: number, shouldSplitChapters: boolean): Promise<[SongMetadata[], string]> { async getSongs(query: string, playlistLimit: number, shouldSplitChapters: boolean): Promise<[SongMetadata[], string]> {
let newSongs: SongMetadata[] = []; const newSongs: SongMetadata[] = [];
let extraMsg = ''; let extraMsg = '';
// Test if it's a complete URL // Test if it's a complete URL
@ -49,7 +49,7 @@ export default class {
} }
} else if (url.protocol === 'spotify:' || url.host === 'open.spotify.com') { } else if (url.protocol === 'spotify:' || url.host === 'open.spotify.com') {
if (this.spotifyAPI === undefined) { if (this.spotifyAPI === undefined) {
throw new Error('Spotify is not enabled!') throw new Error('Spotify is not enabled!');
} }
const [convertedSongs, nSongsNotFound, totalSongs] = await this.spotifySource(query, playlistLimit, shouldSplitChapters); const [convertedSongs, nSongsNotFound, totalSongs] = await this.spotifySource(query, playlistLimit, shouldSplitChapters);
@ -81,7 +81,7 @@ export default class {
} }
} }
} catch (err: any) { } catch (err: any) {
if (err = 'spotify not enabled') { if (err instanceof Error && err.message === 'Spotify is not enabled!') {
throw err; throw err;
} }
@ -112,7 +112,7 @@ export default class {
private 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) { if (this.spotifyAPI === undefined) {
return [[], 0, 0]; return [[], 0, 0];
} }
const parsed = spotifyURI.parse(url); const parsed = spotifyURI.parse(url);

View file

@ -15,12 +15,11 @@ const filterDuplicates = <T extends {name: string}>(items: T[]) => {
}; };
const getYouTubeAndSpotifySuggestionsFor = async (query: string, spotify?: SpotifyWebApi, limit = 10): Promise<APIApplicationCommandOptionChoice[]> => { const getYouTubeAndSpotifySuggestionsFor = async (query: string, spotify?: SpotifyWebApi, limit = 10): Promise<APIApplicationCommandOptionChoice[]> => {
// Only search Spotify if enabled // Only search Spotify if enabled
let spotifySuggestionPromise = spotify !== undefined const spotifySuggestionPromise = spotify === undefined
? spotify.search(query, ['album', 'track'], {limit}) ? undefined
: undefined; : spotify.search(query, ['album', 'track'], {limit});
const youtubeSuggestions = await getYouTubeSuggestionsFor(query); const youtubeSuggestions = await getYouTubeSuggestionsFor(query);
const totalYouTubeResults = youtubeSuggestions.length; const totalYouTubeResults = youtubeSuggestions.length;
@ -36,11 +35,9 @@ const getYouTubeAndSpotifySuggestionsFor = async (query: string, spotify?: Spoti
value: suggestion, value: suggestion,
}), }),
)); ));
if (spotify !== undefined && spotifySuggestionPromise !== undefined) { if (spotify !== undefined && spotifySuggestionPromise !== undefined) {
const spotifyResponse = (await spotifySuggestionPromise).body;
const spotifyResponse = (await spotifySuggestionPromise).body
const spotifyAlbums = filterDuplicates(spotifyResponse.albums?.items ?? []); const spotifyAlbums = filterDuplicates(spotifyResponse.albums?.items ?? []);
const spotifyTracks = filterDuplicates(spotifyResponse.tracks?.items ?? []); const spotifyTracks = filterDuplicates(spotifyResponse.tracks?.items ?? []);
@ -51,14 +48,13 @@ const getYouTubeAndSpotifySuggestionsFor = async (query: string, spotify?: Spoti
const maxSpotifySuggestions = Math.floor(limit / 2); const maxSpotifySuggestions = Math.floor(limit / 2);
const numOfSpotifySuggestions = Math.min(maxSpotifySuggestions, totalSpotifyResults); const numOfSpotifySuggestions = Math.min(maxSpotifySuggestions, totalSpotifyResults);
const maxSpotifyAlbums = Math.floor(numOfSpotifySuggestions / 2); const maxSpotifyAlbums = Math.floor(numOfSpotifySuggestions / 2);
const numOfSpotifyAlbums = Math.min(maxSpotifyAlbums, spotifyResponse.albums?.items.length ?? 0); const numOfSpotifyAlbums = Math.min(maxSpotifyAlbums, spotifyResponse.albums?.items.length ?? 0);
const maxSpotifyTracks = numOfSpotifySuggestions - numOfSpotifyAlbums; const maxSpotifyTracks = numOfSpotifySuggestions - numOfSpotifyAlbums;
// make room for spotify results // Make room for spotify results
const maxYouTubeSuggestions = limit - numOfSpotifySuggestions; const maxYouTubeSuggestions = limit - numOfSpotifySuggestions;
suggestions = suggestions.slice(0, maxYouTubeSuggestions) suggestions = suggestions.slice(0, maxYouTubeSuggestions);
suggestions.push( suggestions.push(
...spotifyAlbums.slice(0, maxSpotifyAlbums).map(album => ({ ...spotifyAlbums.slice(0, maxSpotifyAlbums).map(album => ({
@ -66,14 +62,13 @@ const getYouTubeAndSpotifySuggestionsFor = async (query: string, spotify?: Spoti
value: `spotify:album:${album.id}`, value: `spotify:album:${album.id}`,
})), })),
); );
suggestions.push( suggestions.push(
...spotifyTracks.slice(0, maxSpotifyTracks).map(track => ({ ...spotifyTracks.slice(0, maxSpotifyTracks).map(track => ({
name: `Spotify: 🎵 ${track.name}${track.artists.length > 0 ? ` - ${track.artists[0].name}` : ''}`, name: `Spotify: 🎵 ${track.name}${track.artists.length > 0 ? ` - ${track.artists[0].name}` : ''}`,
value: `spotify:track:${track.id}`, value: `spotify:track:${track.id}`,
})), })),
); );
} }
return suggestions; return suggestions;