Skip to content

Instantly share code, notes, and snippets.

@PaulVanSchayck
Created September 23, 2018 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PaulVanSchayck/0da6647780703b9c7be7e06b97cd8b04 to your computer and use it in GitHub Desktop.
Save PaulVanSchayck/0da6647780703b9c7be7e06b97cd8b04 to your computer and use it in GitHub Desktop.
const code = 'your-refresh-token';
var SpotifyWebApi = require('spotify-web-api-node');
var spotifyApi = new SpotifyWebApi({
clientId : 'a52cd8838b26488186753ad0047bf86a',
clientSecret : '18c98110c7a84543afd65ce73186cdae',
redirectUri: 'http://localhost:8080'
});
// Uncomment to retrieve an access token and a refresh token
/*spotifyApi.setRefreshToken(code);
spotifyApi.refreshAccessToken().then(
function(data) {
console.log('The token expires in ' + data.body['expires_in']);
console.log('The access token is ' + data.body['access_token']);
// Set the access token on the API object to use it in later calls
spotifyApi.setAccessToken(data.body['access_token']);
},
function(err) {
console.log('Something went wrong!', err);
}
);
*/
spotifyApi.setAccessToken('your-retrieved-access-token');
function getTracks(offset) {
return spotifyApi.getPlaylistTracks('1DTzz7Nh2rJBnyFbjsH1Mh', {
offset: offset,
limit: 100,
fields: 'items'
}).then( function(data) {
// console.log(offset);
return data.body.items;
});
}
function getAllTracks(arr, max) {
return getTracks(arr.length).then(function(results) {
if (arr.length == max) {
return arr;
} else {
return getAllTracks(arr.concat(results), max);
}
});
}
spotifyApi.getPlaylist('1DTzz7Nh2rJBnyFbjsH1Mh')
.then(
function(data) {
// console.log('The playlist contains so many tracks:', data.body.tracks.total);
return data.body.tracks.total
},
function(err) {
console.log('Could not retrieve playlist!', err);
}
)
.then(function(length) {
getAllTracks([], length)
.then(function(tracks) {
var items = []
for( let track of tracks ) {
items.push({
service: 'spop',
title: track.track.name,
artist: track.track.artists[0].name,
album: track.track.album.name,
uri: track.track.uri,
albumart: (track.track.album.hasOwnProperty('images') && track.track.album.images.length > 0 ? track.track.album.images[0].url : ''),
});
}
console.log(JSON.stringify(items));
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment