Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ const leetcodeConfig = {
baseUrl: "https://leetcode-cn.com",
submitUrl: "https://leetcode-cn.com/problems/$slug/submit/",
loginUrl: "https://leetcode-cn.com/accounts/login/",
allProblem: "https://leetcode-cn.com/api/problems/all/",
_91UsernameCookieName: "login", // 在91网站中存lc用户名的cookie的键名
_91PwdCookieName: "password", // 在91网站中存lc密码的cookie的键名
lcSeesionCookieName: "LEETCODE_SESSION", // lc存seesionid的 cookie键名
Expand Down
1 change: 0 additions & 1 deletion routes/lc.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ function formateSubmitData(problem = {}){
return Object.assign(problem, {
judge_type: 'large',
lang: problem.lang,
question_id: parseInt(problem.id, 10),
test_mode: false,
typed_code: problem.code
})
Expand Down
51 changes: 46 additions & 5 deletions static/solution/generate.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const fs = require("fs");
const path = require("path");
const fetch = require('node-fetch')

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

const solutions = require("./solutions.json");
let lcProblemIdMap = {}

function toArray(sep = "-", txt) {
if (!txt) return txt;
Expand Down Expand Up @@ -50,6 +53,15 @@ function matchWioutPaddingLine(reg, txt) {
);
}

function getQuestionId(link = "") {
if(!link) return null
let slug = link
.split('/')
.reverse()
.find(item => item)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么反转?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个反转是因为 url的形式为
leetcode.com/api/xxx/
按/分割后末尾,第一个不为空的字符串就是我要的

return lcProblemIdMap[slug]
}

function generate(rawMD, rawMDBuffer, i) {
const regs = {
...getSatelliteDataReg(),
Expand All @@ -73,8 +85,9 @@ function generate(rawMD, rawMDBuffer, i) {
description,
content: encrypt(rawMDBuffer),
title,
link,
link
};
solutions[i]['question_id'] = getQuestionId(link) || solutions[i]['question_id']
}
// 基础篇
function generateBasic() {
Expand Down Expand Up @@ -113,8 +126,36 @@ function generateAdvance() {
});
}

generateBasic();
generateTopic();
generateAdvance();
function getLcProblemIdMap() {
return fetch(allProblem)
.then(res => res.json())
.then(res => {
let result = {}
let data = res.stat_status_pairs
if(data){
result = data.reduce((pre, item) => {
let { stat: { question__title_slug, question_id } = {} } = item || {}
if(question__title_slug && question_id){
pre[question__title_slug] = question_id
}
return pre
}, {})
}
return result
})
}

async function main() {
try {
lcProblemIdMap = await getLcProblemIdMap()
} catch (err) {
console.log(err);
}
generateBasic();
generateTopic();
generateAdvance();

fs.writeFileSync(__dirname + "/solutions.json", JSON.stringify(solutions));
}

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