-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
65 lines (50 loc) · 1.92 KB
/
Copy pathscript.js
File metadata and controls
65 lines (50 loc) · 1.92 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
const API_KEY = "ec3ae1d56cde65b04559cc8d18863981";
const BASE_URL = "https://api.themoviedb.org/3";
const IMG_URL = "https://image.tmdb.org/t/p/w500";
async function loadHeroMovie() {
const res = await fetch(`${BASE_URL}/trending/movie/day?api_key=${API_KEY}`);
const data = await res.json();
const randomIndex = Math.floor(Math.random() * data.results.length);
const movie = data.results[randomIndex];
document.querySelector(".hero-section").style.backgroundImage =
`linear-gradient(to right, black, transparent),
url(https://image.tmdb.org/t/p/w1280${movie.backdrop_path})`;
document.getElementById("hero-title").innerText = movie.title;
document.getElementById("hero-desc").innerText = movie.overview;
document.getElementById("hero-rating").innerText = "★ " + movie.vote_average;
document.getElementById("hero-year").innerText = movie.release_date.slice(0, 4);
}
async function loadMovies(endpoint, containerId) {
const res = await fetch(`${BASE_URL}${endpoint}?api_key=${API_KEY}`);
const data = await res.json();
const container = document.getElementById(containerId);
container.innerHTML = "";
data.results.forEach(movie => {
if (!movie.poster_path) return;
const img = document.createElement("img");
img.src = IMG_URL + movie.poster_path;
img.classList.add("movie-card");
img.onclick = () => playTrailer(movie.id);
container.appendChild(img);
});
}
async function playTrailer(movieId) {
const res = await fetch(
`${BASE_URL}/movie/${movieId}/videos?api_key=${API_KEY}`
);
const data = await res.json();
const trailer = data.results.find(
v => v.type === "Trailer" && v.site === "YouTube"
);
if (!trailer) {
alert("Trailer not available");
return;
}
window.open(
`https://www.youtube.com/watch?v=${trailer.key}`,
"_blank"
);
}
loadHeroMovie();
loadMovies("/trending/movie/week", "trending");
loadMovies("/movie/top_rated", "top-rated");