-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
182 lines (171 loc) · 5.18 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PlayBox Movies</title>
<style>
body {
margin: 0;
font-family: Arial, sans-serif;
background-color: #141414;
color: white;
}
header {
padding: 20px;
text-align: center;
background-color: #1c1c1c;
}
header h1 {
margin: 0;
}
#search-bar {
margin: 20px auto;
display: block;
width: 80%;
max-width: 600px;
padding: 10px;
font-size: 1rem;
border-radius: 5px;
border: none;
outline: none;
}
#movie-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
gap: 10px;
padding: 20px;
}
.movie {
position: relative;
cursor: pointer;
}
.movie img {
width: 100%;
border-radius: 5px;
}
.movie-title {
position: absolute;
bottom: 0;
left: 0;
right: 0;
background-color: rgba(0, 0, 0, 0.7);
padding: 5px;
text-align: center;
font-size: 0.9rem;
}
#movie-details {
display: none;
padding: 20px;
}
#movie-details img {
max-width: 100%;
margin-bottom: 10px;
}
#back-button {
margin-top: 20px;
cursor: pointer;
background-color: #e50914;
color: white;
border: none;
padding: 10px 20px;
font-size: 1rem;
}
</style>
</head>
<body>
<header>
<h1>PlayBox Movies</h1>
<input type="text" id="search-bar" placeholder="Search for movies...">
</header>
<div id="movie-grid"></div>
<div id="movie-details">
<button id="back-button">Back to Movies</button>
<div id="details-content"></div>
</div>
<script>
const API_KEY = '746d75472f6a22fc92b3bdbb6eabd982';
const BASE_URL = 'https://api.themoviedb.org/3';
const IMAGE_URL = 'https://image.tmdb.org/t/p/w500';
const movieGrid = document.getElementById('movie-grid');
const movieDetails = document.getElementById('movie-details');
const detailsContent = document.getElementById('details-content');
const backButton = document.getElementById('back-button');
const searchBar = document.getElementById('search-bar');
let currentPage = 1;
let isLoading = false;
let currentQuery = '';
async function fetchMovies(page = 1) {
isLoading = true;
const url = currentQuery
? `${BASE_URL}/search/movie?api_key=${API_KEY}&query=${encodeURIComponent(currentQuery)}&page=${page}`
: `${BASE_URL}/movie/popular?api_key=${API_KEY}&page=${page}`;
const response = await fetch(url);
const data = await response.json();
displayMovies(data.results);
isLoading = false;
}
function displayMovies(movies) {
movies.forEach(movie => {
const movieElement = document.createElement('div');
movieElement.classList.add('movie');
movieElement.innerHTML = `
<img src="${IMAGE_URL}${movie.poster_path}" alt="${movie.title}">
<div class="movie-title">${movie.title}</div>
`;
movieElement.addEventListener('click', () => fetchMovieDetails(movie.id));
movieGrid.appendChild(movieElement);
});
}
async function fetchMovieDetails(movieId) {
const response = await fetch(`${BASE_URL}/movie/${movieId}?api_key=${API_KEY}&append_to_response=videos,watch/providers`);
const movie = await response.json();
displayMovieDetails(movie);
}
function displayMovieDetails(movie) {
const trailer = movie.videos.results.find(video => video.type === 'Trailer');
const providers = movie['watch/providers'].results.US?.flatrate || [];
const streamingLinks = providers.length
? providers.map(provider => `<p>${provider.provider_name}</p>`).join('')
: 'No streaming options available.';
movieDetails.style.display = 'block';
movieGrid.style.display = 'none';
detailsContent.innerHTML = `
<h2>${movie.title}</h2>
<img src="${IMAGE_URL}${movie.poster_path}" alt="${movie.title}">
<p>${movie.overview}</p>
<p><strong>Release Date:</strong> ${movie.release_date}</p>
<p><strong>Rating:</strong> ${movie.vote_average}</p>
${
trailer
? `<iframe width="100%" height="315" src="https://www.youtube.com/embed/${trailer.key}" frameborder="0" allowfullscreen></iframe>`
: '<p>No trailer available</p>'
}
<h3>Watch Now:</h3>
${streamingLinks}
`;
}
backButton.addEventListener('click', () => {
movieDetails.style.display = 'none';
movieGrid.style.display = 'grid';
});
searchBar.addEventListener('input', (e) => {
currentQuery = e.target.value;
movieGrid.innerHTML = ''; // Clear previous results
currentPage = 1;
fetchMovies();
});
window.addEventListener('scroll', () => {
if (
window.innerHeight + window.scrollY >= document.body.offsetHeight - 100 &&
!isLoading
) {
currentPage++;
fetchMovies(currentPage);
}
});
// Initial fetch
fetchMovies();
</script>
</body>
</html>