-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
248 lines (206 loc) · 8.33 KB
/
script.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
function refreshPage() {
location.reload(); // This reloads the page
}
const API_KEY = '71bfde29fd3664879c4993e76a919e8a';
const API_URL = 'https://api.themoviedb.org/3';
const GENRES = {
action: 28,
drama: 18,
romance: 10749,
scifi: 878,
thriller: 53,
horror: 27,
family: 10751,
adult: 10770,
kids: 16,
animation: 16
};
let currentPage = 1;
async function fetchMoviesByGenre(genreId, page = 1) {
try {
const response = await fetch(`${API_URL}/discover/movie?api_key=${API_KEY}&with_genres=${genreId}&page=${page}`);
const data = await response.json();
return data.results;
} catch (error) {
console.error("Error fetching movies by genre:", error);
return [];
}
}
async function fetchRelatedMovies(movieId) {
const creditsResponse = await fetch(`${API_URL}/movie/${movieId}/credits?api_key=${API_KEY}`);
const credits = await creditsResponse.json();
const actorIds = credits.cast.slice(0, 3).map(actor => actor.id).join(',');
const relatedResponse = await fetch(`${API_URL}/discover/movie?api_key=${API_KEY}&with_cast=${actorIds}`);
const relatedMovies = await relatedResponse.json();
return relatedMovies.results;
}
async function fetchActorId(actorName) {
try {
const response = await fetch(`${API_URL}/search/person?api_key=${API_KEY}&query=${encodeURIComponent(actorName)}`);
const data = await response.json();
if (data.results.length > 0) {
return data.results[0].id; // Return the ID of the first matching actor
} else {
return null; // No actor found
}
} catch (error) {
console.error("Error fetching actor ID:", error);
return null;
}
}
async function fetchMoviesByActor(actorId) {
try {
const response = await fetch(`${API_URL}/discover/movie?api_key=${API_KEY}&with_cast=${actorId}`);
const data = await response.json();
return data.results;
} catch (error) {
console.error("Error fetching movies by actor:", error);
return [];
}
}
async function displayGenres() {
const container = document.getElementById('genres-container');
for (const [genreName, genreId] of Object.entries(GENRES)) {
const genreSection = document.createElement('div');
genreSection.classList.add('genre-section');
const genreTitle = document.createElement('h2');
genreTitle.innerText = capitalize(genreName);
genreSection.appendChild(genreTitle);
const carousel = document.createElement('div');
carousel.classList.add('carousel');
// Fetch and display movies for the genre
const movies = await fetchMoviesByGenre(genreId);
movies.forEach(movie => {
const movieCard = document.createElement('div');
movieCard.classList.add('movie-card');
movieCard.innerHTML = `
<img src="https://image.tmdb.org/t/p/w500${movie.poster_path}" alt="${movie.title}">
<h3>${movie.title}</h3>
`;
movieCard.addEventListener('click', () => {
displayMovieDetails(movie.id);
});
carousel.appendChild(movieCard);
});
// More button
const moreButton = document.createElement('div');
moreButton.classList.add('more-button');
moreButton.innerText = 'More';
moreButton.addEventListener('click', async () => {
currentPage++;
const moreMovies = await fetchMoviesByGenre(genreId, currentPage);
moreMovies.forEach(movie => {
const movieCard = document.createElement('div');
movieCard.classList.add('movie-card');
movieCard.innerHTML = `
<img src="https://image.tmdb.org/t/p/w500${movie.poster_path}" alt="${movie.title}">
<h3>${movie.title}</h3>
`;
movieCard.addEventListener('click', () => {
displayMovieDetails(movie.id);
});
carousel.appendChild(movieCard);
});
});
genreSection.appendChild(carousel);
genreSection.appendChild(moreButton);
container.appendChild(genreSection);
}
}
async function displayMovieDetails(movieId) {
const detailsDiv = document.getElementById('movie-details');
const moviePoster = document.getElementById('movie-poster');
const movieTitle = document.getElementById('movie-title');
const movieOverview = document.getElementById('movie-overview');
const movieReleaseDate = document.getElementById('movie-release-date');
const movieRating = document.getElementById('movie-rating');
const relatedMoviesContainer = document.getElementById('related-movies');
const backButton = document.getElementById('back-button');
const genresContainer = document.getElementById('genres-container');
const searchContainer = document.querySelector('.search-bar-container');
genresContainer.style.opacity = '0';
setTimeout(async () => {
const response = await fetch(`${API_URL}/movie/${movieId}?api_key=${API_KEY}`);
const movie = await response.json();
relatedMoviesContainer.innerHTML = '';
moviePoster.src = `https://image.tmdb.org/t/p/w500${movie.poster_path}`;
movieTitle.innerText = movie.title;
movieOverview.innerText = movie.overview;
movieReleaseDate.innerText = movie.release_date;
movieRating.innerText = movie.vote_average;
const relatedMovies = await fetchRelatedMovies(movieId);
const relatedMoviesHeading = document.createElement('h3');
relatedMoviesHeading.innerText = 'Related Movies';
relatedMoviesContainer.appendChild(relatedMoviesHeading);
const relatedMoviesCarousel = document.createElement('div');
relatedMoviesCarousel.classList.add('carousel');
relatedMovies.forEach(relatedMovie => {
const relatedMovieCard = document.createElement('div');
relatedMovieCard.classList.add('movie-card');
relatedMovieCard.innerHTML = `
<img src="https://image.tmdb.org/t/p/w500${relatedMovie.poster_path}" alt="${relatedMovie.title}">
<h3>${relatedMovie.title}</h3>
`;
relatedMovieCard.addEventListener('click', () => {
displayMovieDetails(relatedMovie.id);
});
relatedMoviesCarousel.appendChild(relatedMovieCard);
});
relatedMoviesContainer.appendChild(relatedMoviesCarousel);
detailsDiv.style.display = 'block';
detailsDiv.style.opacity = '1';
detailsDiv.style.transition = 'opacity 0.5s ease';
backButton.style.display = 'block';
genresContainer.style.display = 'none';
}, 500);
}
document.getElementById('back-button').addEventListener('click', () => {
const detailsDiv = document.getElementById('movie-details');
const genresContainer = document.getElementById('genres-container');
const backButton = document.getElementById('back-button');
detailsDiv.style.opacity = '0';
setTimeout(() => {
detailsDiv.style.display = 'none';
genresContainer.style.display = 'block';
genresContainer.style.opacity = '1';
backButton.style.display = 'none';
}, 500);
});
document.getElementById('search-button').addEventListener('click', async () => {
const query = document.getElementById('search-bar').value;
if (query) {
const container = document.getElementById('genres-container');
container.innerHTML = '';
const response = await fetch(`${API_URL}/search/movie?api_key=${API_KEY}&query=${encodeURIComponent(query)}`);
const searchResults = await response.json();
displaySearchResults(searchResults.results, 'Search Results for: ' + query);
}
});
function displaySearchResults(movies, title) {
const container = document.getElementById('genres-container');
const searchResultsSection = document.createElement('div');
searchResultsSection.classList.add('genre-section');
const searchResultsTitle = document.createElement('h2');
searchResultsTitle.innerText = title;
searchResultsSection.appendChild(searchResultsTitle);
const searchResultsCarousel = document.createElement('div');
searchResultsCarousel.classList.add('carousel');
movies.forEach(movie => {
const movieCard = document.createElement('div');
movieCard.classList.add('movie-card');
movieCard.innerHTML = `
<img src="https://image.tmdb.org/t/p/w500${movie.poster_path}" alt="${movie.title}">
<h3>${movie.title}</h3>
`;
movieCard.addEventListener('click', () => {
displayMovieDetails(movie.id);
});
searchResultsCarousel.appendChild(movieCard);
});
searchResultsSection.appendChild(searchResultsCarousel);
container.appendChild(searchResultsSection);
}
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
displayGenres();