-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
199 lines (169 loc) · 7.1 KB
/
Copy pathscript.js
File metadata and controls
199 lines (169 loc) · 7.1 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
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
const API_KEY_OMDB = '8b9773fd';
const API_KEY_TMDB = '487df02639957e246b25f34c337a8375';
document.addEventListener("DOMContentLoaded", function () {
document.getElementById("darkModeToggle").addEventListener("click", toggleDarkMode);
loadFavorites();
});
/* Search for movies */
async function searchMovie() {
const query = document.getElementById('searchInput').value.trim();
if (!query) {
alert('Please enter a movie or TV show name!');
return;
}
const url = `https://www.omdbapi.com/?s=${query}&apikey=${API_KEY_OMDB}`;
try {
const response = await fetch(url);
const data = await response.json();
if (data.Response === 'True') {
displayMovies(data.Search);
} else {
document.getElementById('result').innerHTML = '<p>No movies found.</p>';
}
} catch (error) {
console.error("Error fetching movie data:", error);
}
}
/* Display movies with Trailer & Streaming Platform */
async function displayMovies(movies) {
const resultDiv = document.getElementById('result');
resultDiv.innerHTML = '';
for (const movie of movies) {
let movieDetails = await getMovieDetails(movie.imdbID);
let streamingPlatform = await getStreamingPlatform(movie.Title);
let trailerURL = await getMovieTrailer(movie.Title);
resultDiv.innerHTML += `
<div class="movie">
<img src="${movie.Poster !== 'N/A' ? movie.Poster : 'placeholder.jpg'}" alt="${movie.Title}">
<h3>${movie.Title} (${movie.Year})</h3>
<p>⭐ IMDb Rating: ${movieDetails.imdbRating || 'N/A'}</p>
<p>📺 Streaming on: ${streamingPlatform}</p>
<iframe class="trailer" src="${trailerURL}" frameborder="0" allowfullscreen></iframe>
<button onclick="addToFavorites('${movie.imdbID}', '${movie.Title}')">❤️ Add to Favorites</button>
</div>
`;
}
}
/* Fetch Trailer from TMDB */
async function getMovieTrailer(title) {
try {
const searchURL = `https://api.themoviedb.org/3/search/movie?api_key=${API_KEY_TMDB}&query=${encodeURIComponent(title)}`;
const searchResponse = await fetch(searchURL);
const searchData = await searchResponse.json();
if (searchData.results.length > 0) {
const movieId = searchData.results[0].id;
const trailerURL = `https://api.themoviedb.org/3/movie/${movieId}/videos?api_key=${API_KEY_TMDB}`;
const trailerResponse = await fetch(trailerURL);
const trailerData = await trailerResponse.json();
if (trailerData.results.length > 0) {
return `https://www.youtube.com/embed/${trailerData.results[0].key}`;
}
}
} catch (error) {
console.error("Error fetching trailer:", error);
}
return "";
}
/* Fetch Streaming Platform Availability */
async function getStreamingPlatform(title) {
try {
const url = `https://api.themoviedb.org/3/search/movie?api_key=${API_KEY_TMDB}&query=${encodeURIComponent(title)}`;
const response = await fetch(url);
const data = await response.json();
if (data.results.length > 0) {
const movieId = data.results[0].id;
const platformURL = `https://api.themoviedb.org/3/movie/${movieId}/watch/providers?api_key=${API_KEY_TMDB}`;
const platformResponse = await fetch(platformURL);
const platformData = await platformResponse.json();
if (platformData.results && platformData.results.US) {
return platformData.results.US.flatrate?.[0]?.provider_name || "Not Available";
}
}
} catch (error) {
console.error("Error fetching streaming platform:", error);
}
return "Not Available";
}
/* Get Detailed Movie Info (Ratings, Genre) */
async function getMovieDetails(imdbID) {
try {
const url = `https://www.omdbapi.com/?i=${imdbID}&apikey=${API_KEY_OMDB}`;
const response = await fetch(url);
return await response.json();
} catch (error) {
console.error("Error fetching movie details:", error);
}
return {};
}
/* Add movie to favorites */
function addToFavorites(imdbID, title) {
let favorites = JSON.parse(localStorage.getItem('favorites')) || [];
if (!favorites.some(movie => movie.id === imdbID)) {
favorites.push({ id: imdbID, title: title });
localStorage.setItem('favorites', JSON.stringify(favorites));
loadFavorites();
} else {
alert("Already in favorites!");
}
}
/* Remove from favorites */
function removeFromFavorites(imdbID) {
let favorites = JSON.parse(localStorage.getItem('favorites')) || [];
favorites = favorites.filter(movie => movie.id !== imdbID);
localStorage.setItem('favorites', JSON.stringify(favorites));
loadFavorites();
}
/* Load Favorites */
function loadFavorites() {
const favoritesList = document.getElementById("favoritesList");
favoritesList.innerHTML = "";
let favorites = JSON.parse(localStorage.getItem('favorites')) || [];
favorites.forEach(movie => {
const li = document.createElement("li");
li.innerHTML = `${movie.title} <button onclick="removeFromFavorites('${movie.id}')">🗑️</button>`;
favoritesList.appendChild(li);
});
}
/* "Surprise Me!" - Get a Random Movie */
async function getRandomMovie() {
const randomMovies = ["Inception", "Interstellar", "Titanic", "The Dark Knight", "Avatar"];
const randomQuery = randomMovies[Math.floor(Math.random() * randomMovies.length)];
const url = `https://www.omdbapi.com/?t=${randomQuery}&apikey=${API_KEY_OMDB}`;
try {
const response = await fetch(url);
const data = await response.json();
if (data.Response === 'True') {
document.getElementById('result').innerHTML = `
<div class="movie">
<img src="${data.Poster !== 'N/A' ? data.Poster : 'placeholder.jpg'}" alt="${data.Title}">
<h3>${data.Title} (${data.Year})</h3>
<p>🎭 Genre: ${data.Genre}</p>
<p>⭐ IMDb Rating: ${data.imdbRating}</p>
<button onclick="addToFavorites('${data.imdbID}', '${data.Title}')">❤️ Add to Favorites</button>
</div>
`;
} else {
document.getElementById('result').innerHTML = '<p>No movie found.</p>';
}
} catch (error) {
console.error("Error fetching random movie:", error);
}
}
/* Voice Search */
function startVoiceSearch() {
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
recognition.lang = "en-US";
recognition.onresult = function (event) {
const transcript = event.results[0][0].transcript;
document.getElementById("searchInput").value = transcript;
searchMovie();
};
recognition.onerror = function () {
alert("Voice recognition failed. Please try again.");
};
recognition.start();
}
/* Toggle Dark Mode */
function toggleDarkMode() {
document.body.classList.toggle("dark-mode");
}