-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
446 lines (381 loc) · 12.2 KB
/
main.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
var playlistsData = [];
var metricType = "";
var progressBarPercent = 0;
var progressBarMessage = "";
var progressBarIncrementCounter = 0;
var playlistsCount = 0;
var promiseThrottle = new PromiseThrottle({
requestsPerSecond: 9,
promiseImplementation: Promise,
});
var wakeLock = null;
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function retryPromiseUntilSuccessful(promiseFunction) {
try {
let returnValue = await promiseFunction();
return returnValue;
} catch (e) {
console.warn("Sleeping for 5 seconds because of an error", e);
await sleep(5000);
return await retryPromiseUntilSuccessful(promiseFunction);
}
}
function timeAgo(time) {
var units = [
{ name: "second", limit: 60, in_seconds: 1 },
{ name: "minute", limit: 3600, in_seconds: 60 },
{ name: "hour", limit: 86400, in_seconds: 3600 },
{ name: "day", limit: 604800, in_seconds: 86400 },
{ name: "week", limit: 2629743, in_seconds: 604800 },
{ name: "month", limit: 31556926, in_seconds: 2629743 },
{ name: "year", limit: null, in_seconds: 31556926 },
];
var diff = (new Date() - new Date(time)) / 1000;
if (diff < 5) return "now";
var i = 0;
while ((unit = units[i++])) {
if (diff < unit.limit || !unit.limit) {
var diff = Math.floor(diff / unit.in_seconds);
return diff + " " + unit.name + (diff > 1 ? "s" : "");
}
}
}
async function getAllPages(request) {
const paginatedResponse = await request;
let currentResponse = paginatedResponse;
while (currentResponse.next) {
currentResponse = await promiseThrottle.add(function () {
return retryPromiseUntilSuccessful(function () {
return spotifyApi.getGeneric(currentResponse.next);
});
});
paginatedResponse.items = paginatedResponse.items.concat(
currentResponse.items
);
}
return paginatedResponse;
}
async function fetchSpotifyGuestAccessToken() {
return (
await (
await fetch("https://spotify-guest-token.herokuapp.com/get_access_token")
).json()
).accessToken;
}
async function getFollowedUsers(spotifyApi) {
const guestAccessToken = await fetchSpotifyGuestAccessToken();
const me = await spotifyApi.getMe();
return (
await (
await fetch(
`https://spclient.wg.spotify.com/user-profile-view/v3/profile/${me.id}/following`,
{
headers: {
accept: "application/json",
"accept-language": "en",
"app-platform": "WebPlayer",
authorization: `Bearer ${guestAccessToken}`,
"spotify-app-version": "1.1.54.282.g5e733e7e",
},
referrer: "https://open.spotify.com/",
referrerPolicy: "strict-origin-when-cross-origin",
body: null,
method: "GET",
mode: "cors",
}
)
).json()
).profiles.filter((x) => x.uri.startsWith("spotify:user"));
}
async function getFullPlaylists(followed) {
const userIds = getUserIds(followed);
playlistsCount = userIds.length;
const usersPlaylists = await Promise.all(userIds.map(getAllUserPlaylists));
const allPlaylists = usersPlaylists.flat().filter((x) => x.tracks.total > 3);
playlistsCount = allPlaylists.length;
updateProgressBar(40, "Loading followers...");
let augmentedPlaylists = await Promise.all(
allPlaylists.map(augmentPlaylistWithFollowers)
);
playlistsData = augmentedPlaylists;
renderPopular();
updateProgressBar(60, "Wondering why Spotify's API is so slow...");
await sleep(1000);
updateProgressBar(60, "Loading activity data...");
augmentedPlaylists = await Promise.all(
augmentedPlaylists.map(augmentPlaylistWithTracks)
);
return augmentedPlaylists;
}
function getUserIds(followed) {
return followed.map((user) => decodeURIComponent(user.uri.split(":")[2]));
}
async function getAllUserPlaylists(userId) {
const playlistsResponse = await getAllPages(
promiseThrottle.add(function () {
return retryPromiseUntilSuccessful(function () {
return spotifyApi.getUserPlaylists(userId, { limit: 50 });
});
})
);
const playlists = playlistsResponse.items;
const onlyUserPlaylists = playlists.filter((p) => p.owner.id === userId);
incrementProgressBar();
return onlyUserPlaylists;
}
async function augmentPlaylistWithFollowers(playlistObj) {
const { followers } = await promiseThrottle.add(function () {
return retryPromiseUntilSuccessful(function () {
return spotifyApi.getPlaylist(playlistObj.id);
});
});
incrementProgressBar();
return { ...playlistObj, followers: followers.total };
}
async function augmentPlaylistWithTracks(playlistObj) {
const tracks = await getAllPlaylistTracks(playlistObj.id);
const dates = getDates(tracks);
incrementProgressBar();
return { ...playlistObj, ...dates };
}
async function getAllPlaylistTracks(playlistId) {
const tracksResponse = await getAllPages(
promiseThrottle.add(function () {
return retryPromiseUntilSuccessful(function () {
return spotifyApi.getPlaylistTracks(playlistId);
});
})
);
return tracksResponse.items;
}
function getDates(tracks) {
const dates = tracks.map((t) => new Date(t.added_at).getTime());
const last_updated = Math.max(...dates);
const created_at = Math.min(...dates);
return { last_updated, created_at };
}
function processLoginRedirect() {
const fragmentString = window.location.hash.slice(1);
const fragmentParams = new URLSearchParams(fragmentString);
if (fragmentParams.get("access_token")) {
window.localStorage.setItem(
"spotifyUserAccessToken",
fragmentParams.get("access_token")
);
window.localStorage.setItem(
"spotifyUserAccessTokenExpiry",
Date.now() + parseInt(fragmentParams.get("expires_in")) * 1000
);
}
// Clear url fragment (#)
history.replaceState(null, null, " ");
}
function redirectToLogin() {
const redirectUrl = encodeURIComponent(window.location.href);
window.location.href = `https://accounts.spotify.com/authorize?client_id=8bf1d78c0a4e44aaa611730d9caf856c&response_type=token&redirect_uri=${redirectUrl}&scope=user-follow-read`;
}
function isLoggedIn() {
return (
window.localStorage.getItem("spotifyUserAccessToken") &&
parseInt(window.localStorage.getItem("spotifyUserAccessTokenExpiry")) >
Date.now()
);
}
async function fetchPlaylists() {
const key = "playlistsData";
let fetchedPlaylistsData = window.localStorage.getItem(key);
if (!fetchedPlaylistsData) {
const spotifyUserFollowedUsers = await getFollowedUsers(spotifyApi);
updateProgressBar(10, "Loading playlists...");
fetchedPlaylistsData = await getFullPlaylists(spotifyUserFollowedUsers);
window.localStorage.setItem(key, JSON.stringify(fetchedPlaylistsData));
} else {
fetchedPlaylistsData = JSON.parse(fetchedPlaylistsData);
}
hideProgressBar();
return fetchedPlaylistsData;
}
function getMetric(playlist) {}
function getPlaylistHTML(playlist) {
const image = playlist.images[playlist.images.length - 1];
imageUrl = image ? image.url : "/default.png";
return `<div role="row" aria-rowindex="1" aria-selected="false">
<div
data-testid="tracklist-row"
class="e8ea6a219247d88aa936a012f6227b0d-scss bddcb131e9b40fa874148a30368d83f8-scss"
draggable="true"
onclick="location='${playlist.uri}'"
>
<div
class="_5845794624a406a62eb5b71d3d1c4d63-scss"
role="gridcell"
aria-colindex="1"
tabindex="-1"
>
<div class="_9811afda86f707ead7da1d12f4dd2d3e-scss">
<img
aria-hidden="false"
draggable="false"
loading="eager"
src="${imageUrl}"
alt=""
class="_64acb0e26fe0d9dff68a0e9725b2a920-scss fc0bebbbc5e1404f464fb4d8c17001dc-scss"
width="40"
height="40"
/>
</div>
<div class="_8ea0b892e971e6b90a252247c160b4f4-scss">
<div
class="da0bc4060bb1bdb4abb8e402916af32e-scss standalone-ellipsis-one-line _8a9c5cc886805907de5073b8ebc3acd8-scss"
dir="auto"
as="div"
>
${playlist.name}
</div>
<span
class="_966e29b71d2654743538480947a479b3-scss standalone-ellipsis-one-line f3fc214b257ae2f1d43d4c594a94497f-scss"
as="span"
><a
draggable="true"
dir="auto"
href="${playlist.owner.uri}"
onclick="stopEventPropagation(event)"
tabindex="-1"
>By ${playlist.owner.display_name}</a
></span
>
</div>
</div>
<div
class="b9f411c6b990949776c8edf3daeb26ad-scss"
role="gridcell"
aria-colindex="2"
tabindex="-1"
>
<div class="ec1b5762556429ac3aeedbae72433491-scss">
${playlist.metric}
</div>
</div>
</div>
</div>`;
}
function stopEventPropagation(event) {
event.stopPropagation();
}
function updateProgressBar(
percent,
text,
progressBarIncrementCounterParam = 0
) {
progressBarPercent = percent;
if (!text.includes("(")) {
progressBarMessage = text;
}
progressBarIncrementCounter = progressBarIncrementCounterParam;
if (text) {
document.getElementById("progressText").textContent = text;
}
document.getElementById("progressBar").style.width = `${percent}%`;
}
function incrementProgressBar() {
updateProgressBar(
progressBarPercent + 30 / playlistsCount,
`${progressBarMessage} (${progressBarIncrementCounter++}/${playlistsCount})`,
progressBarIncrementCounter
);
}
function hideProgressBar() {
document.getElementById("progressBarContainer").style.display = "none";
}
function renderUI(playlists, getMetric) {
const playlistContainer = document.getElementById("playlistsContainer");
playlistsWithMetric = playlists.map((item) => ({
...item,
metric: getMetric(item),
}));
playlistsHtml = playlistsWithMetric.map(getPlaylistHTML).join("");
playlistContainer.innerHTML = playlistsHtml;
}
function selectButton(button) {
if (!button) {
return;
}
Array.from(button.parentElement.children).map((x) =>
x.firstElementChild.classList.remove(
"a4bc298d40e9660cd25cd3ac1a7f9c49-scss"
)
);
button.firstElementChild.classList.add(
"a4bc298d40e9660cd25cd3ac1a7f9c49-scss"
);
}
function renderPopular(button) {
selectButton(button);
renderUI(
playlistsData
.filter((x) => x.followers > 0)
.sort((b, a) => a.followers - b.followers),
(playlist) => `${playlist.followers} followers`
);
}
function renderRecent(button) {
if (!playlistsData && !playlistsData[0].created_at) {
return alert("Please wait for the loading to complete :)");
}
selectButton(button);
renderUI(
playlistsData
.filter((x) => x.followers > 0)
.sort((b, a) => a.last_updated - b.last_updated),
(playlist) => `${timeAgo(playlist.last_updated)} ago`
);
}
function renderNew(button) {
if (!playlistsData && !playlistsData[0].created_at) {
return alert("Please wait for the loading to complete :)");
}
selectButton(button);
renderUI(
playlistsData
.filter((x) => x.followers > 0)
.sort((b, a) => a.created_at - b.created_at),
(playlist) => `${timeAgo(playlist.created_at)} ago`
);
}
function hideModal() {
document.getElementById('modal').style.display = 'none'
}
function showModal() {
document.getElementById('modal').style.display = 'flex'
}
function refreshPlaylists() {
localStorage.removeItem("playlistsData");
location.reload();
}
function refreshPageWhenTokenExpires() {
const timeUntilTokenExpires = parseInt(window.localStorage.getItem("spotifyUserAccessTokenExpiry")) - Date.now()
setTimeout(redirectToLogin, timeUntilTokenExpires - 10000);
}
async function main() {
try {
wakeLock = await navigator.wakeLock.request("screen");
} catch (err) {
console.log(err);
}
processLoginRedirect();
if (!isLoggedIn()) {
updateProgressBar(0, "Redirecting to login via Spotify...");
document.getElementsByClassName("progress")[0].style.display = "none";
return redirectToLogin();
}
refreshPageWhenTokenExpires();
window.spotifyApi = new SpotifyWebApi();
spotifyApi.setAccessToken(
window.localStorage.getItem("spotifyUserAccessToken")
);
playlistsData = await fetchPlaylists();
renderPopular();
}
main();