-
Notifications
You must be signed in to change notification settings - Fork 17
/
session_test.js
344 lines (297 loc) · 11.8 KB
/
session_test.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/usr/bin/env node
const winston = require('winston');
const argv = require('yargs').argv
const readline = require('readline');
const fs = require('fs');
const util = require('util');
const puppeteer = require("puppeteer-extra")
const pluginStealth = require("puppeteer-extra-plugin-stealth")
puppeteer.use(pluginStealth())
const readFile = util.promisify(fs.readFile);
process.setMaxListeners(Infinity);
//TODO: Seperate logger into own file
const logger = winston.createLogger({
format: winston.format.combine(
winston.format.timestamp(),
winston.format.json(),
winston.format.printf((info) => {
return JSON.stringify({
timestamp: info.timestamp,
level: info.level,
tab: info.tab,
message: info.message
});
})
),
transports: [
new winston.transports.Console(),
new winston.transports.File({
filename: 'glasto.log'
})
]
})
/*
Excuse my node
To-do
High priority:
Low priority:
- Extract inner text from actual glastonbury page using same method so we can match better. The 2019 glasto page exists on the selenium page.
- Add randomness to refresh time
Other:
- It would be nice to scale this so it's not just maxing out one device. How though? Possibly containerize and give each container its own VPN
*/
class Tabs {
constructor(url, rateLimitPerMinute, registrationPageInnerText) {
this.tabs = [];
this.url = url;
this.refreshRateInMs = (60 / rateLimitPerMinute) * 1000;
this.registrationPageInnerText = registrationPageInnerText;
this.paused = false;
this.similarityThreshold = 80;
this.lastHighScorer = -1;
}
setPaused(paused) {
if (paused) {
logger.info("Pausing operation. Tabs wills finish their current page load.");
} else {
logger.info("Resuming operation.");
}
this.paused = paused;
}
getPaused() {
return this.paused;
}
async initializeTabs(tabQuantity) {
this.tabs = [];
for (let i = 0; i < tabQuantity; i++) {
let tab = new Tab(this.url);
await tab.initialiseTab();
this.tabs.push(tab);
}
}
async restartTab(tabIndex) {
await this.tabs[tabIndex].close();
let tab = new Tab(this.url);
this.tabs[tabIndex] = tab;
await this.tabs[tabIndex].initialiseTab();
}
async closeTabs() {
for (let i = 0; i < this.tabs.length; i++) {
await this.tabs[i].close();
}
}
calculateSimilarity(retrievedText, desiredText) {
const retrievedTextTokens = retrievedText.replace(/(\r\n|\n|\r)/gm, "").toLowerCase().split(" ");
const desiredTextTokens = desiredText.replace(/(\r\n|\n|\r)/gm, "").toLowerCase().split(" ");
let countOfMatchingWords = 0;
// How could this be improved?
for (let i = 0; i < desiredTextTokens.length; i++) {
if (retrievedTextTokens.includes(desiredTextTokens[i])) {
countOfMatchingWords = countOfMatchingWords + 1;
}
}
const score = (countOfMatchingWords / desiredTextTokens.length) * 100;
return score;
}
async getHighestScoringTabIndex() {
// Get the tab with highest score. Return the first found if multiple exist with same score
let highestScorer = null;
for (let i = 0; i < this.tabs.length; i++) {
if (highestScorer == null) {
highestScorer = i;
continue;
}
if (await this.tabs[i].getSimilarityScore() > await this.tabs[highestScorer].getSimilarityScore()) {
highestScorer = i;
}
}
return highestScorer;
}
// TODO: tidy method
async loadPagesAtRate() {
while (true) {
for (let i = 0; i < this.tabs.length; i++) {
while (this.paused == true) {
await this.sleep(10);
}
//Don't reload the page we think is most similar, unless it's score is 0 (which it starts off with)
if (i != await this.getHighestScoringTabIndex() || await this.tabs[i].getSimilarityScore() == -1) {
if (await this.tabs[i].getReady() == true) {
logger.info({
tab: i,
message: `Loading page`
});
this.tabs[i].loadPage().then(async page => {
logger.info({
tab: i,
message: `Loaded page in ${Date.now() - this.tabs[i].getStartTime()}ms`
});
await this.tabs[i].getInnerHtmlTextOfAllElements().then(async pageInnerHtmlText => {
const similarityScore = await this.calculateSimilarity(pageInnerHtmlText, this.registrationPageInnerText);
await this.tabs[i].setSimilarityScore(similarityScore);
logger.info({
tab: i,
message: `${similarityScore.toFixed(2)}% similarity found`
});
//Hard coded this pause as results from the coach tickets run showed the page we want has a similarity score of 91
if (similarityScore > this.similarityThreshold) {
this.paused = true;
logger.info({
tab: i,
message: `Paused operation as page with > ${this.similarityThreshold}% found`
});
const successfulBrowser = await this.tabs[i].getBrowser()
const successfulBrowserPages = await successfulBrowser.pages()
const successfulBrowserPage = successfulBrowserPages.pop()
const successfulBrowserCookies = await successfulBrowserPage.cookies()
const successfulBrowserPageUrl = await successfulBrowserPage.url();
const newBrowser = await puppeteer.launch({
headless: false
});
const pages = await newBrowser.pages();
const page = pages.pop();
await page.setCookie(successfulBrowserCookies.pop())
await page.goto(successfulBrowserPageUrl, {
waitUntil: 'networkidle2',
timeout: 30000
});
//create new browser
}
const highestScoringTab = await this.getHighestScoringTabIndex();
if(highestScoringTab != this.lastHighScorer) {
this.lastHighScorer = highestScoringTab;
await this.tabs[highestScoringTab].bringToFront();
}
await this.tabs[i].setReady(true);
});
}).catch(async error => {
logger.error({
tab: i,
message: error.toString()
});
await this.tabs[i].setReady(true);
});
//Wait until enough time has passed before loading next tab so we don't break the rate limit
const finishTime = Date.now();
if (finishTime - this.tabs[i].getStartTime() < this.refreshRateInMs) {
await this.sleep(this.refreshRateInMs - (finishTime - this.tabs[i].getStartTime()));
}
} else {
// I've added a sleep here as when there are no pages ready it will freeze up
await this.sleep(10);
}
}
}
}
}
sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
}
class Tab {
constructor(url) {
this.url = url;
this.page = null;
this.browser = null;
this.innerHtmlText = null;
this.similarityScore = -1;
this.ready = false;
this.startTime = null;
}
async getBrowser() {
return await this.browser
}
getReady() {
return this.ready;
}
setReady(ready) {
this.ready = ready;
}
getSimilarityScore() {
return this.similarityScore;
}
getStartTime() {
return this.startTime;
}
setSimilarityScore(similarityScore) {
this.similarityScore = similarityScore;
}
async bringToFront() {
this.page.bringToFront();
}
async initialiseTab() {
logger.info("Spawning new tab")
this.browser = await puppeteer.launch({
headless: true
});
const pages = await this.browser.pages();
this.page = pages.pop();
this.ready = true;
}
async close() {
await this.browser.close()
return await this.page.close();
}
async loadPage() {
this.startTime = Date.now();
await this.setReady(false);
return await this.page.goto(this.url, {
waitUntil: 'networkidle2',
timeout: 30000
});
}
async getInnerHtmlTextOfAllElements() {
let innerHtmlTextOfAllElements = "";
const options = await this.page.$$('body *');
for (const option of options) {
const label = await this.page.evaluate(el => el.innerText, option);
if (label != undefined && label.length > 0) {
innerHtmlTextOfAllElements = innerHtmlTextOfAllElements + label.trim() + " ";
}
}
return innerHtmlTextOfAllElements;
}
async evaluateSelector(selector) {
const result = await this.page.evaluate(selector);
return result || '';
}
}
function parseArgs() {
if (!(argv['site'] && argv['rate-limit'] && argv['max-tabs'])) {
log.info(`Usage:\nnode main.js --site=\"localhost:3000\" --rate-limit=60 --max-tabs=10`);
process.exit(0);
}
return argv;
}
async function readFileAsString(filePath) {
return await readFile(filePath);
}
async function getRegistrationPageInnerText() {
if (argv['test'] && argv['test'] !== 'false') {
return await readFileAsString("resources/test.txt").then(data => {
return data.toString();
});
} else {
return await readFileAsString("resources/live.txt").then(data => {
return data.toString();
});
}
}
async function run() {
parseArgs();
const registrationPageInnerText = await getRegistrationPageInnerText();
const tabs = new Tabs(argv['site'], argv['rate-limit'], registrationPageInnerText);
// Pause/resume by pressing enter
readline.emitKeypressEvents(process.stdin);
process.stdin.on('keypress', (str, key) => {
if (key.ctrl && key.name === 'c') {
tabs.closeTabs();
process.exit(0);
} else if (key.name == 'enter') {
tabs.setPaused(!tabs.getPaused());
}
});
await tabs.initializeTabs(argv['max-tabs']);
await tabs.loadPagesAtRate();
}
run();