forked from thelinmichael/spotify-web-api-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-remove-replace-tracks-in-a-playlist.js
74 lines (60 loc) · 2.84 KB
/
add-remove-replace-tracks-in-a-playlist.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var SpotifyWebApi = require("../");
/**
* This example demonstrates adding tracks, removing tracks, and replacing tracks in a playlist. At this time of writing this
* documentation, this is the available playlist track modification features in the Spotify Web API.
*
* Since authorization is required, this example retrieves an access token using the Authorization Code Grant flow,
* documented here: https://developer.spotify.com/spotify-web-api/authorization-guide/#authorization_code_flow
*
* Codes are given for a set of scopes. For this example, the scopes are playlist-modify-public.
* Scopes are documented here:
* https://developer.spotify.com/spotify-web-api/using-scopes/
*/
/* This code is hardcoded. For a working implementation, the code needs to be retrieved from the user. See documentation about
* the Authorization Code flow for more information.
*/
var authorizationCode = '<insert authorization code with playlist-modify-public scope>';
/**
* Set the credentials given on Spotify's My Applications page.
* https://developer.spotify.com/my-applications
*/
var spotifyApi = new SpotifyWebApi({
clientId : '<insert client id>',
clientSecret : '<insert client secret>',
redirectUri : '<insert redirect URI>'
});
var playlistId;
// First retrieve an access token
spotifyApi.authorizationCodeGrant(authorizationCode)
.then(function(data) {
// Save the access token so that it's used in future requests
spotifyApi.setAccessToken(data['access_token']);
// Create a playlist
return spotifyApi.createPlaylist('thelinmichael', 'My New Awesome Playlist');
}).then(function(data) {
console.log('Ok. Playlist created!');
playlistId = data.body['id'];
// Add tracks to the playlist
return spotifyApi.addTracksToPlaylist('thelinmichael', playlistId, ["spotify:track:4iV5W9uYEdYUVa79Axb7Rh",
"spotify:track:6tcfwoGcDjxnSc6etAkDRR",
"spotify:track:4iV5W9uYEdYUVa79Axb7Rh"]);
}).then(function(data) {
console.log('Ok. Tracks added!');
// Woops! Made a duplicate. Remove one of the duplicates from the playlist
return spotifyApi.removeTracksFromPlaylist('thelinmichael', playlistId,
[{
'uri' : 'spotify:track:4iV5W9uYEdYUVa79Axb7Rh',
'positions' : [0]
}])
}).then(function(data) {
console.log('Ok. Tracks removed!');
// Actually, lets just replace all tracks in the playlist with something completely different
return spotifyApi.replaceTracksInPlaylist('thelinmichael', playlistId, ['spotify:track:5Wd2bfQ7wc6GgSa32OmQU3',
'spotify:track:4r8lRYnoOGdEi6YyI5OC1o', 'spotify:track:4TZZvblv2yzLIBk2JwJ6Un', 'spotify:track:2IA4WEsWAYpV9eKkwR2UYv',
'spotify:track:6hDH3YWFdcUNQjubYztIsG']);
}).then(function(data) {
console.log('Ok. Tracks replaced!');
}).catch(function(err) {
console.log(err.message);
console.log('Something went wrong!');
});