This repository has been archived by the owner on Sep 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assignment_6.html
112 lines (105 loc) · 4.49 KB
/
assignment_6.html
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
<html>
<head>
<title>MusicBrainz API Call</title>
<link rel="stylesheet" href="mystyle2.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
table {
width: 100%;
border-collapse: collapse;
}
td {
border: 2px solid blueviolet;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: skyblue
}
</style>
</head>
<body>
<div>
<h2>Basic Call to the MusicBrainz API</h2>
<p>Retrieve the discography of an artist & the dates of release!</p>
<form id='artist' method="GET">
<label for="name">Artist name?</label>
<input type="text" name="artist" id="name">
<input type="submit" value="Get Results">
</form>
</div>
<div id="table" class="container"></div>
<script type="text/JavaScript">
function queryArtist() {
let params = (new URL(document.location)).searchParams;
if (params.has('artist')) {
let artistName = params.get('artist');
let mBaseURL = "https://musicbrainz.org/ws/2/";
let mbResource = "artist?query=";
let queryURL = mBaseURL + mbResource + artistName;
httpGet(queryURL, getMBID);
}
}
function httpGet(theURL, cbFunction) {
let xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", theURL);
xmlHttp.send();
xmlHttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
cbFunction(this);
}
};
}
function getMBID(xhttp) {
let retrievedData = xhttp.responseXML;
console.log(retrievedData);
let artistData = retrievedData.getElementsByTagName('artist')[0];
let artistMBID = artistData.id;
let artistName = artistData.getElementsByTagName('name')[0].innerHTML;
let mBaseURL = "https://musicbrainz.org/ws/2/release-group?artist=";
let queryURL = mBaseURL + artistMBID;
console.log(queryURL);
httpGet(queryURL, getAlbums)
}
function getAlbums(xhttp) {
let retrievedData = xhttp.responseXML;
console.log(retrievedData);
var albums = [];
let all=retrievedData.getElementsByTagName('release-group');
for (var i = all.length - 1; i >=0; i--) {
let type=all[i].getAttribute('type');
console.log(type);
if(type=='Album') {
albums.push(all[i]);
}
}
sortedAlbums=albums.sort((a,b)=>new Date(a.getElementsByTagName('first-release-date')[0].innerHTML) - new Date(b.getElementsByTagName('first-release-date')[0].innerHTML));
displayTable(sortedAlbums);
}
function displayTable(albums) {
let table = document.createElement('table');
table.id = 'table';
tr = document.createElement('tr');
td1 = document.createElement('td');
tr.appendChild(td1);
td1.innerHTML = "<b>Album Name<b>";
td2 = document.createElement('td');
tr.appendChild(td2);
td2.innerHTML = "<b>Date of Release<b>";
table.appendChild(tr);
for (row = 0; row < albums.length; row++) {
tr = document.createElement('tr');
td1 = document.createElement('td');
tr.appendChild(td1);
td1.innerHTML = albums[row].getElementsByTagName('title')[0].innerHTML;
td2 = document.createElement('td');
tr.appendChild(td2);
td2.innerHTML = albums[row].getElementsByTagName('first-release-date')[0].innerHTML;
table.appendChild(tr);
}
document.getElementById('table').appendChild(table);
}
window.onload = queryArtist;
</script>
</body>
</html>