-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
474 lines (425 loc) · 16.2 KB
/
index.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
require('dotenv').config();
const express = require("express");
const axios = require('axios');
const cors = require("cors");
const crypto = require('crypto');
const { connect } = require('http2');
const fetch = require('cross-fetch');
const cookieParser = require('cookie-parser'); // Storing in a cookie
const app = express();
app.use(express.json());
app.use(cors())
app.use(cookieParser());
CLIENT_ID = process.env.CLIENT_ID;
CLIENT_SECRET = process.env.CLIENT_SECRET;
PORT = process.env.PORT;
REDIRECT_URI = process.env.REDIRECT_URI;
SCOPE = [
"playlist-read-collaborative",
"playlist-read-private",
"playlist-modify-private",
"playlist-modify-public",
"user-library-read",
"user-modify-playback-state"
]
let verifier = null;
let songSet = new Set(); // Set to hold all songs
let songArrayFromSet = null; // Array to send into playlist 100 at a time
app.use(express.static(__dirname + '/public'))
.use(cors())
app.get("/", (request, response) => {
response.send("Welcome to my application!"); // or redirect to another route if desired
});
//PKCE Authorization Code
//Code verifier generation
function generateCodeVerifier(length) {
let text = '';
let possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (let i = 0; i < length; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
//Generate code challenge
async function generateCodeChallenge(codeVerifier) {
const sha256Digest = crypto.createHash('sha256').update(codeVerifier).digest();
const base64Digest = Buffer.from(sha256Digest).toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/, '');
return base64Digest;
}
// Redirects to ask user if the app can give access
app.get("/login", async (request, response) => {
verifier = generateCodeVerifier(128);
const challenge = await generateCodeChallenge(verifier);
const redirect_url = `https://accounts.spotify.com/authorize?response_type=code&client_id=${CLIENT_ID}&scope=${SCOPE}&state=123456&redirect_uri=${REDIRECT_URI}&prompt=consent&code_challenge_method=S256&code_challenge=${challenge}`
response.redirect(redirect_url);
})
// Returns to callback route with verifier and access token
app.get("/callback", async (request, response) => {
const code = request.query["code"]
let resp1 = await axios.post(
url = 'https://accounts.spotify.com/api/token',
data = new URLSearchParams({
'grant_type': 'authorization_code',
'redirect_uri': REDIRECT_URI,
'code': code,
'code_verifier': verifier
}),
config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
params: {
'grant_type': 'client_credentials'
},
auth: {
username: CLIENT_ID,
password: CLIENT_SECRET
}
})
// accessToken = resp1.data.access_token;
response.cookie('access_token', resp1.data.access_token, {
httpOnly: true,
secure: false,
sameSite: 'strict', // Adjust as needed
maxAge: 3600000, // Cookie expiration time in milliseconds (e.g., 1 hour)
});
return response.redirect("home.html");
})
// Clear the access token and any session data
app.get("/logout", (request, response) => {
response.clearCookie("access_token");
response.redirect("/");
});
app.get('/fetchProfile', async (req, res) => {
try {
const accessToken = req.cookies.access_token;
const response = await fetch("https://api.spotify.com/v1/me", {
method: "GET",
headers: { Authorization: `Bearer ${accessToken}` }
})
const profile = await response.json();
res.send(profile);
} catch (error) {
throw new Error("Error fetching profile: " + error);
}
})
// function on frontend retrieves liked songs and sends them over here. This adds the songs to the set.
app.get('/addLikedSongs', async (req, res) => {
try {
const accessToken = req.cookies.access_token;
const limit = 50;
let offset = 0;
let allLiked = [];
let songs = null;
let i = 0;
do {
const result = await fetch(`https://api.spotify.com/v1/me/tracks?limit=${limit}&offset=${offset}`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${accessToken}`
}
});
const data = await result.json();
songs = data.items.map(ob => ob.track).map(ob => ob.uri);
let print = data.items.map(ob => ob.track).map(ob => ob.name)[i];
allLiked = allLiked.concat(songs);
offset += limit;
} while (songs.length > 0);
for (song of allLiked) {
songSet.add(song);
}
res.sendStatus(200); // Sending a success response
} catch (error) {
console.error('Error adding liked songs:', error);
res.sendStatus(500); // Sending an error response
}
});
app.get('/fetchPlaylists', async (req, res) => {
try {
const accessToken = req.cookies.access_token;
const limit = 50;
let offset = 0;
let allplaylists = [];
let playlists = null;
do {
const result = await fetch(`https://api.spotify.com/v1/me/playlists?limit=${limit}&offset=${offset}`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${accessToken}`
}
});
const data = await result.json();
playlists = data.items;
allplaylists = allplaylists.concat(playlists);
offset += limit;
} while (playlists.length > 0);
const playlist = allplaylists.find(item => item.name === "all_songs");
const playlistId = playlist ? playlist.id : 0;
all_songs = playlistId;
res.send(JSON.stringify(playlistId));
} catch (error) {
console.error("Error while searching through playlists", error);
}
});
let shufflePlaylist = null;
app.post('/findPlaylistsToShuffle', async (req, res) => {
try {
const { toFind } = req.body;
// Sometimes a user has created a playlist name with a space at the end. Here is where
// we check for those cases.
const trailingSpace = toFind + " ";
const startSpace = " " + toFind;
const accessToken = req.cookies.access_token;
const limit = 50;
let offset = 0;
let allplaylists = [];
let playlists = null;
do {
const result = await fetch(`https://api.spotify.com/v1/me/playlists?limit=${limit}&offset=${offset}`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${accessToken}`
}
});
const data = await result.json();
playlists = data.items;
allplaylists = allplaylists.concat(playlists);
offset += limit;
} while (playlists.length > 0);
let playlist = allplaylists.find(item => item.name === toFind);
let playlistId = playlist ? playlist.id : 0;
if (playlistId == 0) { // Checking for trailing white space on user's end
playlist = allplaylists.find(item => item.name === trailingSpace);
playlistId = playlist ? playlist.id : 0;
}
if (playlistId == 0) { // Checking for beginning white space on user's end
playlist = allplaylists.find(item => item.name === startSpace);
playlistId = playlist ? playlist.id : 0;
}
if (playlistId != 0) {
shufflePlaylist = playlistId;
}
res.send(JSON.stringify(playlistId));
} catch (error) {
console.error("Error while fetching playlists for shuffling", error)
}
})
// Gets size of playlist
app.post('/getSizeOfPlaylist', async (req, res) => {
try {
const { num } = req.body;
if (shufflePlaylist != null) {
const accessToken = req.cookies.access_token;
const limit = 50;
let offset = 0;
let trackList = [];
let track = null;
do {
const result = await fetch(`https://api.spotify.com/v1/playlists/${shufflePlaylist}/tracks?limit=${limit}&offset=${offset}`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json',
}
});
const data = await result.json();
track = data.items.map(ob => ob.track.uri);
trackList = trackList.concat(track);
offset += limit;
} while (track.length > 0);
await randomizePlaylist(trackList, accessToken, num);
res.sendStatus(200);
}
} catch (error) {
console.error("Error retrieving size of playlist", error);
}
})
// Fisher-Yates algorithm to randomize array
async function randomizePlaylist(array, token, num) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
try {
let max = 0;
if (num > array.length) {
max = array.length;
} else {
max = num;
}
for (const uri of array.slice(0, max)) {
let result = await fetch(`https://api.spotify.com/v1/me/player/queue?uri=${uri}`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
}
});
console.log(result);
if (result.status === 404) {
console.log(`Track not found in Spotify catalog: ${uri}`);
} else if (result.ok) {
console.log(`Successfully added ${uri} to queue`);
} else {
console.log(`Error adding ${uri} to queue. Status: ${result.status}`);
}
}
} catch (error) {
console.error("Error adding to queue", error);
}
}
let all_songs = null;
app.get('/removePlaylist', async (req, res) => {
try {
const accessToken = req.cookies.access_token;
const unfollow = await fetch(`https://api.spotify.com/v1/playlists/${all_songs}/followers`, {
method: 'DELETE',
headers: {
'Authorization': `Bearer ${accessToken}`
}
});
console.log("Promise fulfilled");
res.sendStatus(200);
} catch (error) {
console.log("Error getting songs from all-songs", error);
}
})
// Fetches album list. For each batch of 50 albums, sends to function getTracksForEach.
// This function adds the songs to a set.
app.post('/fetchAlbumList', async (req, res) => {
const limit = 50;
let offset = 0;
let allAlbums = []; // array of album ids
console.log("Fetching album list...");
try {
const accessToken = req.cookies.access_token;
let albums = null;
do {
const result = await fetch(`https://api.spotify.com/v1/me/albums?limit=${limit}&offset=${offset}`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json',
}
});
const data = await result.json();
await getTracksForEach(data, req); // Batch of tracks
albums = data.items.map(ob => ob.album.id);
allAlbums = allAlbums.concat(albums);
offset += limit;
} while (albums.length > 0);
console.log("All songs added to set.");
songArrayFromSet = Array.from(songSet); // Converts song set to array when all is said and done
res.sendStatus(200);
} catch (error) {
console.error("Error fetching albums:", error);
throw error; // Rethrow the error to be caught at the higher level
}
});
// Helper function for /fetchAlbumList route. Appends tracks for each batch of 50 albums to set.
async function getTracksForEach(data, req) {
try {
console.log("In one batch.");
let trackBatchTotalTracks = data.items.map(ob => ob.album).map(ob => ob.total_tracks); // Data is an array of 50 album infos.
let trackBatchSongUri = data.items.map(ob => ob.album).map(ob => ob.tracks).map(ob => ob.items).flat().map(track => track.uri) // array of albums
for (let i = 0; i < trackBatchTotalTracks.length; i++) {
if (trackBatchTotalTracks[i] >= 50) {
// Pass ID to another function, handle with pagination
await handlePagination(data.items[i].album.id, req);
console.log("pagination handled.")
} else {
// Add tracks directly to set
for (song of trackBatchSongUri) {
songSet.add(song);
}
}
}
console.log("All songs compiled.")
} catch (error) {
console.error("Error getting tracks for each.", error)
}
}
// If there are >50 tracks in an album, this function deals with that.
async function handlePagination(albumId, req) {
const accessToken = req.cookies.access_token;
const limit = 50;
let offset = 0;
let trackList = [];
console.log("Handling pagination...");
let tracks = null;
do {
const result = await fetch(`https://api.spotify.com/v1/albums/${albumId}/tracks?limit=${limit}&offset=${offset}`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json',
}
});
const data = await result.json();
tracks = data.items.map(ob => ob.uri);
trackList = trackList.concat(tracks);
offset += limit;
} while (tracks.length > 0)
for (song of trackList) {
songSet.add(song);
}
}
app.post('/addSongsToPlaylist', async (req, res) => {
const { profileid } = req.body;
try {
const accessToken = req.cookies.access_token;
const response = await fetch(`https://api.spotify.com/v1/users/${profileid}/playlists`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
name: 'all_songs',
description: 'Includes all liked songs, albums, & playlists created by you. App by @artbylarapalombi on Instagram.'
})
})
const data = await response.json();
let playlistid = data.id;
await batchSongs(playlistid, req);
res.sendStatus(200);
} catch (error) {
throw new Error("Error creating playlist: " + error);
}
})
// Sends songs to playlist in batches.
async function batchSongs(playlistid, req) {
let limit = 100;
let offset = 0;
const songArrayCopy = songArrayFromSet.slice();
console.log("Length: " + songArrayFromSet.length);
let batch = [];
console.log("Total songs to add: " + songArrayFromSet.length);
while (offset < songArrayFromSet.length) {
const batch = songArrayFromSet.slice(offset, offset + limit);
if (batch.length > 0) {
await sendSongsToPlaylist(batch, playlistid, req);
console.log("Batch added: " + batch.length);
}
offset += limit;
}
}
async function sendSongsToPlaylist(songarr, playlistid, req) {
console.log("Error here? ")
const accessToken = req.cookies.access_token;
const result = await fetch(`https://api.spotify.com/v1/playlists/${playlistid}/tracks`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ "uris": songarr })
});
const data = await result.json();
console.log(data); // Log the response data to check for any errors
}
console.log('Listening on 5173');
app.listen(5173);