· 5 years ago · Feb 10, 2021, 08:56 PM
1$(document).ready(function () {
2
3 $('#term').focus(function () {
4 var full = $("#poster").has("img").length ? true : false;
5 if (full == false) {
6 $('#poster').empty();
7 }
8 });
9
10 /*
11 ************************
12 *ADD YOUR API KEY BELOW*
13 ************************
14 */
15 var API_KEY = 'YOUR API KEY GOES HERE';
16
17 var getPoster = function () {
18
19 var film = $('#term').val();
20
21 if (film == "") {
22
23 $('#poster').html("<h2 class='loading'>Ha! We haven't forgotten to validate the form! Please enter something.</h2>");
24
25 } else {
26
27 $('#poster').html("<h2 class='loading'>Your poster is on its way!</h2>");
28
29 $.getJSON("https://api.themoviedb.org/3/search/movie?api_key=" + API_KEY + "&language=en-US&query=" + film + "&page=1&include_adult=false", function (json) {
30 if (json != "Nothing found.") {
31 console.log(json);
32 $('#poster').html('<h2 class="loading">Well, gee whiz! We found you a poster, skip!</h2><img id="thePoster" src="' + "http://image.tmdb.org/t/p/w185" + json.results[0].poster_path + '" />');
33 } else {
34 $.getJSON("http://api.themoviedb.org/2.1/Movie.search/en/json/23afca60ebf72f8d88cdcae2c4f31866/goonies?callback=?", function (json) {
35 console.log(json);
36 $('#poster').html('<h2 class="loading">We\'re afraid nothing was found for that search. Perhaps you were looking for The Goonies?</h2><img id="thePoster" src=' + json[0].posters[0].image.url + ' />');
37 });
38 }
39 });
40
41 }
42
43 return false;
44 }
45
46 $('#search').click(getPoster);
47 $('#term').keyup(function (event) {
48 if (event.keyCode == 13) {
49 getPoster();
50 }
51 });
52
53});