-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (52 loc) · 1.85 KB
/
index.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
const axios = require("axios").default;
const cheerio = require("cheerio");
const cors = require("cors")({ origin: true });
const express = require("express");
const favicon = require("serve-favicon");
const path = require("path");
const app = express();
const port = process.env.PORT || 6005;
app.use(favicon(path.join(__dirname, "public", "favicon.ico")));
const scrapeIt = async (url) => {
try {
const { data } = await axios.get(url).catch((err) => {
return res.status(500).json({
error: err,
});
});
const selector = cheerio.load(data);
const link = url;
const title = selector(".title_wrapper > h1").text().trim();
const year = selector(".title_wrapper > h1 > span > a").text();
const poster = selector(".poster > a > img").attr("src").toString();
const rating = selector(".ratingValue > strong > span").text();
const duration = selector(".title_wrapper > div > time").text().trim();
const genre = selector(".title_wrapper > div > a").first().text();
const plot = selector(".plot_summary > .summary_text").text().trim();
return { link, title, year, rating, duration, genre, poster, plot };
} catch {
console.error(
`ERROR: An error occurred while trying to fetch the IMDb url: ${url}`
);
}
};
app.get("/", (req, res) => {
res.status(404).json({
error: "IMDb title ID required. Ex: http://localhost:6005/tt0145487",
});
});
app.get("/:title", (req, res) => {
cors(req, res, () => {
if (req.method !== "GET") {
return res.status(401).json({
message: "GET method allowed only.",
});
}
});
scrapeIt("https://www.imdb.com/title/" + req.params.title).then((data) => {
res.status(200).json(data);
});
});
app.listen(port, () => {
console.log(`Server is online at http://localhost:${port}`);
});