-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
198 lines (168 loc) · 5.23 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
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
const http = require("http");
const https = require("https");
const fs = require("fs");
const nconf = require("nconf");
const express = require("express");
const cors = require("cors");
const crypto = require("crypto");
const scrapers = require("./scrapers");
nconf.argv().env().file({
file: "config.json",
});
nconf.set("http:port", "9080");
nconf.set("http:ssl", false);
nconf.set("http:ssl_port", "9443");
nconf.set("http:ssl_cert", "");
nconf.set("http:ssl_privkey", "");
nconf.save();
const port = nconf.get("http:port") || 80;
const sslPort = nconf.get("http:ssl_port") || 443;
const app = express();
const idToData = new Map();
const rateLimit = require("express-rate-limit");
const limiter = rateLimit({
windowMs: 60 * 1000, // 1 minute
max: 50, // limit each IP to 100 requests per windowMs
});
app.use(cors());
app.use(limiter);
app.get("/", (req, res) => {
res.send("LiveStreamNorge API");
});
app.get("/streams", async (req, res) => {
let data = Array.from(idToData.values());
if (req.query.filter && typeof req.query.filter === "object") {
let filteredData = data;
Object.entries(req.query.filter).forEach((arg) => {
var index = arg[0];
var filter = arg[1];
filteredData = filteredData.filter((d) => {
if (filter === "true") filter = true;
if (filter === "false") filter = false;
if (filter && typeof filter === "string" && filter.indexOf(",") > -1) {
filters = filter.split(",");
for (f in filters) {
if (filters.hasOwnProperty(f)) {
if (d[index] === filters[f]) return true;
}
}
return false;
} else {
return d[index] === filter;
}
});
});
res.send(filteredData);
} else {
res.send(data);
}
console.info(`[${req.ip}] Requested /streams`);
});
app.get("/teams", (req, res) => {
let data = Array.from(idToData.values());
let teams = [];
data.forEach((d) => {
if (d.team && teams.indexOf(d.team) === -1) teams.push(d.team);
});
res.send(teams);
});
app.get("/platforms", (req, res) => {
res.send([...scrapers]?.map((s) => s[0]) ?? {});
});
app.get("/src", (req, res) => {
res.send(
`Copyright ${new Date().getFullYear()}, AGPLv3, https://github.com/LiveStreamNorge/lsnd`
);
});
const updatePeriod = 5 * 60 * 1000; // 5 minutes
/**
* @return Boolean
* @returns Return true on successful scrape
*/
async function scrape({ platform, userId, customUsername, ...rest }) {
let data;
const scraper =
(scrapers.has(platform) && scrapers.get(platform)) ||
(async () => {
throw new Error(`Platform ${platform} not supported!`);
});
const id = crypto
.createHash("sha256")
.update(platform + userId + customUsername)
.digest("hex");
if (platform === "kick") {
// wait 2 sec before fetching
await new Promise((resolve) => setTimeout(resolve, 2000));
}
try {
data = await scraper(userId, customUsername);
} catch (e) {
console.error(
`Couldn't scrape ${userId} ${customUsername ?? ""}: `,
e.message
);
// Add a placeholder user (in case the first API request for this user fails)
if (!idToData.get(id)) {
idToData.set(id, {
id,
userId,
platform,
name: customUsername ?? userId,
customUsername,
featuredRank: rest.featuredRank ?? null,
team: rest.team ?? null,
});
}
return false;
}
// Append `id' and `userId' fields before adding to the map
data = { id, userId, ...data };
if (rest.featuredRank) data.featuredRank = rest.featuredRank;
if (rest.team) data.team = rest.team;
idToData.set(id, data);
return true;
}
const timestamp = () => new Date().toTimeString().split(" ")[0];
const loadPeople = async (people) => {
console.info("Populating scrape data...");
// multithread all initial scrapes, wait for them all to finish
await Promise.all(
people.map(async (person, i) => {
setTimeout(() => {
setInterval(async () => {
await scrape(person);
console.info(`[${timestamp()}] Rescraped ${person.userId}`);
}, updatePeriod);
}, (updatePeriod / people.length) * i);
// Split `updatePeriod` into equal periods, and then scrape every `updatePeriod`,
// so that the scrapes are evenly distributed over the `updatePeriod`.
(await scrape(person)) && console.info(`Scraped ${person.userId}!`);
})
);
console.info("Finished scraping everyone!");
};
const people = require("./people.json");
const httpServer = http.createServer(app);
httpServer.listen(port, async () => {
console.log(`lsnd listening to 0.0.0.0:${port}`);
await loadPeople(people);
});
// Try setting up an https server
if (nconf.get("http:ssl") === true) {
try {
const key = fs.readFileSync(
nconf.get("http:ssl_privkey") ||
"/etc/certs/lsn-api.dat.cloud/privkey.pem"
);
const cert = fs.readFileSync(
nconf.get("http:ssl_cert") || "/etc/certs/lsn-api.dat.cloud/fullchain.pem"
);
const httpsServer = https.createServer({ key, cert }, app);
httpsServer.listen(443, () => {
console.log(`lsnd/TLS listening to 0.0.0.0:${sslPort}`);
});
} catch (e) {
console.error("Couldn't set up HTTPS server!");
console.error(e.message);
}
}