This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
196 lines (168 loc) · 4.6 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
const chromium = require("chrome-aws-lambda");
const Cdp = require("chrome-remote-interface");
const { spawn } = require("child_process");
function sleep(miliseconds = 100) {
return new Promise(resolve => setTimeout(() => resolve(), miliseconds));
}
async function run(url, duration, lobby, audio, slow) {
let data, meta;
let loaded = false;
const loading = async (startTime = Date.now()) => {
if (!loaded && Date.now() - startTime < 12 * 1000) {
await sleep(100);
await loading(startTime);
}
};
const options = chromium.args.concat([
"--remote-debugging-port=9222",
"--window-size=1280x720",
"--hide-scrollbars"
]);
const path = await chromium.executablePath;
const chrome = spawn(path, options);
chrome.stdout.on("data", data => console.log(data.toString()));
chrome.stderr.on("data", data => console.log(data.toString()));
let client;
for (let i = 0; i < 20; i++) {
try {
client = await Cdp();
} catch (e) {
console.log(e);
await new Promise(res => setTimeout(res, 500));
}
}
const { DOM, Network, Page, Runtime, Emulation, Input } = client;
Runtime.consoleAPICalled(entry => {
console.log(
"console api called: " + entry.args.map(arg => arg.value).join(" ")
);
});
try {
await Promise.all([
Network.enable(),
Page.enable(),
DOM.enable(),
Runtime.enable()
]);
if (slow) {
await Network.emulateNetworkConditions({
offline: false,
latency: 500,
downloadThroughput: 100000,
uploadThroughput: 50000
});
}
await Emulation.setDeviceMetricsOverride({
mobile: false,
deviceScaleFactor: 0,
scale: 1,
width: 1280,
height: 0
});
await Page.loadEventFired(() => {
loaded = true;
});
await Page.navigate({ url });
await loading();
const height = 720;
await Emulation.setDeviceMetricsOverride({
mobile: false,
deviceScaleFactor: 0,
scale: 1,
width: 1280,
height: height
});
await Emulation.setVisibleSize({
width: meta && meta.width ? meta.width : 1280,
height: meta && meta.height ? meta.height : height
});
if (!lobby) {
const { root } = await DOM.getDocument();
for (let i = 0; i < 60; i++) {
try {
const { nodeId: dataId } = await DOM.querySelector({
nodeId: root.nodeId,
selector: "#bot-data-input"
});
await DOM.setFileInputFiles({
nodeId: dataId,
files: [`${process.env.LAMBDA_TASK_ROOT}/bot-recording.json`]
});
break;
} catch (e) {
console.log("No data node yet.");
await new Promise(res => setTimeout(() => res(), 500));
}
}
if (audio) {
await Input.dispatchMouseEvent({
type: "mousePressed",
x: 100,
y: 100,
button: "left",
clickCount: 1
});
await Input.dispatchMouseEvent({
type: "mouseReleased",
x: 100,
y: 100,
button: "left",
clickCount: 1
});
for (let i = 0; i < 60; i++) {
try {
const { nodeId: audioId } = await DOM.querySelector({
nodeId: root.nodeId,
selector: "#bot-audio-input"
});
await DOM.setFileInputFiles({
nodeId: audioId,
files: [`${process.env.LAMBDA_TASK_ROOT}/bot-recording.mp3`]
});
break;
} catch (e) {
console.log("No audio node yet.");
await new Promise(res => setTimeout(() => res(), 500));
}
}
}
}
await new Promise(res => setTimeout(() => res(), duration * 1000));
} catch (error) {
console.error(error);
}
chrome.kill();
await client.close();
return { data, meta };
}
module.exports.handler = async function handler(event, context, callback) {
console.log("handling");
console.log(JSON.stringify(event));
const queryStringParameters = event.query || {};
const {
hub_sid,
host,
duration = 30,
password,
lobby,
audio,
slow
} = queryStringParameters;
if (password !== "YOUR_PASS_HERE") {
return callback(null, {
statusCode: 200,
body: "bad password"
});
}
const url = `https://${host}/${hub_sid}${lobby ? "" : "?bot=true"}`;
try {
await run(url, duration, !!lobby, !!audio, !!slow);
} catch (error) {
console.error("Error running", url, error);
return callback(error);
}
return callback(null, {
statusCode: 200,
body: "done"
});
};