-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblog-one.html
339 lines (281 loc) · 9.4 KB
/
blog-one.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="author" content="Himadri Shah" />
<meta name="description" content="Personal Portfolio of Himadri Shah" />
<meta name="keywords" content="Portfolio, Web Development" />
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="./style.css" />
<!-- Font Awesome Downloaded -->
<link rel="stylesheet" href="all.min.css" />
<!-- Google Fonts -->
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Poppins&display=swap"
rel="stylesheet"
/>
<link
href="https://fonts.googleapis.com/css2?family=Sacramento&display=swap"
rel="stylesheet"
/>
<title>Build a Movie App using TheMovieDb API</title>
</head>
<body>
<nav>
<ul class="nav-links">
<li><a onclick="window.location.href='index.html'">Home</a></li>
<li><a onclick="window.location.href='about.html'">About</a></li>
<li><a onclick="window.location.href='projects.html'">Projects</a></li>
<li>
<a
onclick="window.location.href='blogs.html'"
style="border-bottom: 2px solid var(--blue)"
>Blogs</a
>
</li>
<!-- <li><a onclick="jumpToDiv('contact')">Contact</a></li> -->
</ul>
<!-- <div class="burger">
<div class="line1"></div>
<div class="line2"></div>
<div class="line3"></div>
</div> -->
</nav>
<div class="blog-wrapper">
<main class="blog-page">
<h1>Build a Movie App using TheMovieDb API</h1>
<img src="./img/netflix.png" class="blog-movie-image" alt="Movie App" />
<h2>Introduction</h2>
The pandemic has made us all stay indoors for most of the day, which is
why we need some entertainment activities that can refresh us while
continuing to stay indoors. So, I thought of building a Movie App that
can give me a list of movies and their details upon searching for it. On
digging further, I came across TheMovieDb(TMDb) API which provides
information on movies, TV shows, actors, and much more. This API is free
to use and integrate into your website.
<h2>Get Started</h2>
Visit <a href="https://www.themoviedb.org/">TMDb website</a>, #
for a free account and generate your API key from the Settings tab. Read
every section of the
<a
href="https://developers.themoviedb.org/3/getting-started/introduction"
>API documentation</a
>
thoroughly.
<h2>HTML Page Structure</h2>
We are going to create a simple movie app, which will give a list of
movies, their Poster image, and rating. Here, we have an
<code>input</code> field for users to enter their search keywords. The
<code>div</code> with class result is used to display the result after
making the API call. Also, add the jQuery CDN inside the
<code>script</code> tag.
<div class="code">
<pre>
<div class="search">
<p>Search for a Movie</p>
<form id="searchForm">
<input type="text" id="searchInput" placeholder="Search here...">
</form>
</div>
<div class="result"></div>
<!-- jQuery minified CDN -->
<script type="text/javascript" src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</pre
>
</div>
<h2>Apply Styling</h2>
Style the search <code>div</code> using CSS
<div class="code">
<pre>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background-color: rgb(237, 242, 244);
overflow: auto;
}
.search {
margin: 2rem;
text-align: center;
}
p {
color: rgb(84, 101, 255);
font-size: 1.75rem;
margin: 0.5rem;
}
form input {
font-size: 1rem;
width: 60%;
padding: 0.5rem 0.75rem;
border: none;
}
form input:focus {
outline: none;
border: 1px solid rgb(84, 101, 255);
}
</pre
>
</div>
<h2>Add jQuery</h2>
Add jQuery code to call a JavaScript function
<code>getMovies</code> when a user submits the input form
<div class="code">
<pre>
$(document).ready(() => {
$('#searchForm').on('submit', (e) => {
e.preventDefault();
let searchText = $('#searchInput').val();
getMovies(searchText);
});
});
</pre
>
</div>
<h2>Making the API Call</h2>
In the <code>getMovies()</code> function, we will pass the
<code>searchText</code> as a parameter. The variable <code>url</code> is
the URL to make the jQuery AJAX API call. If the API call is successful,
then the result will be returned in JSON format which will be stored in
<code>data</code>. Then we will loop through it to fetch the values that
we want and structure them inside a <code>div</code> . If no result is
available, then we will display the error statement, as shown in the
<code>else</code> block.
<div class="code">
<pre>
const API_KEY = // enter your api key here
let baseURL = 'https://api.themoviedb.org/3/';
let imageURL = 'https://image.tmdb.org/t/p/w185';
function getMovies(searchText) {
let url = `${baseURL}search/movie?api_key=${API_KEY}&query=${searchText}&language=en-US&include_adult=false`;
$.ajax({
method: 'GET',
url: url,
success: function(data) {
let output = '';
if(data["results"].length > 0) {
for(let i = 0; i < data["results"].length; i++) {
let posterPath = data["results"][i]["poster_path"];
output +=
`<div class="movie">
<img class="image" src=${imageURL + String(posterPath)} alt="No image found." loading="lazy">
<div class="ratingFlex">
<h4 class="title">
${data["results"][i]["title"]}
</h4>
<div class="rating">
${data["results"][i]["vote_average"]}
</div>
</div>
</div>`;
$('.result').html(output);
}
}
else {
output +=
`<p class="empty">
No such movie was found. Try searching other keywords.
<p>`;
$('.result').html(output);
}
}
});
}
</pre
>
</div>
<h2>Style the Result of API call</h2>
Style the result <code>div</code> and its content.
<div class="code">
<pre>
.result {
margin: 2rem;
display: flex;
flex-direction: row;
align-items: unset;
justify-content: center;
flex-wrap: wrap;
}
.movie {
background-color: rgb(191, 215, 255);
color: rgb(84, 101, 255);
margin: 1rem;
margin-bottom: 1rem;
padding: 0.25rem;
width: min-content;
height: 100%;
overflow: hidden;
}
.movie .image{
display: block;
transition: transform 0.7s ease;
max-width: 320px;
min-height: 450px;
font-size: 1rem;
text-align: center;
}
.movie .image:hover {
transform: scale(1.02);
}
.ratingFlex {
display: flex;
align-items: flex-start;
justify-content: space-between;
margin: 0.5rem;
}
.ratingFlex .title {
padding: 0;
margin: 0;
}
.ratingFlex .rating {
background-color: rgb(237, 242, 244);
border: 1px solid rgb(84, 101, 255);
padding: 0.15rem 0.5rem;
margin: 0 0.5rem;
}
</pre
>
</div>
<h2>Output</h2>
We're done! Search for any movie and get the output as shown below.
<img
src="./img/blog-moviemania.png"
class="blog-movie-image"
alt="Movie App"
/>
</main>
</div>
<div class="contact-section">
<div class="title">Connect with me!</div>
<div class="contact">
<a href="mailto: himadrishah2000@gmail.com" title="Email"
><i class="fas fa-envelope-square"></i
></a>
<a
href="https://linkedin.com/in/himadri2110"
target="_blank"
title="LinkedIn"
><i class="fab fa-linkedin"></i
></a>
<a href="https://github.com/himadri2110" target="_blank" title="GitHub"
><i class="fab fa-github-square"></i
></a>
<a
href="https://twitter.com/himadri2110"
target="_blank"
title="Twitter"
><i class="fab fa-twitter-square"></i
></a>
</div>
<div class="footer">Made with 💚 by <u>Himadri Shah</u></div>
</div>
<!-- Scroll to Top -->
<div class="scroll-to-top">
<i class="fas fa-chevron-circle-up" onclick="scrollToTop()"></i>
</div>
</body>
</html>