Repository navigation
Expand file tree
/
Copy pathapi.html
More file actions
154 lines (153 loc) · 6.53 KB
/
Copy pathapi.html
File metadata and controls
154 lines (153 loc) · 6.53 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
<!-- Nirvaan Govind | CS20 Assignment 5: Product List in JSON, Part 2 API -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>api.html</title>
<style>
.song {
border: 1px solid black;
padding: 16px;
background-color: beige;
font-family: Arial, sans-serif;
}
.button {
display: block;
margin: 0 auto 20px;
padding: 8px 20px;
font-size: 14px;
font-family: Arial, sans-serif;
background-color: beige;
color:black;
cursor: pointer;
border: 1px solid black;
}
.button:hover {
background-color: #e8e4c4;
}
h1 {
font-family: Arial, sans-serif;
text-align: center;
}
body {
max-width: 1000px;
margin: 0 auto;
}
#result {
display:grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 20px;
}
#searchInput {
display: block;
margin: 0 auto 12px;
padding: 10px;
width: 300px;
font-size: 16px;
font-family: Arial, sans-serif;
border: 1px solid black;
}
img {
width: 100%;
}
.message {
grid-column: 1 / -1;
text-align: center;
font-family: Arial, sans-serif;
}
audio {
width: 100%;
}
#about {
font-family: Arial, sans-serif;
margin-top: 40px;
}
</style>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</head>
<body>
<h1>All the Music You'll Ever Need to Know About</h1>
<input type="text" id="searchInput" placeholder="Search artist/song">
<button type="button" class="button" id="searchBtn">Search</button>
<div id="result"></div>
<script>
$("#searchBtn").click(function() {
let query = $("#searchInput").val();
if (query.trim() === "") {
$("#result").html("<p class='message'>Type an artist or song first.</p>");
return;
}
let url = "https://itunes.apple.com/search?term=" + encodeURIComponent(query) + "&media=music&limit=6";
fetch(url)
.then(res => res.text())
.then(data=>{
data = JSON.parse(data);
let output = "";
for (let song of data.results) {
output += "<div class='song'>";
output += "<img src='" + song.artworkUrl100.replace("100x100", "300x300") + "'>";
output += "<h3>" + song.trackName + "</h3>";
output += "<p>" + song.artistName + "</p>";
output += "<p>" + song.collectionName + "</p>";
output += "<audio controls src='" + song.previewUrl + "'></audio>";
output += "</div>";
}
if (output === "") {
output = "<p class='message'>Uh-oh. Nothing found.</p>";
}
$("#result").html(output);
})
.catch(error => console.log(error));
})
</script>
<div id="about">
<p><b>The API I selected, and why</b></p>
<ul>
<li>The API I selected is the iTunes Search API</li>
<li>The iTunes Search API lets anyone search the iTunes Store Catalog</li>
<li>It returns songs, albums, movies, podcasts, etc</li>
<li>
For each query, the API returns the title, artist, album, genre, release date,
price, album artwork, and a link to a 30-sec audio preview
</li>
<li>
I chose this API because it matched all the requirements, but also because it
returns rich data like album artwork and a 30-second audio preview, which let me
build something more interactive than just plain text.
</li>
</ul>
<br>
<p><b>The website where I found it</b></p>
<ul>
<li>I specifically looked for Music-based keyless APIs that return data in a JSON format</li>
<li>While doing so, I came across <a href="https://codehelper.me/resources/apis/music/">this</a> list.</li>
<li>That led me to the API's <a href="https://performance-partners.apple.com/search-api">source docs</a> from Apple</li>
</ul>
<br>
<p><b>Options/Parameters used for API request</b></p>
<ul>
<li>The API is called with a GET request to https://itunes.apple.com/search, with 3 parameters in the query string:
<ul>
<li>
<b>term</b>: the search query, coming from what the user types in the search box.
This is URL-encoded with <code>encodeURIComponent()</code> so names with special
characters don't break the URL.
</li>
<li>
<b>media=music</b>: limits results to music only. Without it, the API also returns movies, podcasts, apps, and books.
</li>
<li>
<b>limit=6</b>: caps the number of results at 6. The default is 50.
</li>
</ul>
</li></ul>
<br>
<p><b>An example where this API would be helpful</b></p>
<ul>
<li>This API could be quite useful if one were to build a music cataloguing app like Letterboxd, but for songs instead of movies.</li>
</ul>
<br>
</div>
</body>
</html>