-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (56 loc) · 1.87 KB
/
index.js
File metadata and controls
69 lines (56 loc) · 1.87 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
const { JSDOM } = require("jsdom");
const playwright = require("playwright");
const express = require("express");
const mcache = require("memory-cache");
const cors = require('cors');
const app = express();
const port = process.env.PORT || 3000;
const cacheTime = process.env.CACHE_TIME || 5;
app.use(cors());
const cache = (duration) => {
return (req, res, next) => {
let key = "__express__" + req.originalUrl || req.url;
let cachedBody = mcache.get(key);
if (cachedBody) {
res.send(cachedBody);
} else {
res.sendResponse = res.send;
res.send = (body) => {
mcache.put(key, body, duration * 60000);
res.sendResponse(body);
};
next();
}
};
};
async function getProgress(checkoutID) {
let squareUrl = "https://checkout.square.site/merchant/B4M6RCB1WWG5F/checkout/W3OO2333C5MMCSJS3SBUOQN4";
if (checkoutID) {
squareUrl = "https://checkout.square.site/merchant/B4M6RCB1WWG5F/checkout/" + checkoutID;
}
try {
const browser = await playwright.chromium.launch({
headless: true,
});
const page = await browser.newPage();
await page.goto(squareUrl);
await page.waitForTimeout(400);
let loc = page.locator(".donation-progress-amount");
let amount = await loc.innerText();
amount = Number.parseInt(amount.substring(1).replace(/,/g, ""));
await browser.close();
return { amount: amount };
}
catch (err) {
return err;
}
}
app.get("/", cache(cacheTime), async (req, res) => {
res.send(await getProgress());
});
app.get("/:checkoutID", cache(cacheTime), async (req, res) => {
res.send(await getProgress(req.params.checkoutID));
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});