-
Notifications
You must be signed in to change notification settings - Fork 9
/
fetchSubmission.js
79 lines (68 loc) · 1.82 KB
/
fetchSubmission.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
const fs = require('fs')
const {
numberPadZero,
sleep,
request,
} = require('./utils')
const {
SLEEP_TIME,
} = require('./config')
const extMap = {
bash: 'sh',
c: 'c',
cpp: 'cpp',
csharp: 'cs',
golang: 'go',
java: 'java',
javascript: 'js',
mysql: 'sql',
python: 'py',
python3: 'py',
ruby: 'rb',
scala: 'scala',
swift: 'swift',
}
const dataPath = 'submission'
if (!fs.existsSync(dataPath)) {
fs.mkdirSync(dataPath)
}
const solved = []
;(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 })).filter(({ id }) => solved.includes(id)).sort((a, b) => a.id - b.id)
for (let problem of problems) {
const { id, slug } = problem
console.log(`Downloading ${slug}`)
const { data: { submissions_dump } } = await request({
url: `https://leetcode.com/api/submissions/${slug}`,
})
const acceptedSubmission = submissions_dump.filter(({ status_display }) => status_display === 'Accepted')
if (acceptedSubmission.length === 0) {
await sleep(SLEEP_TIME)
continue
}
const { url, lang } = acceptedSubmission[0]
await sleep(SLEEP_TIME)
const { data: codeData } = await request({
url: `https://leetcode.com${url}`,
})
let idStr = numberPadZero(id, 4)
const matches = codeData.match(/submissionCode: '(.*)',\n editCodeUrl/)
const code = `${
matches[1].replace(/\\u[\dA-F]{4}/gi, match =>
String.fromCharCode(parseInt(match.replace(/\\u/g, ''), 16))
)}\n`
const filename = `${idStr}-${slug}.${extMap[lang]}`
fs.writeFileSync(`${dataPath}/${filename}`, code)
console.log(`Downloaded ${filename}`)
await sleep(SLEEP_TIME)
}
})()