-
Notifications
You must be signed in to change notification settings - Fork 0
/
createByUrl.js
132 lines (116 loc) · 3.97 KB
/
createByUrl.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
const fs = require('fs')
const clc = require('cli-color');
const { exit } = require('process');
const puppeteer = require('puppeteer');
const args = process.argv;
const url = args[2];
async function getPageInfo(url) {
const browser = await puppeteer.launch({ defaultViewport: { width: 1920, height: 1080 } });
const page = await browser.newPage();
let flag = 1
let retry = 4
let timeout = 20
while (flag) {
try {
await page.goto(url, { 'timeout': 1000 * timeout })
flag = 0
} catch (e) {
console.log(clc.yellow(e.message))
if (flag < retry) {
flag++
} else {
throw new Error(`More than ${retry} retries`)
}
}
}
while (!await page.click('#lang-select')) {
if (await page.$('div[data-cypress=LanguageSelector-JavaScript]')) {
await page.click('div[data-cypress=LanguageSelector-JavaScript]')
break;
}
// 删除登录框
if (await page.$('.modal-container')) {
await page.evaluate(() => {
let element = document.querySelector('.modal-container');
element.parentNode.removeChild(element);
console.log("success remove class modal-container")
});
}
}
let { questionTitle, code } = await page.evaluate(() => {
const titleDom = document.querySelector('h4[data-cypress=QuestionTitle]')
const codeDom = document.querySelectorAll('.view-line')
let codeString = ''
for (let line of codeDom) {
codeString += `${line.textContent}\n`
}
return {
questionTitle: titleDom.textContent,
code: codeString,
}
});
const funcReg = /var\s(.*)\s=\sfunction/
let index = questionTitle.split('.')[0]
const funcMatch = code.match(funcReg)
let funcName = ''
if (funcMatch && funcMatch.length === 2) {
funcName = funcMatch[1]
} else {
throw new Error(`call page.evaluate() get questionTitle and code faild! questionTitle: ${questionTitle}, code: ${code}`)
}
// 文件前缀是否数字,不是则用leetcode作为前缀
const numReg = /^[0-9]+?$/;
if (!numReg.test(index)) {
const leetCodeFileReg = new RegExp(`^leetcode_[0-9]+?_${funcName}.js$`) //;
const libDir = fs.readdirSync('lib/')
let i = 1
for (const item of libDir) {
if (item.indexOf('leetcode') === 0) {
if (leetCodeFileReg.test(item)) {
throw new Error("file may exist - " + item);
}
i++
}
}
index = `leetcode_${i}`
}
const fileName = `${index}_${funcName}.js`
code += `\n\nconsole.log(${funcName}())`
await browser.close();
return {
fileName,
questionTitle,
code,
}
}
function checkPageInfo(item) {
if (!item.fileName || !item.questionTitle || !item.code) {
throw new Error(`pageInfo is \n${JSON.stringify(item, null, " ")}`)
}
}
// main
(async () => {
let pageInfo = {}
try {
pageInfo = await getPageInfo(url)
checkPageInfo(pageInfo)
} catch (e) {
console.error(clc.bgRed('[Call error]'), clc.red('request leetcode.com faild:', e.message))
exit()
}
// 判断是否存在文件
const path = "lib/" + pageInfo.fileName
if (await fs.existsSync(path)) {
console.error(clc.bgRed('[Call error]'), clc.red('create file is exist'))
exit()
} else {
// 创建文件
const codeData = `// ${pageInfo.questionTitle}. \n` + `// ${url}\n\n` + `${pageInfo.code}`
try {
await fs.writeFileSync(path, codeData)
console.log(clc.green(`create ${pageInfo.fileName} file success!\nproblem url: ${url}\nfile path: ${path}`))
} catch (e) {
console.error(clc.bgRed('[write file error]'), clc.red(e.message))
}
}
})()