forked from thelinmichael/spotify-web-api-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-top-tracks-for-artist.js
56 lines (51 loc) · 1.81 KB
/
get-top-tracks-for-artist.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const SpotifyWebApi = require('../');
/**
* This example retrieves the top tracks for an artist.
* https://developer.spotify.com/documentation/web-api/reference/artists/get-artists-top-tracks/
*/
/**
* This endpoint doesn't require an access token, but it's beneficial to use one as it
* gives the application a higher rate limit.
*
* Since it's not necessary to get an access token connected to a specific user, this example
* uses the Client Credentials flow. This flow uses only the client ID and the client secret.
* https://developer.spotify.com/documentation/general/guides/authorization-guide/#client-credentials-flow
*/
const spotifyApi = new SpotifyWebApi({
clientId: '<insert client id>',
clientSecret: '<insert client secret>'
});
// Retrieve an access token
spotifyApi
.clientCredentialsGrant()
.then(function(data) {
// Set the access token on the API object so that it's used in all future requests
spotifyApi.setAccessToken(data.body['access_token']);
// Get the most popular tracks by David Bowie in Great Britain
return spotifyApi.getArtistTopTracks('0oSGxfWSnnOXhD2fKuz2Gy', 'GB');
})
.then(function(data) {
console.log('The most popular tracks for David Bowie is..');
console.log('Drum roll..');
console.log('...');
/*
* 1. Space Oddity - 2009 Digital Remaster (popularity is 51)
* 2. Heroes - 1999 Digital Remaster (popularity is 33)
* 3. Let's Dance - 1999 Digital Remaster (popularity is 20)
* 4. ...
*/
data.body.tracks.forEach(function(track, index) {
console.log(
index +
1 +
'. ' +
track.name +
' (popularity is ' +
track.popularity +
')'
);
});
})
.catch(function(err) {
console.log('Unfortunately, something has gone wrong.', err.message);
});