-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsongdata.js
563 lines (518 loc) · 21.8 KB
/
songdata.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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
// Example starter JavaScript for disabling form submissions if there are invalid fields
window.addEventListener('load', function() {
// Fetch all the forms we want to apply custom Bootstrap validation styles to
var forms = document.getElementsByClassName('needs-validation');
// Loop over them and prevent submission
var validation = Array.prototype.filter.call(forms, function(form) {
form.addEventListener('submit', function(event) {
if (form.checkValidity() === false) {
event.preventDefault();
event.stopPropagation();
}
form.classList.add('was-validated');
}, false);
});
}, false);
function pickHex(color1, color2, weight) {
var w1 = weight;
var w2 = 1 - w1;
var rgb = [Math.round(color1[0] * w1 + color2[0] * w2),
Math.round(color1[1] * w1 + color2[1] * w2),
Math.round(color1[2] * w1 + color2[2] * w2)
];
return rgb;
}
function keyMap(src, target) {
target = target || {};
Object.keys(src).forEach(function(propName) {
var prop = src[propName];
if (typeof prop == "object") {
target[propName] = Object.keys(prop).join(',');
keyMap(prop, target);
}
});
return target;
};
function searchForTrack(ev) {
//console.log("searching...");
if (sessionStorage.getItem("track_features")) {
sessionStorage.removeItem("track_features");
}
if (sessionStorage.getItem("track_info")) {
sessionStorage.removeItem("track_info");
}
if (sessionStorage.getItem("artist_info")) {
sessionStorage.removeItem("artist_info");
}
if ($('.form-control:invalid').length == 0) {
//console.log("searching...");
var searchfield = $("#validationCustom01")[0];
var search = searchfield.value;
//console.log(searchfield.value);
var params = { "q": search, "type": "track", "limit": 1 };
//console.log(jQuery.param(params));
var url = "https://api.spotify.com/v1/search/?" + jQuery.param(params);
//url = "https://api.spotify.com/v1/audio-features/3Wv6AagA0qqWH7nRDrkgh7";
loadRequest(url, complete_search, 1);
// send data as a dictionary
//Put a loading icon in the button and disable it
//spinner = html.SPAN(Class = "spinner-border spinner-border-sm");
var spinner = document.createElement("span");
spinner.classList = "spinner-border spinner-border-sm";
var searchbutton = $("#searchbutton")[0];
//console.log(searchbutton.innerHTML);
searchbutton.innerHTML = "";
searchbutton.append(spinner);
searchbutton.disabled = true;
}
}
function complete_search(req) {
if (req.status == 200 || req.status == 0) {
results = JSON.parse(req.responseText);
if (results["tracks"]["items"].length > 0) {
for (i = 0; i < results["tracks"]["items"].length; i++) {
try {
console.log(results["tracks"]["items"][i]["name"] + " by " + results["tracks"]["items"][i]["artists"][0]["name"]);
var track_id = results["tracks"]["items"][i]["id"];
var artist_id = results["tracks"]["items"][i]["artists"][0]["id"];
var features_url = "https://api.spotify.com/v1/audio-features/" + track_id;
var info_url = "https://api.spotify.com/v1/tracks/" + track_id;
var artist_url = "https://api.spotify.com/v1/artists/" + artist_id;
loadRequest(features_url, saveData, 1);
loadRequest(info_url, saveData, 2);
loadRequest(artist_url, saveData, 3);
//getTrackData(results["tracks"]["items"][i]["id"]);
} catch (err) {
console.log("Error");
break;
}
}
} else {
console.log("No results for that search");
showAlert("<strong>No songs found for that search!</strong>", "alert-danger", 5000);
$("#searchbutton")[0].innerHTML = "Show me data!";
$("#searchbutton").prop("disabled", false);
}
} else if (req.status == 401) {
$("#searchbutton")[0].innerHTML = "Please login to search";
}
}
function saveData(req, identifier) {
//console.log(req.responseText);
var response = req.responseText;
if (identifier == 1) {
sessionStorage.setItem("track_features", response);
} else if (identifier == 2) {
sessionStorage.setItem("track_info", response);
} else if (identifier == 3) {
sessionStorage.setItem("artist_info", response);
} else {
console.log("Unkown Error");
}
var track_features = sessionStorage.getItem("track_features");
//console.log(track_features);
var track_info = sessionStorage.getItem("track_info");
//console.log(track_info);
var artist_info = sessionStorage.getItem("artist_info");
//console.log(artist_info);
if (track_features !== null & track_info !== null & artist_info != null) {
//console.log("Data received, formatting");
formatData(track_features, track_info, artist_info);
}
}
function formatData(track_features, track_info, artist_info) {
track_features = JSON.parse(track_features);
track_info = JSON.parse(track_info);
artist_info = JSON.parse(artist_info);
let song_key_codes = new Map([
[-1, "Unkown key"],
[0, "C"],
[1, "C#"],
[2, "D"],
[3, "D#"],
[4, "E"],
[5, "F"],
[6, "F#"],
[7, "G"],
[8, "G#"],
[9, "A"],
[10, "A#"],
[11, "B"]
]);
//console.log(song_key_codes)
let song_mode_codes = new Map([
[-1, "Unkown mode"],
[0, "minor"],
[1, "major"]
]);
track_features["duration"] = convertMilliseconds(track_features["duration_ms"]);
delete track_features["duration_ms"];
track_features["loudness"] = track_features["loudness"].toFixed(1) + " decibels";
track_features["valence (happiness)"] = +track_features["valence"].toFixed(2);
delete track_features["valence"];
track_features["time signature"] = track_features["time_signature"];
delete track_features["time_signature"];
track_features["tempo"] = Math.round(track_features["tempo"]) + " BPM";
track_features["danceability"] = Math.round(track_features["danceability"] * 100);
track_features["energy"] = Math.round(track_features["energy"] * 100);
track_features["speechiness"] = Math.round(track_features["speechiness"] * 100);
track_features["acousticness"] = Math.round(track_features["acousticness"] * 100);
track_features["instrumentalness"] = Math.round(track_features["instrumentalness"] * 100);
track_features["liveness"] = Math.round(track_features["liveness"] * 100);
track_features["valence (happiness)"] = Math.round(track_features["valence (happiness)"] * 100);
//console.log(track_features)
//Insert player widget
/*<iframe src="https://open.spotify.com/embed/track/0NWPxcsf5vdjdiFUI8NgkP"
width="300" height="80" frameborder="0" allowtransparency="true"
allow="encrypted-media"></iframe>*/
if ($('#player').length == 0) {
//this is the first request and there is no player present
} else {
//the player exists already
$('#player').remove();
}
var player = document.createElement("iframe");
var playerdiv = $("#playerdiv");
player.id = "player";
player.src = "https://open.spotify.com/embed/track/" + track_info["id"];
player.width = "300";
player.height = "80";
player.frameBorder = "0";
player.allowTransparency = "true";
player.allow = "encrypted-media";
player.classList = "shadow rounded-lg";
playerdiv.append(player);
//All the elements will be inserted in the div with the "container" class
var container = $(".container")[0];
if ($('table').length == 0) {
//this is the first request and there is no table present
} else {
//the table exists already
$('#rowdiv').remove();
}
var rowdiv = document.createElement("div");
rowdiv.classList = "row";
rowdiv.id = "rowdiv";
var tablediv = document.createElement("div");
tablediv.classList = "col";
var table = document.createElement("table");
table.classList = "table table-sm table-borderless mx-auto mt-3 p-2 rounded-lg shadow";
table.style.backgroundColor = properties.BACKGROUND_COLOR
table.style.color = properties.TEXT_COLOR
table.style.maxWidth = "500px";
var thead = document.createElement("thead");
thead.classList = "border-success border-bottom";
var tbody = document.createElement("tbody");
table.append(thead);
table.append(tbody);
//Add header 1: song title and artist
try {
var song_title = track_info["name"];
var album = track_info["album"]["name"];
var artist = track_info["album"]["artists"][0]["name"];
var tablerowh = document.createElement("tr");
tablerowh.classList = "text-center";
var tableheader = document.createElement("th");
tableheader.colSpan = "2";
tableheader.innerHTML = "Stats for " + song_title + " by " + artist;
//html.TH("Stats for " + song_title + " by " + artist, colspan="2"); //+ " on the album " + album + ":\n"
tablerowh.append(tableheader);
thead.append(tablerowh);
} catch {
thead.innerHTML = "Error: Could not get track from the provided id";
}
//add header 2: actual table header
var tablerowa = document.createElement("tr");
var tableheader1 = document.createElement("th");
tableheader1.innerHTML = "Song Attribute";
var tableheader2 = document.createElement("th");
tableheader2.innerHTML = "Value";
tableheader2.classList = "text-left";
tablerowa.append(tableheader1);
tablerowa.append(tableheader2);
thead.append(tablerowa);
//html.TR(html.TH("Song Attribute") + html.TH("Value", Class="text-left"))
/*for key, value in track_features:
tbody <= html.TR(html.TD(key) +
html.TD(value))
Now we add some style to the table
table.style = {"padding": "5px",
"backgroundColor": "#aaaaaa",
"width": "100%"}
Now we add the table to the new div previously created
tablediv <= table
except:
document <= "An error occured";*/
track_features["song popularity"] = track_info["popularity"];
//console.log(track_info["popularity"]);
track_features["artist popularity"] = artist_info["popularity"];
//console.log("artist popularity: " + artist_info["popularity"] + " " + track_features["artist popularity"]);
if (track_info["explicit"] == false) {
//console.log("explicit: no");
track_features["explicit"] = "no";
} else {
//console.log("explicit: yes");
track_features["explicit"] = "yes";
}
track_features["release date"] = track_info["album"]["release_date"];
//console.log("release date: " + track_info["album"]["release_date"]);
//var first = true;
//console.log(artist_info["genres"][artist_info["genres"].length-1]);
if (artist_info["genres"].length == 0) {
genre_string = "unknown";
} else if (artist_info["genres"].length == 1) {
genre_string = artist_info["genres"][0];
} else {
var genre_string = ""
for (let i of artist_info["genres"].keys()) {
//console.log(i);
if (artist_info["genres"][i] == artist_info["genres"][artist_info["genres"].length - 1]) {
genre_string = genre_string + artist_info["genres"][i];
} else {
genre_string = genre_string + artist_info["genres"][i] + ", ";
}
}
}
//tbody <= html.TR(html.TD("genres", Class="text-left", style={"max-width":"80px"}) + html.TD(genre_string, Class="text-left", style={"max-width":"80px"}))
var tablerow = document.createElement("tr");
var tabledata1 = document.createElement("td");
tabledata1.innerHTML = "artist genres";
tabledata1.classList = "text-left";
tabledata1.style.maxWidth = "80px";
var tabledata2 = document.createElement("td");
tabledata2.innerHTML = genre_string;
tabledata2.classList = "text-left";
tabledata2.style.maxWidth = "80px";
tablerow.append(tabledata1);
tablerow.append(tabledata2);
tbody.append(tablerow);
for (key in track_features) {
if (key == "key") {
//console.log("going into key");
//console.log(song_key_codes);
for (let cay of song_key_codes.keys()) {
//console.log(track_features[key] + " " + cay + " hello");
if (track_features[key] == cay) {
track_features[key] = song_key_codes.get(cay);
}
}
}
if (key == "mode") {
//console.log("going into mode")
for (let cay of song_mode_codes.keys()) {
if (track_features[key] == cay) {
track_features[key] = song_mode_codes.get(cay);
}
}
}
if (key == "type" || key == "uri" || key == "track_href" || key == "analysis_url" || key == "id") {} else {
//console.log(key + ": " + track_features[key])
//tbody <= html.TR(html.TD(key, Class="text-left", style={"max-width":"80px"}) + html.TD(track_features[key], Class="text-left", style={"max-width":"80px"}))
var tablerow = document.createElement("tr");
var tabledata1 = document.createElement("td");
tabledata1.innerHTML = key;
tabledata1.classList = "text-left";
tabledata1.style.maxWidth = "80px";
var tabledata2 = document.createElement("td");
tabledata2.innerHTML = track_features[key];
tabledata2.classList = "text-left";
tabledata2.style.maxWidth = "80px";
tablerow.append(tabledata1);
tablerow.append(tabledata2);
tbody.append(tablerow);
//console.log(key, typeof(track_features[key]))
val = track_features[key] / 100;
if (typeof(track_features[key]) == "number" && key != "time signature") {
if (val == .5) {
color = [255, 255, 255];
}
/*else if (val < .5) {
color = pickHex([255, 255, 255], [117, 117, 117], val)
} */
else {
color = pickHex([255, 0, 0], [255, 255, 255], val);
}
colorstring = "rgb(" + color[0] + ", " + color[1] + ", " + color[2] + ")";
tabledata2.style.color = colorstring;
tabledata1.style.color = colorstring;
//console.log(key, val, typeof(val), tabledata2.style.color)
}
//var hue = Math.floor((100 - val) * 220 / 100); // go from green to red
//var saturation = Math.abs(val - 50)/50; // fade to white as it approaches 50
//console.log($("body")[0].style.backgroundColor, color)
//$("body")[0].classList = ""
//val = .500000001
//$("body")[0].style.backgroundColor = "rgb(" + color[0] + ", " + color[1] + ", " + color[2] + ")" //"hsl(" + hue + ", " + 100 + "%, 50%)"
}
}
table.append(thead);
table.append(tbody);
tablediv.append(table);
rowdiv.append(tablediv);
container.append(rowdiv);
generateChart(track_features);
/*if ($('table').length == 0) {
//this is the first request and there is no table present
} else {
//the table exists already
$('#rowdiv').remove();
}
var rowdiv = document.createElement("div");
rowdiv.classList = "row";
rowdiv.id = "rowdiv";
var tablediv = document.createElement("div");
tablediv.classList = "col";
var table = document.createElement("table");
table.classList = "table table-dark table-sm table-borderless mx-auto mt-3 p-2 rounded-lg shadow";
table.style.maxWidth = "500px";*/
searchbutton.innerHTML = "Show me data!";
searchbutton.disabled = false;
}
function generateChart(track_features) {
let labels = [];
let song_data = [];
for (i in track_features) {
if (isNumber(track_features[i]) == false || i == "time signature" || i == "release date") {
delete track_features[i];
} else {
track_features[i] = parseFloat(track_features[i]);
labels.push(capitalize(i));
song_data.push(track_features[i]);
}
}
//console.log(track_features)
if ($('#chartdiv')) {
$('#chartdiv').remove();
}
var container = $(".container")[0];
var chartcontainer = document.createElement("div");
chartcontainer.style.maxWidth = "500px";
chartcontainer.style.width = $("table")[0].offsetWidth;
//console.log($("table")[0].offsetWidth);
chartcontainer.classList = "mx-auto mt-3 p-2 rounded-lg shadow";
chartcontainer.id = "chartcontainer";
var col = document.createElement("div");
col.classList = "col";
var rowdiv = document.createElement("div");
rowdiv.classList = "row";
rowdiv.id = "chartdiv";
chartcontainer.innerHTML = '<canvas id="myChart"></canvas>';
col.append(chartcontainer);
rowdiv.append(col);
container.append(rowdiv);
var ctx = document.getElementById('myChart').getContext('2d');
ctx.canvas.width = $('#chartcontainer')[0].offsetWidth; // resize to parent width
//ctx.canvas.height = $('#chartcontainer').height(); // resize to parent height
//var ctx = document.getElementById('myChart').getContext('2d');
/*var chart = new Chart(ctx, {
// The type of chart we want to create
type: 'bar',
// The data for our dataset
data: {
labels: labels, //['Popularity', 'Artist Popularity', 'Danceability', 'Energy', 'Happiness', 'Accousticness', 'Liveness', 'Loudness', 'Instrumentalness']
datasets: [{
backgroundColor: '#1DB95433',
borderColor: '#1DB954',
borderDash: [],
pointBackgroundColor: '#1DB954',
data: song_data //[0, 10, 5, 2, 20, 30, 45, 46, 87]
}]
},
// Configuration options go here
options: {
legend: {
display: false
},
scale: {
pointLabels: {
fontColor: 'white',
fontSize: '14'
},
ticks: {
beginAtZero: true,
max: 100,
stepSize: 20
}
}
}
});*/
var chart = new Chart(ctx, {
// The type of chart we want to create
type: 'horizontalBar',
// The data for our dataset
data: {
labels: labels, //['Popularity', 'Artist Popularity', 'Danceability', 'Energy', 'Happiness', 'Accousticness', 'Liveness', 'Loudness', 'Instrumentalness']
datasets: [{
//backgroundColor: '#1DB954',
backgroundColor: function(context) {
//console.log(context);
val = context.dataset.data[context.dataIndex] / 100;
if (val == .5) {
color = [255, 255, 255];
}
/*else if (val < .5) {
color = pickHex([255, 255, 255], [117, 117, 117], val)
} */
else {
color = pickHex([255, 0, 0], [255, 255, 255], val);
}
return "rgb(" + color[0] + ", " + color[1] + ", " + color[2] + ")";
}, //['red', 'blue', 'green', 'brown', 'black', 'orange', 'purple', 'yellow', 'gray'],
//borderColor: '#1DB954',
minBarLength: 2,
//pointBackgroundColor: '#1DB954',
data: song_data //[0, 10, 5, 2, 20, 30, 45, 46, 87]
}]
},
// Configuration options go here
options: {
legend: {
display: false
},
scales: {
yAxes: [{
ticks: {
fontColor: "#FFFFFF"
}
}],
xAxes: [{
ticks: {
suggestedMax: 100,
fontColor: "#FFFFFF",
fontSize: 17
}
}]
}
/*,
animation: {
easing: "easeInBounce",
duration: 2000
}*/
}
});
chart.update();
}
function init() {
$("#songdata_link").addClass("active");
$("#searchlabel")[0].style.color = properties.TEXT_COLOR;
var success = getParamsFromURL("songdata");
//sessionStorage.setItem('spotify_auth_state', "test") //sessionStorage.getItem('received_state'))
//$("#songdata_link")[0].href = "https://spotifydata.com/songdata" + sessionStorage.getItem('raw_hash')
//$("#userdata_link")[0].href = "https://spotifydata.com/userdata" + sessionStorage.getItem('raw_hash')
//try {
if (success & sessionStorage.getItem('received_state') == sessionStorage.getItem('spotify_auth_state')) {
$("#validationCustom01").keypress(function(e) {
if (e.which == 13) {
e.preventDefault();
if ($("#searchbutton")[0].disabled == false) {
searchForTrack();
}
}
});
$("#searchbutton")[0].addEventListener("click", function() { searchForTrack() });
//if
} else {
showErrorMessage();
console.log(success, sessionStorage.getItem('received_state'), sessionStorage.getItem('spotify_auth_state'))
}
}