-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
100 lines (78 loc) · 2.28 KB
/
handler.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
const chromium = require("chrome-aws-lambda");
const BASE_URL = `https://search.kmb.hk/kmbwebsite/Function/FunctionRequest.ashx?action=`;
const URL_MAKER = (action, route, bound, lang) => {
let _url = BASE_URL + action;
if (route) {
_url += "&route=" + route;
}
if (bound) {
_url += "&bound=" + bound;
}
if (lang) {
_url += "&lang=" + lang;
}
return _url;
};
const getFormBody = (route, bound, seq, bsicode) => {
bsicode = bsicode.replace(/-/g, "");
const now = new Date();
const utc = now.getTime();
const ms = utc % 100;
const timestamp =
now.toISOString().replace(/T/, " ").replace(/\..+/, "") + "." + ms + ".";
const sep = "--31" + timestamp + "13--";
const token =
route + sep + bound + sep + "1" + sep + bsicode + sep + seq + sep + utc;
const token64 = "E" + Buffer.from(token).toString("base64");
return [token64, timestamp];
};
let page;
// let browser;
exports.main = async (event, context) =>{
const { route, bound, seq, bsicode } = event.queryStringParameters;
let result = null;
let browser = null;
try {
browser = await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless,
ignoreHTTPSErrors: true,
});
let page = await browser.newPage();
await page.goto("https://search.kmb.hk/KMBWebSite/index.aspx?lang=tc");
const formBody = getFormBody(route, bound, seq, bsicode);
const getEta = async () => {
const etaRes = await page.evaluate(async (formBody) => {
const _captchaKey = await grecaptcha.execute(recaptchaKey, {
action: "get_eta",
});
const response = await $.post(
"/KMBWebSite/Function/FunctionRequest.ashx/?action=get_ETA&lang=1",
{
captcha: _captchaKey,
token: formBody[0],
t: formBody[1],
}
);
return response.data;
}, formBody);
return etaRes;
};
const content = await getEta();
result = {
statusCode: 200,
body: JSON.stringify({
content,
}),
};
} catch (error) {
return error;
} finally {
if (browser !== null) {
await browser.close();
}
}
return result
};