-
Notifications
You must be signed in to change notification settings - Fork 9
/
fetchQuestion.js
45 lines (37 loc) · 1.03 KB
/
fetchQuestion.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
const fs = require('fs')
const {
numberPadZero,
sleep,
request,
parseXpath
} = require('./utils')
const {
SLEEP_TIME,
} = require('./config')
const dataPath = 'problem'
if (!fs.existsSync(dataPath)) {
fs.mkdirSync(dataPath)
}
;(async () => {
const problems = (await request(
{
url: 'https://leetcode.com/api/problems/all/',
},
)).data.stat_status_pairs.map(({
stat: {
question_id: id,
question__title_slug: slug,
},
}) => ({ id, slug })).sort((a, b) => a.id - b.id)
for (let problem of problems) {
const { id, slug } = problem
let idStr = numberPadZero(id, 4)
const filename = `${idStr}-${slug}.txt`
console.log(`Downloading ${slug}`)
const { data: questionData } = await request({ url: `https://leetcode.com/problems/${slug}` })
const questionText = parseXpath(questionData, 'head > meta[name="description"]', 'content')
fs.writeFileSync(`${dataPath}/${filename}`, questionText)
console.log(`Downloaded ${filename}`)
await sleep(SLEEP_TIME)
}
})();