-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpotify.js
137 lines (131 loc) · 5.19 KB
/
Spotify.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const clientID = 'c76cb3963c544805bf457075575dc6f6';
// const redirectURI = 'http://localhost:3000/'; // use this for local development mode
const redirectURI = 'https://react-jammming-spotify.netlify.app/';
let accessToken;
const baseURL = 'https://api.spotify.com';
// Spotify utility module
const Spotify = {
getAccessToken() {
// return token if set
if (accessToken) {
console.log('returning access token...');
return accessToken;
}
// build URL and get auth key
const accessTokenFromURL = window.location.href.match(/access_token=([^&]*)/);
const tokenExpirationFromURL = window.location.href.match(/expires_in=([^&]*)/);
if (accessTokenFromURL && tokenExpirationFromURL) {
console.log('getting token from URL...');
accessToken = accessTokenFromURL[1];
const tokenExpiration = Number(tokenExpirationFromURL[1]);
// clear parameters, allows grab new token at expiration
window.setTimeout(() => accessToken = '', tokenExpiration * 1000);
window.history.pushState('Access Token', null, '/');
return accessToken;
} else {
console.log('building auth URL...');
const scope = 'playlist-modify-public';
let url = 'https://accounts.spotify.com/authorize';
url += '?response_type=token';
url += '&client_id=' + encodeURIComponent(clientID);
url += '&scope=' + encodeURIComponent(scope);
url += '&redirect_uri=' + encodeURIComponent(redirectURI);
// url += '&state=' + encodeURIComponent(state);
window.location = url; // redirects after this
}
},
// search for tracks
async search(term) {
Spotify.getAccessToken();
try {
let response = await fetch(`${baseURL}/v1/search?type=track&q=${term}
`, {
method: 'GET',
headers: {
'Authorization': 'Bearer ' + accessToken,
}
});
let data = await response.json();
if (!response.ok) {
throw new Error(`${data.error.status} ${data.error.message}`)
};
if (data.tracks.items.length > 0) {
return data.tracks.items.map((track) => {
return {
'id': track.id,
'name': track.name,
'artist': track.artists[0].name,
'album': track.album.name,
'uri': track.uri,
}
});
} else {
return [];
}
} catch (e) {
console.log(e);
}
},
// write custom playlist to Spotify account
async savePlaylist(playlistName, trackURIs) {
// check values are saved. if not, return
if (!(playlistName && trackURIs)) {
console.log('savePlaylist params false, early return');
return;
}
Spotify.getAccessToken();
let headers = {
'Authorization': 'Bearer ' + accessToken,
}
// request Spotify username, save the response id parameter to the user’s ID variable.
let userID;
try {
let response = await fetch(`${baseURL}/v1/me`, { method: 'GET', headers: headers });
let data = await response.json();
if (!response.ok) {
throw new Error(`Requesting username. ${data.error.status} ${data.error.message}`);
};
userID = data.id;
} catch (e) {
console.log(e);
}
// use the returned user ID to make a POST request that creates a new playlist in the user’s account and returns a playlist ID.
let playlistID;
try {
let response = await fetch(`${baseURL}/v1/users/${userID}/playlists`, {
method: 'POST',
headers: headers,
body: JSON.stringify({
name: playlistName
}),
});
let data = await response.json();
if (!response.ok) {
throw new Error(`Creating playlist. ${data.error.status} ${data.error.message}`);
};
playlistID = data.id;
console.log('playlistID', playlistID);
} catch (e) {
console.log(e);
}
// use the returned user ID to make a POST request that creates a new playlist in the user’s account and returns a playlist ID.
try {
let response = await fetch(`${baseURL}/v1/playlists/${playlistID}/tracks`, {
method: 'POST',
headers: headers,
body: JSON.stringify({
uris: trackURIs
}),
});
let data = await response.json();
if (!response.ok) {
throw new Error(`Adding playlist tracks. ${data.error.status} ${data.error.message}`);
};
playlistID = data.snapshot_id;
console.log('snapshot_id', playlistID);
} catch (e) {
console.log(e);
}
}
}
export default Spotify;