diff --git a/crawler/leetcode-cn.js b/crawler/leetcode-cn.js index e42da14..2657be9 100644 --- a/crawler/leetcode-cn.js +++ b/crawler/leetcode-cn.js @@ -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); diff --git a/test-lccn.js b/test-lccn.js new file mode 100644 index 0000000..49fb368 --- /dev/null +++ b/test-lccn.js @@ -0,0 +1,8 @@ +const lccn_crawler = require('./crawler/leetcode-cn'); + +async function test() { + let info = await lccn_crawler("quanpeng") + console.log(info) +} + +test();