-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
171 lines (156 loc) · 5.42 KB
/
index.js
File metadata and controls
171 lines (156 loc) · 5.42 KB
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
/* global clientID, domain, playlists, tracks, albums, artists */
var libOnly;
var allPLs;
var includePLs = false;
function getKeyFromCookie() {
for (var cookie of document.cookie.split(";")) {
if (cookie.split("=")[0] === "spotifyAPIKey") {
return cookie.split("=")[1];
}
}
return null;
}
function getKeyFromURL() {
for (var frag of window.location.hash.split(/[#?&]/)) {
if (frag.startsWith("access_token")) {
return frag.substring(frag.indexOf("access_token") + 13, frag.length);
}
}
return null;
}
function determine() {
var key = getKeyFromCookie();
if (key !== null) {
setUserToken(key);
return true;
}
key = getKeyFromURL();
if (key !== null) {
var expire = new Date();
expire.setDate(expire.getDate() + 7);
document.cookie = "spotifyAPIKey=" + key + "; expires=" + expire.toUTCString();
window.location = "/spotiview/index.html";
return false;
}
return false;
}
function loadAlbumsByYearHelper(valChecker) {
var ret = {
albs: [],
lowestYear: -1,
highestYear: -1,
longestAlbArr: -1
};
for (var alb of Object.values(albums)) {
if (!valChecker(alb)) {
continue;
}
if (!ret.albs[alb.year]) {
ret.albs[alb.year] = [];
}
ret.albs[alb.year].push(alb);
if (alb.year !== undefined && (alb.year > ret.highestYear || ret.highestYear === -1)) {
ret.highestYear = alb.year;
}
if (alb.year !== undefined && (alb.year < ret.lowestYear || ret.lowestYear === -1)) {
ret.lowestYear = alb.year;
}
if (ret.albs[alb.year].length > ret.longestAlbArr || ret.longestAlbArr === -1) {
ret.longestAlbArr = ret.albs[alb.year].length;
}
}
return ret;
}
function loadAlbumsByYear() {
libOnly = loadAlbumsByYearHelper(a => !a.fromPL);
allPLs = loadAlbumsByYearHelper(a => !a.notAddedByOwner);
}
function displayAll() {
for (var tableCount = 1; tableCount <= 2; tableCount++) {
var thead = document.getElementById("aHead" + tableCount);
var tabl = document.getElementById("aTable" + tableCount);
var nameLater = [allPLs, libOnly][tableCount - 1];
var ttd = "";
for (var i = nameLater.lowestYear; i <= nameLater.highestYear; i++) {
ttd += "<th class='rowe'><p>" + "</p></th>";
}
thead.innerHTML = "<tr>" + ttd + "</tr>";
for (var i = nameLater.longestAlbArr; i >= 0; i -= 1) {
ttd = "";
for (var year = nameLater.lowestYear; year <= nameLater.highestYear; year++) {
if (nameLater.albs[year] === undefined) {
ttd += "<td><p></p></td>";
} else if (nameLater.albs[year].length > i) {
ttd +=
"<td><img src='" +
nameLater.albs[year][i].img +
"' height='16' width='16' title=\"" + nameLater.albs[year][i].name.replaceAll(/"/g, '\\\"') + " (" + nameLater.albs[year][i].year + ")\" class='imag'></img></td>";
} else {
ttd += "<td><p></p></td>";
}
}
tabl.innerHTML += "<tr class='tabl'>" + ttd + "</tr>";
}
ttd = "";
for (var i = nameLater.lowestYear; i <= nameLater.highestYear; i++) {
var text = "";
if (("" + i).endsWith("0") || i === nameLater.lowestYear || i === nameLater.highestYear) {
text = i;
}
ttd += "<td><p class='lbl' style='text-align:center'>" + text + "</p></td>";
}
tabl.innerHTML += "<tr>" + ttd + "</tr>";
}
}
function scopeSelect() {
includePLs = document.getElementById("scope-select").value === "allPLs";
document.getElementById("aTable1").hidden = !includePLs;
document.getElementById("aTable2").hidden = includePLs;
document.getElementById("top5artists").hidden = includePLs;
document.getElementById("top5artistsPL").hidden = !includePLs;
}
function artists() {
var ap = document.getElementById("top5artists");
var apPL = document.getElementById("top5artistsPL");
var top5 = Object.keys(artists).sort(function (a, b) {
return artists[b].numTracks - artists[a].numTracks;
});
var top5PL = Object.keys(artists).sort(function (a, b) {
return artists[b].numPLTracks - artists[a].numPLTracks;
});
var count = 0;
for (var artist of top5) {
count++;
ap.innerHTML += "<p>" + artists[artist].name + " (" + artists[artist].numTracks + ")</p>";
if (count === 5)
break;
}
count = 0;
for (var artist of top5PL) {
count++;
apPL.innerHTML += "<p>" + artists[artist].name + " (" + artists[artist].numPLTracks + ")</p>";
if (count === 5)
break;
}
}
function afterLoad() {
document.getElementById("loading").hidden = true;
loadAlbumsByYear();
displayAll();
scopeSelect();
artists();
document.getElementById("analysis").hidden = false;
}
function onLoad() {
if (determine()) {
document.getElementById("loading").hidden = false;
gatherData(afterLoad);
} else {
document.getElementById("landing").hidden = false;
}
}
function donate() {
document.getElementById("donateDiv").hidden = false;
window.scrollTo(0, 10000);
}
onLoad();