-
Notifications
You must be signed in to change notification settings - Fork 0
/
vercel.js
204 lines (154 loc) · 6.15 KB
/
vercel.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
import express from "express";
import axios from "axios";
import axiosRetry from 'axios-retry';
import ejs from "ejs";
import path from "path";
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
axiosRetry(axios, { retries: 3 });
import {} from 'dotenv/config';
const app = express();
const port = 3000;
// const hostname = "192.168.146.4";
const api_key_auth = process.env.apikeyauth;
const access_token_auth = process.env.accesstokenauth;
let baseimgURL = 'https://image.tmdb.org/t/p/w500';
// app.use(express.static("public"));
//Changes for vercel deployment
// Require static assets from public folder
app.use(express.static(path.join(__dirname, 'public')));
// Set 'views' directory for any views
// being rendered res.render()
app.set('views', path.join(__dirname, 'views'));
app.get("/", async (req,res) => {
// Method 1 : Angela Yu
console.log("Fetching API");
try{
const response4 = await axios.get("https://api.themoviedb.org/3/movie/upcoming?language=en-US&page=1" , {
timeout: 1000,
headers: {
accept: 'application/json',
Authorization: `Bearer ${access_token_auth}`
}
});
const hindidata = await response4.data;
const hindimovie = await hindidata.results;
console.log("API 1 fetched successfully");
const response1 = await axios.get("https://api.themoviedb.org/3/movie/now_playing?language=en-US&page=1" , {
timeout: 1000,
headers: {
accept: 'application/json',
Authorization: `Bearer ${access_token_auth}`
}
});
const nowplayingdata = await response1.data;
const nowplayingmovie = await nowplayingdata.results;
console.log("API 2 fetched successfully");
// Method 2
// const options = {
// method: 'GET',
// url: 'https://api.themoviedb.org/3/movie/now_playing?language=en-US&page=1',
// headers: {
// accept: 'application/json',
// Authorization: 'Bearer eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIwYWExM2I2ZWU0N2Q0ZGFjMjE4ZjIyZTdhMmQzYjU4ZiIsInN1YiI6IjY1OGRiOWRmOGVkMDNmNWU4MDkzZjcwNiIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.EN9Lg9h_JqSPfjmYYYCTir5S8664u_3moUjojoauqI0'
// }
// };
// axios
// .request(options)
// .then(function (response) {
// console.log(response.data);
// })
// .catch(function (error) {
// console.error(error);
// });
// console.log(data);
const response2 = await axios.get("https://api.themoviedb.org/3/movie/top_rated?language=en-US&page=1" , {
timeout: 1000,
headers: {
accept: 'application/json',
Authorization: `Bearer ${access_token_auth}`
}
});
const populardata = await response2.data;
const popularmovie = await populardata.results;
// console.log(nowplayingdata[0]);
console.log("API 3 fetched successfully");
// console.log(moviedata[0]);
res.render("index.ejs" , {hi : hindimovie , p : popularmovie , np : nowplayingmovie , baseimgURL: baseimgURL});
console.log("Get request accepted");
} catch(error){
res.send("Sorry , we are getting some error with API now ...")
console.log("Sorry , we are getting some type of error .......");
console.error(error);
}
});
console.log("Fetching of APIs and dispalying them done......")
app.get("/movie/:movieid" , async (req,res) => {
let movieid = req.params.movieid;
try {
console.time("API request");
let response;
let castapi;
[response,castapi] = await Promise.all([
axios.get(`https://api.themoviedb.org/3/movie/${movieid}?api_key=${api_key_auth}`),
axios.get(`https://api.themoviedb.org/3/movie/${movieid}/credits?language=en-US` , {
timeout: 1000,
headers: {
accept: 'application/json',
Authorization: `Bearer ${access_token_auth}`
}
})
]);
let data = await response.data;
let castdata = await castapi.data;
console.timeEnd("API request");
res.render("movie.ejs" , {data : data , baseimgURL: baseimgURL , castdata : castdata});
for (let i=0 ; i< castdata.cast.length ; i++){
console.log(castdata.cast[i].name);
}
console.log(castdata.cast.length);
}catch(error){
console.error(error);
// console.error(error);
}
});
app.get("/cast/:cid/:cname" , async (req,res) => {
let person_id = req.params.cid;
let person_name = req.params.cname;
try {
// API request
const response = await axios.get(`https://api.themoviedb.org/3/person/${person_id}/movie_credits` , {
timeout: 1000,
headers: {
accept: 'application/json',
Authorization: `Bearer ${access_token_auth}`
}
});
const result = await response.data;
// console.log(result);
res.render("cast.ejs", {hi : result , baseimgURL : baseimgURL , cname : person_name});
}catch(error){
console.error(error);
}
});
app.get("/category/:gid/:gname" , async (req,res) => {
let genreid = req.params.gid;
let genrename = req.params.gname;
try {
const response = await axios.get(`https://api.themoviedb.org/3/discover/movie?include_adult=false&include_video=false&language=en-US&page=1&sort_by=popularity.desc&with_genres=${genreid}` , {
timeout: 1000,
headers: {
accept: 'application/json',
Authorization: `Bearer ${access_token_auth}`
}
});
const result = await response.data;
res.render("genre.ejs" , {genre : result , baseimgURL : baseimgURL , gname : genrename });
}catch(error){
console.error(error);
}
});
app.listen(port , () => {
console.log("Server running in port 3000....");
});