-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_problem_list.js
More file actions
75 lines (63 loc) · 2.22 KB
/
export_problem_list.js
File metadata and controls
75 lines (63 loc) · 2.22 KB
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
(async () => {
const links = Array.from(document.querySelectorAll('a'))
.filter(a => a.href.includes('/problems/') && !a.href.includes('/solution'))
.map(a => {
const slugMatch = a.href.match(/\/problems\/([^\/?]+)/);
const slug = slugMatch ? slugMatch[1] : '';
return slug;
});
const results = [];
for (let i = 0; i < links.length; i++) {
const slug = links[i];
const url = `https://leetcode.com/problems/${slug}/`;
try {
const res = await fetch(url);
const text = await res.text();
// Extract JSON from <script id="__NEXT_DATA__">
const jsonMatch = text.match(/<script id="__NEXT_DATA__" type="application\/json">(.+?)<\/script>/);
if (!jsonMatch) {
console.log(`Skipped ${slug} — __NEXT_DATA__ not found`);
continue;
}
const data = JSON.parse(jsonMatch[1]);
const question = data.props.pageProps.dehydratedState.queries
.map(q => q.state.data)
.find(d => d && d.question)
?.question;
if (!question) {
console.log(`Skipped ${slug} — question data not found`);
continue;
}
let title = question.title;
let difficulty = question.difficulty;
let problemId = question.questionFrontendId;
let topics = question.topicTags.map(t => t.name).join('|');
console.log(`${i}: ${title} | ID: ${problemId} | Difficulty: ${difficulty} | Topics: ${topics}`);
results.push({
title, slug, url, difficulty, problemId, topics
});
} catch (err) {
console.error(`Error fetching ${slug}:`, err);
}
}
// Build CSV
const csvContent = [
["Title", "Slug", "URL", "Difficulty", "Problem ID", "Topics"].join(","),
...results.map(r =>
[
`"${r.title.replace(/"/g,'""')}"`,
`"${r.slug}"`,
`"${r.url}"`,
`"${r.difficulty}"`,
`"${r.problemId}"`,
`"${r.topics}"`
].join(",")
)
].join("\n");
const blob = new Blob([csvContent], { type: 'text/csv' });
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = 'leetcode_problems_full.csv';
a.click();
console.log(`📥 Downloaded leetcode_problems_full.csv (${results.length} problems)`);
})();