-
Notifications
You must be signed in to change notification settings - Fork 0
/
accountable.js
241 lines (184 loc) · 7.66 KB
/
accountable.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
#!/usr/bin/env node
require('dotenv').config({ path: '/Users/isaiah/LeetCode-Accountability-Partner/.env' });
const puppeteer = require('puppeteer');
const nodemailer = require('nodemailer');
const fs = require('fs').promises;
function sleep(duration) {
return new Promise(resolve => {
setTimeout(resolve, duration);
});
}
async function checkForChallengePage(page) {
const title = await page.title();
// Check if the title is "Just a moment..." which is typical for Cloudflare challenge pages
if (title === "Just a moment...") {
console.log("Challenge page detected. Performing specific actions...");
// Add your specific actions here
await performChallengeResponseActions(page);
} else {
console.log("Normal page detected.");
}
}
async function performChallengeResponseActions(page) {
await sleep(5000);
const iframes = await page.frames();
iframes.forEach(frame => {
console.log(`Frame found with URL: ${frame.url()}`);
});
const element = await page.$('#fbMan1');
console.log('element found');
const boundingBox = await element.boundingBox();
console.log('box found');
const x = boundingBox.x + 30;
const y = boundingBox.y + boundingBox.height / 2;
await sleep(5000);
await page.mouse.click(x, y);
console.log('click box');
console.log('success')
await sleep(5000);
}
async function scrapeRowsAndEmail() {
const loginUrl = process.env.LOGIN_URL;
const targetUrl = process.env.TARGET_URL;
let browser;
try {
browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
const page = await browser.newPage();
await page.setViewport({
width: 1920,
height: 1080,
deviceScaleFactor: 1,
});
await page.evaluateOnNewDocument(() => {
Object.defineProperty(navigator, 'webdriver', {
get: () => false,
});
window.navigator.chrome = {
runtime: {},
};
const originalQuery = window.navigator.permissions.query;
window.navigator.permissions.query = (parameters) => (
parameters.name === 'notifications' ?
Promise.resolve({ state: Notification.permission }) :
originalQuery(parameters)
);
});
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36');
// Login
await page.goto(loginUrl, {
waitUntil: 'networkidle2',
timeout: 60000 // Increase timeout to 60 seconds (60000 ms)
});
// Get the HTML of page and save to file
const html = await page.content();
await fs.writeFile('botDetection.html', html);
// check for bot detection
await checkForChallengePage(page)
await page.click('form button[type="submit"]');
await page.waitForNavigation({ waitUntil: 'networkidle2' });
await sleep(2000);
await page.type('#login_field', process.env.USERNAME);
await page.type('#password', process.env.PASSWORD);
await page.click('input[type="submit"]');
await page.waitForNavigation({ waitUntil: 'networkidle2' });
await sleep(20000);
// Go to target page
await page.goto(targetUrl, { waitUntil: 'networkidle2', timeout: 60000 });
let dailyCount = 0;
let totalCompleted = 0;
const problems = [];
// get total probs
totalCompleted = await page.evaluate(() => {
const element = document.querySelector('div.text-sd-blue-500 .text-2xl.font-semibold');
return element ? element.textContent.trim() : null;
});
console.log(totalCompleted)
let foundAllDailyProblems = false;
// get all problems of the same date
while (!foundAllDailyProblems) {
const dates = await page.evaluate(() => {
const rows = document.querySelectorAll('div[role="row"]');
return Array.from(rows).map(row => {
const dateCell = row.querySelector('div[role="cell"]:first-child .text-sd-muted-foreground');
return dateCell ? dateCell.textContent.trim() : null;
}).filter(date => date !== null);
});
const elements = await page.evaluate(() => {
const rows = document.querySelectorAll('div[role="row"]');
return Array.from(rows).map(row => {
const nameElement = row.querySelector('div[role="cell"]:nth-child(2) a.font-semibold');
const text = nameElement ? nameElement.textContent.trim() : null;
const href = nameElement ? nameElement.getAttribute('href') : null;
return text && href ? { text, href } : null;
}).filter(item => item !== null);
});
const first = dates[0];
for (let i = 0; i < elements.length && i < dates.length; i++) {
const date = dates[i];
const element = elements[i];
if (date !== first) {
console.log(`Total of ${dailyCount} problems completed today.`);
foundAllDailyProblems = true;
break;
}
dailyCount++;
problems.push(`<a href="https://leetcode.com${element.href}" target="_blank">${element.text}</a><br>`);
}
if (!elements.length) {
console.log('No more elements found on the current page.');
break;
}
}
const problemsHTML = problems.join('');
let subject;
if (dailyCount >= 20) {
subject = "DUDE: LeetCode Accountability";
} else if (dailyCount >= 15) {
subject = "AMAZING: LeetCode Accountability";
} else if (dailyCount >= 10) {
subject = "WOW: LeetCode Accountability";
} else if (dailyCount >= 5) {
subject = "SUCCESS: LeetCode Accountability";
} else {
subject = "HELP: LeetCode Accountability";
}
const message = [
`<a href="https://github.com/isaiah-garcia/LeetCode-Accountability-Progress-Reporter" target="_blank"><img src="https://raw.githubusercontent.com/isaiah-garcia/LeetCode-Accountability-Progress-Reporter/master/accountable_email_logo.png" alt="Accountable logo" style="max-width: 100%; height: auto;"></a>`,
`<br><br>`,
`Daily count: ${dailyCount}`,
`<br>`,
`Total problems completed: ${totalCompleted}<br><br>`,
problemsHTML,
`<br><br><br><br><br><br>`,
`<a href="https://github.com/isaiah-garcia/LeetCode-Accountability-Progress-Reporter" target="_blank">Get Accountable</a>`,
`<br>`,
`<br>`
].join('');
// Send email to accountability partner
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS,
},
});
const mailOptions = {
from: process.env.EMAIL_USER,
to: process.env.EMAIL_RECIPIENT,
subject: subject,
html: message,
};
await transporter.sendMail(mailOptions);
} catch (error) {
console.error("An error occurred:", error);
} finally {
if (browser) {
await browser.close();
}
}
}
scrapeRowsAndEmail();
console.log(`${new Date().toISOString()} - Process completed successfully.`);