From 4ff6dcbd416ef485d6fe00bf2045e1b8d22c46bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?RedBee=E3=83=84?= Date: Thu, 18 Jul 2024 21:07:24 +0530 Subject: [PATCH] Add: Top tracks by artist ID or url --- src/Spotify.ts | 16 +++++++++++++++- src/lib/API.ts | 9 +++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Spotify.ts b/src/Spotify.ts index 63350e6..bf352a2 100644 --- a/src/Spotify.ts +++ b/src/Spotify.ts @@ -1,5 +1,5 @@ import { promises, unlink } from 'fs-extra' -import SpotifyApi, { IAuth, UserObjectPublic } from './lib/API' +import SpotifyApi, { IAuth, TopTrackDetails, UserObjectPublic } from './lib/API' import Artist from './lib/details/Atrist' import Playlist from './lib/details/Playlist' import SongDetails from './lib/details/Track' @@ -68,6 +68,20 @@ export default class SpotifyFetcher extends SpotifyApi { } } + /** + * Gets the list of Top Tracks from the given Artists URL + * @param url + * @returns {string[]} having the uri of the tracks + */ + getTopTracksArtist = async ( + url: string + ) => { + await this.verifyCredentials() + const artistResult = await this.getArtist(url) + const topTracks = (await this.getTopTracksByArtist(artistResult.id)) + return topTracks.body.tracks.map((track) => track.album.uri) + } + /** * Gets the playlist info from URL * @param url URL of the playlist diff --git a/src/lib/API.ts b/src/lib/API.ts index d3f4a6f..ca7619a 100644 --- a/src/lib/API.ts +++ b/src/lib/API.ts @@ -123,6 +123,10 @@ export default class SpotifyApi { await this.verifyCredentials() return (await this.spotifyAPI.getUser(id)) as UserObjectPublic } + getTopTracksByArtist = async(url:string) =>{ + await this.verifyCredentials() + return (await this.spotifyAPI.getArtistTopTracks(url,'IN')) as unknown as TopTrackDetails + } } export interface IAuth { @@ -130,6 +134,11 @@ export interface IAuth { clientSecret: string } +export interface TopTrackDetails { + body:{ + tracks: SpotifyApi.TrackObjectFull[] + } +} interface ClientCredentialsGrantResponseEX { access_token: string expires_in: number