-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetPlaylistsTracks.js
38 lines (31 loc) · 1.33 KB
/
getPlaylistsTracks.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
const fs = require('fs')
const SpotifyWebApi = require('spotify-web-api-node');
const token = "BQDNU9WxrgL4mAduOcDTJedrMWCI7h5uT4esruuVOsupKu7GeVKwkWps9FPASA8XQfavM5PKvr1KX5WmP6WfPX5CHf13zi2hNAxqHHmFhrCQn9u8ol5FBePC_KdA96ixV5KKhqzJAdNllQRWBi618U5a4d96oyJPejFVhqFUpwsIXwZpX_jiWXivxUvZIjr2jTsRuoapsTYlTcIi6679hMdejbFgHEXRL-BaH9PumfRq67hfIvIkGZafY-uZK6Ug-QTUNF5w8FDBJXxoQ_CvppJElr0weKMwwtLHgcM_VkOo7LfhS3eq"
const spotifyApi = new SpotifyWebApi();
spotifyApi.setAccessToken(token);
// Get All playlists
async function getUserPlaylists(userName) {
const data = await spotifyApi.getUserPlaylists(userName)
for (let playlist of data.body.items) {
let tracks = await getPlaylistTracks(playlist.id, playlist.name);
const tracksJSON = { tracks };
let data = JSON.stringify(tracksJSON);
fs.writeFileSync(playlist.name+'.json', data);
}
}
//Get playlist tracks
async function getPlaylistTracks(playlistId, playlistName) {
const data = await spotifyApi.getPlaylistTracks(playlistId, {
offset: 1,
limit: 100,
fields: 'items'
});
let tracks = [];
for (let trackObj of data.body.items) {
const track = trackObj.track;
tracks.push(trackObj);
console.log(track.name + " : " + track.artists[0].name);
}
return tracks;
}
getUserPlaylists('tbaranowicz');