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
/
version2.html
79 lines (63 loc) · 2.85 KB
/
version2.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
<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'>
function queryArtistAndTrack () {
const apiKey = "2a251e83d22408e76b6faeee0cb94d04";
// get artist & track name from form
let params = (new URL(document.location)).searchParams;
if (params.has('artistName') && params.has('trackName')) {
let artistName = params.get('artistName');
console.log(artistName);
let trackName = params.get('trackName');
console.log(trackName);
// create the API call
let queryURL = "http://ws.audioscrobbler.com/2.0" + "?method=" + "track.getsimilar" + "&artist=" + artistName + "&track=" + trackName + "&api_key=" + apiKey;
retrievedData = httpGet(queryURL);
// parse the repsonse
let trackData = retrievedData.getElementsByTagName('track')[0];
let similarTrackName = trackData.getElementsByTagName('name');
let artistData = retrievedData.getElementsByTagName('artist')[0];
let similarArtistName = artistData.getElementsByTagName('name')[0].innerHTML;
let queryURL2 = "http://ws.audioscrobbler.com/2.0" + "?method=" + "track.getInfo" + "&api_key=" + apiKey + "&artist=" + similarArtistName + "&track=" + similarTrackName;
retrievedData2 = httpGet(queryURL2);
// append the results to the DOM
var similarTracks = [];
for (var i = 0; i < 5; i++) {
similarTracks.push(similarTrackName[i].innerHTML);
}
console.log("Similar tracks:" + similarTracks)
}
}
function httpGet(theUrl) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", theUrl, false);
xmlHttp.send();
return xmlHttp.responseXML;
}
</script>
</body>
</html>