-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
95 lines (89 loc) · 3.04 KB
/
api.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
const apiKey = "9d285b108d7eb06e4de4e16d20c92787";
async function fetchData(url) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error("Failed to fetch data.");
}
return await response.json();
} catch (error) {
console.error("Error fetching data:", error);
throw error;
}
}
export async function fetchTopRatedMovies() {
const url = `https://api.themoviedb.org/3/movie/top_rated?api_key=${apiKey}&language=en-US&page=1`;
try {
const data = await fetchData(url);
const topRatedMovies = [];
const numMovies = Math.min(data.results.length, 10);
for (let i = 0; i < numMovies; i++) {
topRatedMovies.push(data.results[i]);
}
return topRatedMovies;
} catch (error) {
throw new Error("Failed to fetch top rated movies.");
}
}
export async function fetchPopularMovies() {
const url = `https://api.themoviedb.org/3/movie/popular?api_key=${apiKey}&language=en-US&page=1`;
try {
const data = await fetchData(url);
const popularMovies = [];
for (let i = 0; i < 10 && i < data.results.length; i++) {
popularMovies.push(data.results[i]);
}
return popularMovies;
} catch (error) {
throw new Error("Failed to fetch popular movies.");
}
}
export async function searchMovies(query) {
const url = `https://api.themoviedb.org/3/search/movie?api_key=${apiKey}&query=${query}&language=en-US&page=1`;
try {
const data = await fetchData(url);
return data.results;
} catch (error) {
throw new Error("Failed to search for movies.");
}
}
export async function searchTVShows(query) {
const url = `https://api.themoviedb.org/3/search/tv?api_key=${apiKey}&query=${query}&language=en-US&page=1`;
try {
const data = await fetchData(url);
return data.results;
} catch (error) {
throw new Error("Failed to search for TV shows.");
}
}
export async function searchPeople(query) {
const url = `https://api.themoviedb.org/3/search/person?api_key=${apiKey}&query=${query}&language=en-US&page=1`;
try {
const data = await fetchData(url);
return data.results;
} catch (error) {
throw new Error("Failed to search for people.");
}
}
// fetch a random movie
export async function fetchRandomMovie() {
const url = `https://api.themoviedb.org/3/discover/movie?api_key=${apiKey}&language=en-US&sort_by=release_date.desc,popularity.desc&page=1`;
try {
const data = await fetchData(url);
const randomIndex = Math.floor(Math.random() * data.results.length);
return data.results[randomIndex];
} catch (error) {
throw new Error("Failed to fetch random movie.");
}
}
// fetch a random movie backdrop image
export async function fetchRandomBackdropImage() {
try {
const randomMovie = await fetchRandomMovie();
const backdropPath = randomMovie.backdrop_path;
return `https://image.tmdb.org/t/p/original${backdropPath}`;
} catch (error) {
console.error("Failed to fetch random backdrop image:", error);
throw error;
}
}