-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaylist.js
58 lines (50 loc) · 1.79 KB
/
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
const SpotifyWebApi = require('spotify-web-api-node');
const spotifyApi = new SpotifyWebApi({
clientId: process.env.spotifyClientId,
clientSecret: process.env.spotifyClientSecret
});
const TRACK_FIELDS = 'total,next,items(track(name,artists(name),album(name),duration_ms))';
module.exports.getPlaylist = async event => {
if (!event.pathParameters || !event.pathParameters.id) {
return {
statusCode: 400,
body: 'You must provide a playlist ID',
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
}
}
}
const playlistId = event.pathParameters.id;
// Authorization.
const authorizationData = await spotifyApi.clientCredentialsGrant();
const { expires_in: expiry, access_token: accessToken } = authorizationData.body;
spotifyApi.setAccessToken(accessToken);
// Store tokens for future requests.
// TODO.
const playlist = await spotifyApi.getPlaylist(playlistId, {
fields: `id,name,description,images(url),owner(display_name),tracks(${TRACK_FIELDS})`
});
// Fetch all of the tracks in the playlist if specified.
if (event.queryStringParameters && event.queryStringParameters.fetchAllTracks) {
let next = playlist.body.tracks.next;
while (next) {
const [ _, offset ] = next.match(/tracks\?offset=([0-9]+)/);
const tracksResponse = await spotifyApi.getPlaylistTracks(playlistId, {
fields: TRACK_FIELDS,
offset,
size: 100
});
playlist.body.tracks.items = playlist.body.tracks.items.concat(tracksResponse.body.items);
next = tracksResponse.body.next;
}
}
return {
statusCode: 200,
body: JSON.stringify(playlist.body),
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
}
};
};