-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrendingmovies.js
88 lines (78 loc) · 3.63 KB
/
trendingmovies.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
88
const trendingmoviebtns= document.getElementById('unique-movies-grid');
const trendingmovieleftbutton = document.getElementById('trendingmovie-left-button');
const trendingmovierightbutton = document.getElementById('trendingmovie-right-button');
// Scroll left
trendingmovieleftbutton.addEventListener('click', () => {
trendingmoviebtns.scrollBy({ left: -300, behavior: 'smooth' });
});
// Scroll right
trendingmovierightbutton.addEventListener('click', () => {
trendingmoviebtns.scrollBy({ left: 300, behavior: 'smooth' });
});
const apiKey ='d6e256dc1cc661c0bf793767a74948df'; // Replace with your TMDB API key
const uniqueMoviesGrid = document.getElementById('unique-movies-grid');
let allMovies = []; // To store all fetched movies
document.addEventListener('DOMContentLoaded', fetchTrendingMovies);
function fetchTrendingMovies() {
const url = `https://api.themoviedb.org/3/trending/movie/week?api_key=${apiKey}&append_to_response=credits`;
fetch(url)
.then(response => response.json())
.then(data => {
allMovies = data.results;
displayMovies(allMovies); // Display all movies
})
.catch(error => console.error('Error fetching movies:', error));
}
function displayMovies(movies) {
movies.forEach(movie => {
const genres = movie.genre_ids.map(id => getUniqueGenreName(id)).join(', ');
const movieCard = document.createElement('div');
movieCard.classList.add('unique-movie-flip-card');
movieCard.innerHTML = `
<div class="unique-movie-card-inner">
<div class="unique-movie-front">
<img src="https://image.tmdb.org/t/p/w500${movie.poster_path}" alt="${movie.title}">
</div>
<div class="unique-movie-back">
<h3>${movie.title} [${new Date(movie.release_date).getFullYear()}]</h3>
<p>Rating ⭐️: ${movie.vote_average} / 10</p>
<p>${movie.vote_average} based on ${movie.vote_count} user ratings</p>
<p>Release Info: ${movie.release_date}</p>
<p>Genre: ${genres}</p>
<p>Story Line: ${movie.overview}</p>
<a href="https://www.themoviedb.org/movie/${movie.id}" target="_blank">Read More</a>
</div>
</div>
`;
uniqueMoviesGrid.appendChild(movieCard);
// Add flip effect on click
movieCard.addEventListener('click', () => {
movieCard.classList.toggle('flip');
});
});
}
// Utility function to convert genre IDs to names
function getUniqueGenreName(id) {
const genreMap = {
28: 'Action',
12: 'Adventure',
16: 'Animation',
35: 'Comedy',
80: 'Crime',
99: 'Documentary',
18: 'Drama',
10751: 'Family',
14: 'Fantasy',
36: 'History',
27: 'Horror',
10402: 'Music',
9648: 'Mystery',
10749: 'Romance',
878: 'Science Fiction',
10770: 'TV Movie',
53: 'Thriller',
10752: 'War',
37: 'Western',
};
return genreMap[id] || 'Unknown';
}