Skip to content

Commit d2a53cf

Browse files
committed
fix: 修复lc solutionId与题号不一致的问题
1 parent 63e934c commit d2a53cf

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

config/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ const leetcodeConfig = {
424424
baseUrl: "https://leetcode-cn.com",
425425
submitUrl: "https://leetcode-cn.com/problems/$slug/submit/",
426426
loginUrl: "https://leetcode-cn.com/accounts/login/",
427+
allProblem: "https://leetcode-cn.com/api/problems/all/",
427428
_91UsernameCookieName: "login", // 在91网站中存lc用户名的cookie的键名
428429
_91PwdCookieName: "password", // 在91网站中存lc密码的cookie的键名
429430
lcSeesionCookieName: "LEETCODE_SESSION", // lc存seesionid的 cookie键名

routes/lc.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ function formateSubmitData(problem = {}){
136136
return Object.assign(problem, {
137137
judge_type: 'large',
138138
lang: problem.lang,
139-
question_id: parseInt(problem.id, 10),
140139
test_mode: false,
141140
typed_code: problem.code
142141
})

static/solution/generate.js

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
const fs = require("fs");
22
const path = require("path");
3+
const fetch = require('node-fetch')
34

5+
const { leetcodeConfig:{ allProblem } } = require('../../config/index')
46
const { encrypt } = require("../../utils/crypto.js");
57

68
const solutions = require("./solutions.json");
9+
let lcProblemIdMap = {}
710

811
function toArray(sep = "-", txt) {
912
if (!txt) return txt;
@@ -50,6 +53,15 @@ function matchWioutPaddingLine(reg, txt) {
5053
);
5154
}
5255

56+
function getQuestionId(link = "") {
57+
if(!link) return null
58+
let slug = link
59+
.split('/')
60+
.reverse()
61+
.find(item => item)
62+
return lcProblemIdMap[slug]
63+
}
64+
5365
function generate(rawMD, rawMDBuffer, i) {
5466
const regs = {
5567
...getSatelliteDataReg(),
@@ -73,8 +85,9 @@ function generate(rawMD, rawMDBuffer, i) {
7385
description,
7486
content: encrypt(rawMDBuffer),
7587
title,
76-
link,
88+
link
7789
};
90+
solutions[i]['question_id'] = getQuestionId(link) || solutions[i]['question_id']
7891
}
7992
// 基础篇
8093
function generateBasic() {
@@ -113,8 +126,36 @@ function generateAdvance() {
113126
});
114127
}
115128

116-
generateBasic();
117-
generateTopic();
118-
generateAdvance();
129+
function getLcProblemIdMap() {
130+
return fetch(allProblem)
131+
.then(res => res.json())
132+
.then(res => {
133+
let result = {}
134+
let data = res.stat_status_pairs
135+
if(data){
136+
result = data.reduce((pre, item) => {
137+
let { stat: { question__title_slug, question_id } = {} } = item || {}
138+
if(question__title_slug && question_id){
139+
pre[question__title_slug] = question_id
140+
}
141+
return pre
142+
}, {})
143+
}
144+
return result
145+
})
146+
}
147+
148+
async function main() {
149+
try {
150+
lcProblemIdMap = await getLcProblemIdMap()
151+
} catch (err) {
152+
console.log(err);
153+
}
154+
generateBasic();
155+
generateTopic();
156+
generateAdvance();
157+
158+
fs.writeFileSync(__dirname + "/solutions.json", JSON.stringify(solutions));
159+
}
119160

120-
fs.writeFileSync(__dirname + "/solutions.json", JSON.stringify(solutions));
161+
main()

0 commit comments

Comments
 (0)