-
Notifications
You must be signed in to change notification settings - Fork 0
/
movie.js
88 lines (71 loc) · 3.1 KB
/
movie.js
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
function getSite(id) {
//alert(id);
fetch(`https://api.themoviedb.org/3/movie/${id}?api_key=9f81f98dda461876ca00dc09fc4226ac&language=en-US`, {
method: 'GET',
}).then(reply => reply.json()).then(function(reply){
console.log(reply);
// return reply.homepage;
if ( reply.homepage != null )
{
document.getElementById("website").setAttribute('href', reply.homepage);
document.getElementById("poster").setAttribute('src', `http://image.tmdb.org/t/p/w200/${reply.poster_path}`);
}
}).catch(error => console.error('Error: ', error));
}
function getGenre(genre) {
fetch('https://api.themoviedb.org/3/genre/movie/list?api_key=9f81f98dda461876ca00dc09fc4226ac&language=en-US', {
method: 'GET',
}).then(result => result.json()).then(function(result){
console.log(result);
var gen = result.genres;
for ( var i = 0; i < gen.length; i++ )
{
if ( gen[i].name.toLowerCase() == genre.toLowerCase() )
{
return gen[i].id;
}
}
return 37;
}).catch(error => console.error('Error: ', error));
}
function getMovieRec(genre) {
var genre_id = getGenre(genre);
fetch('https://api.themoviedb.org/3/discover/movie?api_key=9f81f98dda461876ca00dc09fc4226ac&sort_by=popularity.desc&primary_release_date.gt=2014', {
method: 'GET',
params: {
//api_key: '9f81f98dda461876ca00dc09fc4226ac',
language: 'en-US',
//sort_by: 'original_title.asc',
include_adult: 'false',
include_video: 'false',
page: 1,
with_genres: genre_id
}
}).then(response => response.json()).then(function(response){
console.log(response);
let random = Math.floor(Math.random() * 20);
document.getElementById("title").innerHTML = response.results[random].original_title;
document.getElementById("rating").innerHTML = response.results[random].vote_average;
getSite(response.results[random].id);
}).catch(error => console.error('Error: ', error));
}
document.getElementById('selections').addEventListener('change', function(){
if(document.getElementById('selections').value == 'Movies'){
document.getElementById('option2').style.display = "block";
document.getElementById('options').style.display = "none";
}
});
document.getElementById("movieButton").addEventListener('mouseup', function(){
if(document.getElementById('genreName').value){
getMovieRec(document.getElementById('genreName').value);
document.getElementById('option2').style.display = "none";
document.getElementById('info').style.display = "block";
}
});
document.getElementById('genreName').addEventListener("keyup", function(event) {
if (event.keyCode === 13 && document.getElementById('genreName').value) {
getMovieRec(document.getElementById('genreName').value);
document.getElementById('option2').style.display = "none";
document.getElementById('info').style.display = "block";
}
});