-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathgenerateProblem.js
More file actions
33 lines (26 loc) · 1021 Bytes
/
generateProblem.js
File metadata and controls
33 lines (26 loc) · 1021 Bytes
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
// This script generates problems.json(all the problems number with their difficulty) from leetcode.com API
import fs from 'fs'
import fetch from 'node-fetch'
async function main() {
try {
const res = await fetch('https://leetcode.com/api/problems/all/')
const json = await res.json()
const problems = {}
json.stat_status_pairs.forEach((p) => {
const id = p.stat.question_id
const slug = p.stat.question__title_slug
const difficulty =
['easy', 'medium', 'hard'][p.difficulty.level - 1] || 'medium'
problems[id] = { slug, difficulty }
})
if (!fs.existsSync('./public/data')) fs.mkdirSync('./public/data')
fs.writeFileSync(
'./public/data/problems.json',
JSON.stringify(problems, null, 2),
)
console.log('✅ problems.json generated successfully!')
} catch (err) {
console.error('❌ Failed to generate problems.json:', err)
}
}
main()