-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
141 lines (129 loc) · 4.67 KB
/
Copy pathindex.js
File metadata and controls
141 lines (129 loc) · 4.67 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
const express = require("express");
const axios = require("axios");
const path = require("path");
const fs = require("fs");
const app = express();
const xml2js = require("xml2js");
const cheerio = require("cheerio");
const port = 10000;
const parseString = require("xml2js").parseString;
const tagCache = new Map();
app.use(express.json());
app.get("/api/search", async (req, res) => {
const tags = req.query.q || "";
const page = parseInt(req.query.p || 0);
const id = req.query.id || "";
const type = req.query.t || "post";
try {
let data;
if (type == "tags") {
// Thanks for the unofficial api endpoint guys :3
const response = await axios.get(
`https://api.rule34.xxx/autocomplete.php?q=${tags}&api_key=e08211f99626513cef071b7cd9de37956f14591672d36bb973583c0101d2f98e2b28399a43ec563dd6d7fa25357d6d12c945ac2ed145c5679cbe632425e963d1&user_id=5625142`
);
data = response.data;
} else {
if (id) {
const [postRes, commentRes] = await Promise.all([
axios.get(
`https://api.rule34.xxx/index.php?page=dapi&s=post&q=index&id=${id}&json=1&api_key=e08211f99626513cef071b7cd9de37956f14591672d36bb973583c0101d2f98e2b28399a43ec563dd6d7fa25357d6d12c945ac2ed145c5679cbe632425e963d1&user_id=5625142`
),
axios.get(
`https://api.rule34.xxx/index.php?page=dapi&s=comment&q=index&post_id=${id}&api_key=e08211f99626513cef071b7cd9de37956f14591672d36bb973583c0101d2f98e2b28399a43ec563dd6d7fa25357d6d12c945ac2ed145c5679cbe632425e963d1&user_id=5625142`
),
]);
data = postRes.data[0];
const comments = await xmlToJson(commentRes.data);
if (comments && comments.comments) {
data.comments = comments.comments.comment;
}
} else {
const response = await axios.get(
`https://api.rule34.xxx/index.php?page=dapi&s=post&q=index&tags=${tags}&pid=${page}&json=1&api_key=e08211f99626513cef071b7cd9de37956f14591672d36bb973583c0101d2f98e2b28399a43ec563dd6d7fa25357d6d12c945ac2ed145c5679cbe632425e963d1&user_id=5625142`
);
data = response.data;
}
}
res.json(data);
} catch (error) {
res.status(500).json({ error: "Failed to fetch post" });
}
});
app.get("/api/download/:id", async (req, res) => {
const id = req.params.id;
try {
const response = await axios.get(
`https://api.rule34.xxx/index.php?page=dapi&s=post&q=index&id=${id}&json=1&api_key=e08211f99626513cef071b7cd9de37956f14591672d36bb973583c0101d2f98e2b28399a43ec563dd6d7fa25357d6d12c945ac2ed145c5679cbe632425e963d1&user_id=5625142`
);
const data = response.data[0];
const imageData = await axios.get(data.file_url, {
responseType: "arraybuffer",
});
const image = Buffer.from(imageData.data, "binary");
fs.writeFile(
path.join(__dirname, "/site/downloads", `${id}.png`),
image,
(err) => {
if (err) {
console.error(err);
res.status(500).send("An error occurred");
} else {
res.sendFile(path.join(__dirname, "/site/downloads", `${id}.png`));
}
}
);
setTimeout(() => {
fs.unlink(path.join(__dirname, "/site/downloads", `${id}.png`), (err) => {
if (err) {
console.error(err);
}
});
}, 10000);
} catch (error) {
res.status(500).send("An error occurred");
}
});
app.get(
["/loadouts", "/loadouts/create", "/loadout/:id/edit", "/loadout/:id"],
async (req, res) => {
const route = req.path;
let filePath;
if (route == "/loadouts")
filePath = path.join(__dirname, "site/html/lists/lists.html");
else if (route == "/loadouts/create")
filePath = path.join(__dirname, "site/html/lists/create.html");
else if (route.startsWith("/loadout/") && route.endsWith("/edit"))
filePath = path.join(__dirname, "site/html/lists/edit.html");
else if (route.startsWith("/loadout/"))
filePath = path.join(__dirname, "site/html/lists/list.html");
res.sendFile(filePath);
}
);
app.get("/post/:id", async (req, res) => {
res.sendFile(path.join(__dirname, "site/html/post.html"));
});
app.use(
express.static(path.join(__dirname, "site", "html"), { extensions: ["html"] })
);
app.use(express.static(path.join(__dirname, "site")));
app.use((req, res) => {
res.sendFile(path.join(__dirname, "site/html/index.html"));
});
app.listen(port, () => {
console.log("Server started on port", port);
});
function xmlToJson(xml) {
return new Promise((resolve, reject) => {
xml2js.parseString(
xml,
{ mergeAttrs: true, explicitArray: false },
(err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
}
);
});
}