-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupcoming movies.js
150 lines (129 loc) · 6.8 KB
/
upcoming movies.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
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
upcomingMovies= document.getElementById('upcoming-grid');
const upcomingleftButton = document.getElementById('upcoming-left-button');
const upcomingrightButton = document.getElementById('upcoming-right-button');
// Scroll left
upcomingleftButton.addEventListener('click', () => {
upcomingMovies.scrollBy({ left: -300, behavior: 'smooth' });
});
// Scroll right
upcomingrightButton.addEventListener('click', () => {
upcomingMovies.scrollBy({ left: 300, behavior: 'smooth' });
});
window.addEventListener('DOMContentLoaded', function() {
const tmdbApiKey ='d6e256dc1cc661c0bf793767a74948df'; // Replace with your TMDb API key
const upcomingGrid = document.getElementById('upcoming-grid');
const upcomingLoadMoreBtn = document.getElementById('upcoming-load-more-btn');
let currentUpcomingPage = 1;
let isLoadingUpcoming = false;
// Fetch upcoming movies
async function fetchUpcomingMovies(page) {
const url = `https://api.themoviedb.org/3/movie/upcoming?api_key=${tmdbApiKey}&language=en-US&page=${page}`;
try {
const response = await fetch(url);
const data = await response.json();
if (!data || !data.results) {
console.error('Error: No results found in the response.');
return [];
}
return data.results;
} catch (error) {
console.error('Fetch error:', error);
return [];
}
}
// Fetch detailed movie information
async function fetchUpcomingMovieDetails(movieId) {
const url = `https://api.themoviedb.org/3/movie/${movieId}?api_key=${tmdbApiKey}&language=en-US&append_to_response=credits,videos,recommendations,similar`;
try {
const response = await fetch(url);
return await response.json();
} catch (error) {
console.error('Fetch error:', error);
return null;
}
}
// Display movie details in a card
function displayUpcomingMovie(movie, details) {
const movieElement = document.createElement('div');
movieElement.className = 'upcoming-card';
const genres = details.genres?.map(g => g.name).join(', ') || 'N/A';
const cast = details.credits?.cast?.slice(0, 5)?.map(c => c.name).join(', ') || 'N/A';
const trailer = details.videos?.results?.find(v => v.type === 'Trailer');
const overview = movie.overview || 'No overview available';
const runtime = details.runtime ? `${details.runtime} minutes` : 'N/A';
const language = details.original_language ? details.original_language.toUpperCase() : 'N/A';
const tagline = details.tagline || 'N/A';
const productionCompanies = details.production_companies?.map(c => c.name).join(', ') || 'N/A';
const productionCountries = details.production_countries?.map(c => c.name).join(', ') || 'N/A';
const budget = details.budget ? `$${details.budget.toLocaleString()}` : 'N/A';
const revenue = details.revenue ? `$${details.revenue.toLocaleString()}` : 'N/A';
const homepage = details.homepage ? `<a href="${details.homepage}" target="_blank">Official Website</a>` : 'N/A';
const keywords = details.keywords?.results?.map(k => k.name).join(', ') || 'N/A';
movieElement.innerHTML = `
<div class="upcoming-card-inner">
<div class="upcoming-card-front">
<p class="title">Title🎬: ${movie.title} (${movie.release_date.split('-')[0]})</p>
<img src="https://image.tmdb.org/t/p/w300${movie.poster_path}" alt="${movie.title}">
</div>
<div class="upcoming-card-back">
<h3 style="color:red">Title🎬: ${movie.title} (${movie.release_date.split('-')[0]})</h3>
<p style="color:yellow"><strong>Vote Count:</strong> ${details.vote_count || 'N/A'}</p>
<p style="color:red"><strong>Status:</strong> ${details.status || 'N/A'}</p>
<p style="color:yellow"><strong>Ratings⭐:</strong> ${details.vote_average || 'N/A'}</p>
<p style="color:lightblue;"><strong>Genres:</strong> ${genres}</p>
<p><strong>Cast:</strong> ${cast}</p>
<p><strong>Runtime:</strong> ${runtime}</p>
<p><strong>Language:</strong> ${language}</p>
<p><strong>Tagline:</strong> "${tagline}"</p>
<p><strong>Overview:</strong> ${overview}</p>
<p><strong>Production Companies:</strong> ${productionCompanies}</p>
<p><strong>Production Countries:</strong> ${productionCountries}</p>
<p><strong>Budget:</strong> ${budget}</p>
<p><strong>Revenue:</strong> ${revenue}</p>
<p><strong>Popularity:</strong> ${details.popularity || 'N/A'}</p>
<p><strong>Keywords:</strong> ${keywords}</p>
${trailer ? `<a href="https://www.youtube.com/watch?v=${trailer.key}" target="_blank">Watch Trailer</a>` : ''}
<br>${homepage}
</div>
</div>
`;
upcomingGrid.appendChild(movieElement);
// Add flip effect
movieElement.addEventListener('click', () => {
movieElement.classList.toggle('flip');
});
}
// Display upcoming movies
async function displayUpcomingMovies() {
if (isLoadingUpcoming) return; // Prevent multiple fetches
isLoadingUpcoming = true;
const upcomingList = await fetchUpcomingMovies(currentUpcomingPage);
if (upcomingList.length === 0) {
console.log('No more movies found.');
isLoadingUpcoming = false;
return;
}
for (const movie of upcomingList) {
const movieDetails = await fetchUpcomingMovieDetails(movie.id);
if (movieDetails) {
displayUpcomingMovie(movie, movieDetails);
} else {
console.log(`Error fetching details for movie with ID ${movie.id}`);
}
}
currentUpcomingPage++; // Increment the page counter for the next fetch
isLoadingUpcoming = false;
}
// Infinite scrolling
window.addEventListener('scroll', () => {
if (window.innerHeight + window.scrollY >= document.body.offsetHeight - 2) {
displayUpcomingMovies();
}
});
// Initial fetch
displayUpcomingMovies();
// Load more button
upcomingLoadMoreBtn.addEventListener('click', () => {
displayUpcomingMovies();
});
});