-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
322 lines (292 loc) Β· 11.7 KB
/
bot.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
const { Builder, By, until, Capabilities } = require('selenium-webdriver')
const fs = require('fs')
const axios = require('axios')
const chrome = require('selenium-webdriver/chrome')
const SOLUTIONS_PATH = './solutions.md'
const CONFIG_PATH = './config.json'
const COOKIES_LEETCODE_PATH = './cookies_leetcode.json'
const COOKIES_HACKERRANK_PATH = './cookies_hackerrank.json'
const config = JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf-8'))
const IS_LEETCODE = config.URL.includes('leetcode')
const IS_HACKERRANK = config.URL.includes('hackerrank')
const IS_LEETCODE_STUDY_PLAN = IS_LEETCODE && config.URL.includes('studyplan')
const IS_HACKERRANK_PREPARATION_KIT = IS_HACKERRANK && config.URL.includes('preparation-kits')
const SHORT_WAIT = 3000
const LONG_WAIT = 6000
async function setupBrowser() {
let chromeCapabilities = Capabilities.chrome()
chromeCapabilities.set('pageLoadStrategy', 'normal')
let options = new chrome.Options()
// options.addArguments('--headless') // Set Chrome to run in headless mode
options.addArguments(
'user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'
)
let driver = await new Builder()
.forBrowser('chrome')
.withCapabilities(chromeCapabilities)
.setChromeOptions(options)
.build()
return driver
}
async function loadCookies(driver, cookieFile) {
const cookies = JSON.parse(fs.readFileSync(cookieFile)).forEach(
async (cookie) => await driver.manage().addCookie(cookie)
)
console.log('πͺ loaded')
}
async function saveCookies(driver, cookieFile) {
const cookies = await driver.manage().getCookies()
fs.writeFileSync(cookieFile, JSON.stringify(cookies))
console.log('πͺ saved')
}
async function saveSolutionToFile(lang, taskTitle, taskUrl, descriptionHtml, solution) {
try {
fs.appendFileSync(
SOLUTIONS_PATH,
`π [${taskTitle}](${taskUrl}) for βΉοΈ ${lang}:\r\n\r\nπ:\r\n${descriptionHtml}\r\n\r\nπ:\r\n${solution}\r\n---\r\n`
)
console.log('π saved')
} catch (error) {
console.error('π΄ err:', error)
}
}
async function performLogin(driver, loginUrl, loginElementCss, cookieFile) {
await driver.executeScript(`window.localStorage.setItem('global_lang', '${config.LANG.toLowerCase()}');`)
await loadCookies(driver, cookieFile)
await driver.sleep(SHORT_WAIT)
await driver.navigate().refresh()
await driver.sleep(SHORT_WAIT)
let loggedInElement = await driver.findElements(By.css(loginElementCss))
if (loggedInElement.length === 0) {
console.log('βΉοΈ not logged in, redirecting to login page...')
await driver.get(loginUrl)
await driver.sleep(SHORT_WAIT)
console.log('βΉοΈ Log in manually, solve the CAPTCHA if needed, and press Enter here:')
process.stdin.once('data', async () => {
driver.sleep(SHORT_WAIT)
await saveCookies(driver, cookieFile)
await performLogin(driver, loginUrl, loginElementCss, cookieFile)
})
} else {
console.log('βΉοΈ log in: ok')
}
}
async function ensureLogin(driver) {
await driver.get(config.URL)
const [loginUrl, cookieFile, loginElementCss] = IS_LEETCODE
? ['https://leetcode.com/accounts/login/', COOKIES_LEETCODE_PATH, '#navbar_user_avatar']
: IS_HACKERRANK
? ['https://www.hackerrank.com/login', COOKIES_HACKERRANK_PATH, 'button[data-analytics="NavBarProfileDropDown"]']
: ''
performLogin(driver, loginUrl, loginElementCss, cookieFile)
}
async function goToNextTask(driver) {
await driver.get(config.URL)
await driver.navigate().refresh()
await driver.sleep(LONG_WAIT)
const tasksXPath = IS_LEETCODE
? IS_LEETCODE_STUDY_PLAN
? "//div[contains(@class, 'font-medium') and contains(@class, 'text-lc-text-primary') and contains(@class, 'dark:text-dark-lc-text-primary')]/div[@class='truncate']"
: "//a[contains(@class, 'h-5 hover:text-blue-s dark:hover:text-dark-blue-s')]"
: IS_HACKERRANK
? '//span[text()="Solve Challenge"]'
: ''
const parentXPath = IS_LEETCODE
? IS_LEETCODE_STUDY_PLAN
? './../../../..'
: './../..'
: IS_HACKERRANK
? IS_HACKERRANK_PREPARATION_KIT
? './../../../..'
: './../../../../../../..'
: ''
const hakerRankTaskTitleXPath = IS_HACKERRANK_PREPARATION_KIT
? './/h2[contains(@class, "interview-ch-li-title")]'
: './/h4[contains(@class, "challengecard-title")]'
const isLeetCodeTaskUnsolved = (parentHtml) => {
if (IS_LEETCODE_STUDY_PLAN) {
// βͺοΈ circle withou βοΈ done tick
return parentHtml.includes(
'M12 22C6.477 22 2 17.523 2 12S6.477 2 12 2s10 4.477 10 10-4.477 10-10 10zm0-2a8 8 0 100-16 8 8 0 000 16z'
)
} else {
// does not include premium tag π·οΈ
return !parentHtml.includes('M5.7334 8.3623H7.12988C7.51725 8.3623 7.84766 8.22559')
}
}
let tasks = await driver.findElements(By.xpath(tasksXPath))
console.log(`π found ${tasks.length} tasks on the page.`)
if (IS_LEETCODE && !IS_LEETCODE_STUDY_PLAN) {
// skip top ποΈ task
tasks = tasks.slice(1)
}
let title = ''
for (let task of tasks) {
let parent = await task.findElement(By.xpath(parentXPath))
let parentHtml = await parent.getAttribute('outerHTML')
const titleElement = IS_HACKERRANK ? await parent.findElement(By.xpath(hakerRankTaskTitleXPath)) : undefined
if (IS_HACKERRANK || isLeetCodeTaskUnsolved(parentHtml)) {
title = IS_LEETCODE ? await task.getText() : await titleElement.getText()
console.log(`π found unsolved task: ${title}`)
await driver.executeScript('arguments[0].scrollIntoView(true);', task)
if (IS_HACKERRANK) {
await titleElement.click()
} else if (IS_LEETCODE_STUDY_PLAN) {
await task.click()
} else if (IS_LEETCODE) {
let taskUrl = await task.getAttribute('href')
await driver.executeScript(`window.open('${taskUrl}', '_blank');`)
}
break
}
}
await driver.sleep(SHORT_WAIT)
return title
}
async function askGptForSolution(lang, taskTitle, taskUrl, descriptionHtml, codeStub) {
const prompt = `
You are a senior ninja engineer. Solve [${taskTitle}](${taskUrl}) for ${lang}:
${JSON.stringify(descriptionHtml)}
Wrap solution code in a \`\`\`lang code block, followed by short comments explaining the solution.
Don't put anything else in code block. Code should fill this stub: ${JSON.stringify(
codeStub
)}. js means node.js, don't do recursion if not directly asked.
`
try {
const response = await axios.post(
'https://api.openai.com/v1/chat/completions',
{
model: config.MODEL,
messages: [{ role: 'user', content: prompt }],
max_tokens: config.MAX_TOKENS,
},
{
headers: {
Authorization: `Bearer ${config.OPENAI_API_KEY}`,
'Content-Type': 'application/json',
},
}
)
const gptResponse = response.data.choices[0].message.content.trim()
return gptResponse
} catch (error) {
console.error('π΄ err:', error)
}
}
async function detectEditor(driver) {
return await driver.executeScript(`
if (typeof monaco !== 'undefined' && monaco.editor.getModels().length > 0) {
return 'monaco';
} else if (document.querySelector('.CodeMirror')) {
return 'codemirror';
} else {
return 'none';
}
`)
}
async function pasteCodeIntoEditor(driver, editorType, code) {
if (editorType === 'monaco') {
const codeEditor = await driver.findElement(By.className('monaco-editor'))
await codeEditor.click()
await driver.sleep(SHORT_WAIT)
await driver.executeScript('monaco.editor.getModels()[0].setValue("");')
await driver.sleep(SHORT_WAIT)
await driver.executeScript('monaco.editor.getModels()[0].setValue(arguments[0]);', code)
} else if (editorType === 'codemirror') {
const codeEditor = await driver.findElement(By.css('.CodeMirror'))
await codeEditor.click()
await driver.sleep(SHORT_WAIT)
await driver.executeScript(
`
var editor = document.querySelector('.CodeMirror').CodeMirror;
editor.setValue(arguments[0]);
`,
code
)
}
}
async function submitCode(driver, editorType) {
if (IS_LEETCODE) {
await driver.findElement(By.css('button[data-e2e-locator="console-submit-button"]')).click()
} else if (IS_HACKERRANK && editorType === 'monaco') {
await driver.executeScript('document.querySelector(".hr-monaco-submit").click();')
} else if (IS_HACKERRANK && editorType === 'codemirror') {
const submitButton = await driver.findElement(By.xpath('//button[contains(text(), "Submit Code")]'))
await submitButton.click()
}
await driver.sleep(LONG_WAIT)
}
async function setLang(driver) {
if (IS_HACKERRANK) {
const dropdown = await driver.findElement(By.css('div.custom-select.select-language, div.select2-container'))
await dropdown.click()
console.log('βΉοΈ lang dropdown click')
await driver.sleep(SHORT_WAIT / 2)
const langOption = await driver.findElement(
By.xpath(
`//div[contains(translate(text(), "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), "${config.LANG.toLowerCase()}")]`
)
)
await langOption.click()
console.log('βΉοΈ lang selected')
}
await driver.sleep(SHORT_WAIT / 2)
}
async function solve(driver, taskTitle, lang) {
let taskUrl = await driver.getCurrentUrl()
await driver.sleep(SHORT_WAIT)
let tabs = await driver.getAllWindowHandles()
await driver.switchTo().window(tabs[tabs.length - 1])
await driver.sleep(SHORT_WAIT)
let descriptionDiv = IS_LEETCODE
? await driver.wait(until.elementLocated(By.css("div.elfjS[data-track-load='description_content']")), LONG_WAIT)
: await driver.wait(until.elementLocated(By.className('challenge-body-html')), LONG_WAIT)
let descriptionHtml = IS_LEETCODE ? await descriptionDiv.getAttribute('innerHTML') : await descriptionDiv.getText()
console.log('π description', descriptionHtml)
await setLang(driver)
const editorType = await detectEditor(driver)
let codeStub =
editorType === 'monaco'
? await driver.executeScript('return monaco.editor.getModels()[0].getValue();')
: await driver.executeScript(`
return document.querySelector('.CodeMirror').CodeMirror.getValue();
`)
console.log('π code stub', codeStub)
const solution = await askGptForSolution(lang, taskTitle, taskUrl, descriptionHtml, codeStub)
saveSolutionToFile(lang, taskTitle, taskUrl, descriptionHtml, solution)
const codeBlockRegex = /```(?:[a-z]+)?\s*([\s\S]*?)```/i
const match = solution.match(codeBlockRegex)
const code = match ? match[1].trim() : solution.trim()
console.log('π code from gpt:', JSON.stringify(code))
await pasteCodeIntoEditor(driver, editorType, code)
submitCode(driver, editorType)
if (IS_LEETCODE) {
await driver.wait(until.elementLocated(By.xpath("//div[text()='All Submissions']")), LONG_WAIT)
await driver.sleep(SHORT_WAIT)
await driver.close() // close the active tab
await driver.switchTo().window(tabs[0])
await driver.navigate().refresh()
} else if (IS_HACKERRANK) {
await driver.wait(until.elementLocated(By.className('testcase-results')), SHORT_WAIT)
await driver.wait(until.elementIsVisible(driver.findElement(By.className('testcase-results'))), SHORT_WAIT)
}
await driver.sleep(SHORT_WAIT)
}
;(async function main() {
try {
console.log(`URL: ${config.URL}, lang: ${config.LANG}`)
let driver = await setupBrowser()
await ensureLogin(driver)
let taskTitle = await goToNextTask(driver)
while (taskTitle !== '') {
try {
await solve(driver, taskTitle, config.LANG)
taskTitle = await goToNextTask(driver)
} catch (err) {
console.error('π΄ err:', err)
}
}
} finally {
// await driver.quit()
}
})()