-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
index.js
43 lines (34 loc) · 1.42 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
const http = require('http');
const httpie = require('httpie');
const port = process.env.PORT || 8888;
const opts = { timeout: 3000 };
const server = http.createServer(async (request, response) => {
const [ _, username, usePledges ] = request.url.split("/");
if (!username) {
response.writeHead(500, { 'Content-Type': 'application/json' });
response.write(JSON.stringify({"error": 'username must be set.'}));
return;
}
const { data } = await httpie.get('https://patreon.com/' + username, opts);
const campaignAPI = data.match(/https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/www.patreon.com\/api\/campaigns\/([0-9]+)/);
const { data: rawData } = await httpie.get(campaignAPI[0], opts);
const campaignData = JSON.parse(rawData)['data']['attributes'];
const patron_count = campaignData['patron_count'].toString().match(/([0-9]+)/)[1];
const campaign_pledge_sum = campaignData['campaign_pledge_sum']/100;
const message = (type === "pledges")
? `${CURRENCY[campaignData['currency']]}${Math.floor(campaign_pledge_sum)}${(suffix || "/mo")}`
: `${patron_count} ${(suffix || "patrons")}`;
const res = {
schemaVersion: 1,
label: "patreon",
namedLogo: "patreon",
message: message,
color: "ff5441",
cacheSeconds: 60 * 60 * 8 // 8 hours
}
response.writeHead(200, { 'Content-Type': 'application/json' });
response.write(JSON.stringify(res));
response.end();
});
server.listen(port);
console.log("Listening on", port);