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
/
lecture_nov_17.html
89 lines (78 loc) · 3.86 KB
/
lecture_nov_17.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
<html>
<head>
<title>API Mash-up</title>
<link rel="stylesheet" href="https://minicss.org/flavorFiles/mini-default.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.image-pad {
float: left;
padding-top: 5px;
padding-right: 50px;
padding-bottom: 30px;
padding-left: 0px;
}
</style>
</head>
<body>
<h1>Type the name of a track you love and its artist!</h1>
<form method="GET">
<label for="track">Track name:</label>
<input type="text" name="trackName" id="track">
<br>
<label for="artist">Artist name:</label>
<input type="text" name="artistName" id="artist">
<input type="submit">
</form>
<div id="bioContainer"></div>
<script type='text/JavaScript'>
const apiKey = "2a251e83d22408e76b6faeee0cb94d04";
function queryArtistAndTrack () {
let params = (new URL(document.location)).searchParams;
if (params.has('artistName') && params.has('trackName')) {
let artistName = params.get('artistName');
let trackName = params.get('trackName');
let queryURL = "http://ws.audioscrobbler.com/2.0" + "?method=" + "track.getsimilar" + "&artist=" + artistName + "&track=" + trackName + "&api_key=" + apiKey;
httpGet(queryURL)
var resultTracks = [];
for (var i = 0; i < 5; i++) {
resultTracks.push(trackName[i].innerHTML);
}
console.log("Similar Tracks:" + resultTracks)
}
for (var i = 0; i < 5; i++) {
console.log(resultTracks[i])
let retrievedData = xhttp.responseXML;
let trackData = retrievedData.getElementsByTagName('track')[0];
let trackName = trackData.getElementsByTagName('name')[0].innerHTML;
let artistData = retrievedData.getElementsByTagName('artist')[0];
let artistName = artistData.getElementsByTagName('name')[0].innerHTML;
let queryURL2 = "http://ws.audioscrobbler.com/2.0" + "?method=" + "track.getInfo" + "&api_key=" + apiKey + "&artist=" + artistName + "&track=" + trackName;
httpGet(queryURL2)
let retrievedData2 = xhttp.responseXML;
let bioContent = retrievedData2.getElementsByTagName('content')[0].innerHTML;
console.log("Similar's Bio:" + bioContent);
let imageURL = retrievedData2.getElementsByTagName('image')[3].innerHTML;
console.log("Similar's Image:" + imageURL);
similar = document.createElement('similar');
similar.innerHTML = resultTracks[i];
let bio = document.createElement('p');
bio.innerHTML = bioContent;
let image = document.createElement('img');
image.src = imageURL;
image.classList.add("image-pad");
let infobox = document.getElementById('bioContainer');
infobox.appendChild(similar);
infobox.appendChild(bio);
infobox.appendChild(image);
document.getElementById('bioContainer').appendChild(infobox);
}
}
function httpGet(theUrl) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", theURL, false);
xmlHttp.send();
return xmlHttp.responseXML;
}
</script>
</body>
</html>