-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
119 lines (104 loc) · 3 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
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
const express = require("express");
const cors = require("cors");
const axios = require("axios");
const cheerio = require("cheerio");
const bodyParser = require("body-parser");
const dotenv = require("dotenv");
const url = "https://kimetsu-no-yaiba.fandom.com/wiki/Kimetsu_no_Yaiba_Wiki";
const characterUrl = "https://kimetsu-no-yaiba.fandom.com/wiki/";
//SET UP
const app = express();
app.use(bodyParser.json({ limit: "50mb" }));
app.use(cors());
dotenv.config();
app.use(
bodyParser.urlencoded({
limit: "50mb",
extended: true,
parameterLimit: 50000,
})
);
//ROUTES
//GET ALL CHARACTERS
app.get("/v1", (req, resp) => {
const thumbnails = [];
const limit = Number(req.query.limit);
try {
axios(url).then((res) => {
const html = res.data;
const $ = cheerio.load(html);
$(".portal", html).each(function () {
const name = $(this).find("a").attr("title");
const url = $(this).find("a").attr("href");
const image = $(this).find("a > img").attr("data-src");
thumbnails.push({
name: name,
url: "https://demon-slayer-api.onrender.com/v1" + url.split("/wiki")[1],
image: image,
});
});
if (limit && limit > 0) {
resp.status(200).json(thumbnails.slice(0, limit));
} else {
resp.status(200).json(thumbnails);
}
});
} catch (err) {
resp.status(500).json(err);
}
});
//GET A CHARACTER
app.get("/v1/:character", (req, resp) => {
let url = characterUrl + req.params.character;
const titles = [];
const details = [];
const galleries = [];
const characters = [];
const characterObj = {};
try {
axios(url).then((res) => {
const html = res.data;
const $ = cheerio.load(html);
//Get gallery
$(".wikia-gallery-item",html).each(function(){
const gallery = $(this).find("a > img").attr("data-src");
galleries.push(gallery);
})
$("aside", html).each(function () {
// Get banner image
const image = $(this).find("img").attr("src");
//Get the title of character title
$(this)
.find("section > div > h3")
.each(function () {
titles.push($(this).text());
});
// Get character details
$(this)
.find("section > div > div")
.each(function () {
details.push($(this).text());
});
if (image !== undefined) {
// Create object with title as key and detail as value
for (let i = 0; i < titles.length; i++) {
characterObj[titles[i].toLowerCase()] = details[i];
}
characters.push({
name: req.params.character.replace("_"," "),
gallery: galleries,
image: image,
...characterObj,
});
}
});
resp.status(200).json(characters);
});
} catch (err) {
resp.status(500).json(err);
}
});
// RUN PORT
app.listen(process.env.PORT || 8000, () => {
console.log("Server is running...");
});