-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (55 loc) · 1.65 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
const express = require("express");
const cors = require("cors");
const fetch = require("node-fetch");
const dayInMilliseconds = 1000 * 60 * 60 * 24;
let searchDate = new Date().getTime() - 2600000 * 1000;
let stories = [];
const getStory = (req, res) => {
if (stories.length > 0) {
res.status(200).send(
`https://mspfa.com/?s=${
stories[Math.floor(Math.random() * (stories.length - 1))].i
}&p=1`
);
} else {
res.status(500).send("https://mspfa.com/?s=1&p=1");
}
};
const updateStories = async () => {
searchDate += dayInMilliseconds;
stories = [];
let offset = 0;
let passes = 0;
while (
(stories.length == 0 || stories[stories.length - 1].u > searchDate) &&
passes < 10
) {
passes++;
await fetch("https://mspfa.com/", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Accept: "application/json",
},
body: `do=stories&n=&t=&h=4&o=updated&p=p&m=500&f=${offset}`,
})
.then((e) => e.json())
.then((data) => {
offset += 500;
stories = stories.concat(data);
});
}
};
// App setup
const app = express();
app.use(cors({ methods: "GET", origin: "*" }));
// Get story data
updateStories();
setInterval(updateStories, dayInMilliseconds);
// GET random MSPFA story
app.get("/api/random", getStory);
// Start server
process.env.PORT = process.env.PORT || 3003;
app.listen(process.env.PORT, () => {
console.log(`Server running on port ${process.env.PORT}.`);
});