Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix 405 error in leetcode-cn crawler & update to new api #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
92 changes: 48 additions & 44 deletions crawler/leetcode-cn.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,57 @@ async function getLeetCodeCnInfo(username) {
acceptance: '0%',
star_rating: 0
};
let headers = {
'authority': 'leetcode.cn',
'content-type': 'application/json',
'origin': 'https://leetcode.cn',
'sec-fetch-site': 'same-origin',
'sec-fetch-mode': ' cors',
'sec-fetch-dest': 'empty',
'referer': `https://leetcode.cn/u/${username}/`,
'User-Agent':
'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Mobile Safari/537.36'

};

let headers = {};

try {
let endPoints = [
{
url: `https://leetcode.cn/graphql`,
data: {
'operationName': 'userQuestionProgress',
'variables': { 'userSlug': `${username}` },
'query': 'query userQuestionProgress($userSlug: String\u0021) { userProfileUserQuestionProgress(userSlug: $userSlug) { numAcceptedQuestions { difficulty count __typename } numFailedQuestions { difficulty count __typename } numUntouchedQuestions { difficulty count __typename } __typename }}'
let queryData = {
query: `
query userSessionProgress($userSlug: String!) {
userProfileUserQuestionSubmitStats(userSlug: $userSlug) {
acSubmissionNum {
difficulty
count
}
totalSubmissionNum {
difficulty
count
}
}
userProfileUserQuestionProgress(userSlug: $userSlug) {
numAcceptedQuestions {
difficulty
count
}
numFailedQuestions {
difficulty
count
}
numUntouchedQuestions {
difficulty
count
}
}
}
}, {
url: `https://leetcode.cn/graphql`,
data: {
'operationName': 'userQuestionSubmitStats',
'variables': { 'userSlug': `${username}` },
'query': 'query userQuestionSubmitStats($userSlug: String!) { userProfileUserQuestionSubmitStats(userSlug: $userSlug) { acSubmissionNum { difficulty count __typename } totalSubmissionNum { difficulty count __typename } __typename }}'
}
}
];
let [userQuestionProgressResponse, userQuestionSubmitStatsResponse] = await Promise.all(endPoints.map(endPoint => axios.post(endPoint.url, endPoint.data, {
headers
})));
result.easy_solved = userQuestionProgressResponse.data.data.userProfileUserQuestionProgress.numAcceptedQuestions[0].count;
result.medium_solved = userQuestionProgressResponse.data.data.userProfileUserQuestionProgress.numAcceptedQuestions[1].count;
result.hard_solved = userQuestionProgressResponse.data.data.userProfileUserQuestionProgress.numAcceptedQuestions[2].count;
`,
variables: { userSlug: `${username}` },
operationName: 'userSessionProgress'
};

let response = await axios.post('https://leetcode.cn/graphql', queryData, { headers });

const submitStats = response.data.data.userProfileUserQuestionSubmitStats;
const progressStats = response.data.data.userProfileUserQuestionProgress;

result.easy_solved = progressStats.numAcceptedQuestions.find(item => item.difficulty === 'EASY').count;
result.medium_solved = progressStats.numAcceptedQuestions.find(item => item.difficulty === 'MEDIUM').count;
result.hard_solved = progressStats.numAcceptedQuestions.find(item => item.difficulty === 'HARD').count;
result.total_solved = result.easy_solved + result.medium_solved + result.hard_solved;
let totalSubmitNum = 0;
let acceptSubmitNum = 0;
for (let i = 0; i < 3; i++) {
totalSubmitNum += userQuestionSubmitStatsResponse.data.data.userProfileUserQuestionSubmitStats.totalSubmissionNum[i].count;
acceptSubmitNum += userQuestionSubmitStatsResponse.data.data.userProfileUserQuestionSubmitStats.acSubmissionNum[i].count;
}
result.acceptance = acceptSubmitNum / totalSubmitNum;
result.acceptance *= 100;
result.acceptance = result.acceptance.toFixed(1);
result.acceptance = result.acceptance + '%';

let totalSubmitNum = submitStats.totalSubmissionNum.reduce((acc, item) => acc + item.count, 0);
let acceptSubmitNum = submitStats.acSubmissionNum.reduce((acc, item) => acc + item.count, 0);

result.acceptance = ((acceptSubmitNum / totalSubmitNum) * 100).toFixed(1) + '%';
result.star_rating = 0;
} catch (e) {
console.error(e.message);
Expand Down
8 changes: 8 additions & 0 deletions test-lccn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const lccn_crawler = require('./crawler/leetcode-cn');

async function test() {
let info = await lccn_crawler("quanpeng")
console.log(info)
}

test();