-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (39 loc) · 1.69 KB
/
index.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
// Import the necessary packages
const SpotifyWebApi = require('spotify-web-api-node');
const fs = require('fs');
// Create a new instance of the Spotify Web API with your client ID and client secret
const spotifyApi = new SpotifyWebApi({
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET
});
// Authenticate with the Spotify API and retrieve an access token
spotifyApi.clientCredentialsGrant()
.then(data => {
// console.log('The access token is ' + data.body['access_token']);
// Set the access token on the Spotify Web API object
spotifyApi.setAccessToken(data.body['access_token']);
// Get the playlist ID from the command line arguments
const playlistId = process.env.PLAYLIST_ID;
spotifyApi.getPlaylist(playlistId, { fields: 'tracks(total)' }).then((data) => {
const totalTracks = data.body.tracks.total;
// Print the track names by batches of 100
for (let i = 0; i < totalTracks; i += 100) {
spotifyApi.getPlaylistTracks(playlistId, { offset: i, limit: 100 }).then(data => {
const dataP = JSON.parse(JSON.stringify(data.body, null, 2));
const items = dataP.items; // get the array of items
items.forEach(item => {
const artist = item.track.artists[0].name;
const trackName = item.track.name;
console.log(`${artist} - ${trackName}`);
});
// console.log('Playlist data saved to playlist.json');
})
.catch(err => {
console.error('Error getting playlist data:', err);
});
}
});
})
.catch(error => {
console.error('Error authenticating with Spotify API:', error);
});