Skip to content

Commit 76e953b

Browse files
committed
fix(crawler): fix leetcode_cn
The api is different. But it is still broken because leetcode.cn bans crawler with Cloudflare. The URL and e2e tests are updated. related to #3062
1 parent cc38179 commit 76e953b

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

crawler/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ crawlers:
8484
meta:
8585
title: LeetCode_CN
8686
description:
87-
url: https://leetcode-cn.com
87+
url: https://leetcode.cn
8888
csu:
8989
meta:
9090
title: CSU

crawler/crawlers/leetcode_cn.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,40 @@ module.exports = async function (config, username) {
77
}
88

99
const input = {
10-
query: 'query userPublicProfile($userSlug:String!){userProfilePublicProfile(userSlug:$userSlug){submissionProgress{totalSubmissions acTotal}}}',
10+
query: `
11+
query userSessionProgress($userSlug:String!){
12+
userProfileUserQuestionSubmitStats(userSlug:$userSlug){
13+
acSubmissionNum {
14+
difficulty
15+
count
16+
}
17+
totalSubmissionNum {
18+
difficulty
19+
count
20+
}
21+
}
22+
}`,
1123
variables: { userSlug: username },
1224
}
1325

1426
const res = await request
15-
.post('https://leetcode-cn.com/graphql')
27+
.post('https://leetcode.cn/graphql/')
28+
.set('User-Agent', 'ojhunt/1.0.0')
1629
.send(input)
1730

18-
const data = res.body.data.userProfilePublicProfile
31+
const data = res.body.data.userProfileUserQuestionSubmitStats
32+
const acList = data.acSubmissionNum
33+
const subList = data.totalSubmissionNum
1934

20-
if (!data) {
35+
if (acList.length === 0 && subList.length === 0) {
2136
throw new Error('The user does not exist')
2237
}
2338

39+
const solved = acList.map(item => item.count).reduce((a, b) => a + b)
40+
const submissions = subList.map(item => item.count).reduce((a, b) => a + b)
41+
2442
return {
25-
solved: data.submissionProgress.acTotal,
26-
submissions: data.submissionProgress.totalSubmissions,
43+
solved,
44+
submissions,
2745
}
2846
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"data":{"userProfilePublicProfile":{"submissionProgress":{"totalSubmissions":4,"acTotal":2}}}}
1+
{"data":{"userProfileUserQuestionSubmitStats":{"acSubmissionNum":[{"difficulty":"EASY","count":1},{"difficulty":"MEDIUM","count":1},{"difficulty":"HARD","count":0}],"totalSubmissionNum":[{"difficulty":"EASY","count":2},{"difficulty":"MEDIUM","count":2},{"difficulty":"HARD","count":0}]}}}

e2e/cypress/integration/frontend/history.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ before(() => {
1515

1616
// FIXME: the two route below do not work in intercept
1717
cy.server()
18-
cy.route('post', 'https://cors.ojhunt.com/https://leetcode-cn.com/graphql',
18+
cy.route('post', 'https://cors.ojhunt.com/https://leetcode.cn/graphql/',
1919
'fixture:summary_leetcode.txt')
2020
.as('summary_leetcode')
2121
cy.route('/api/crawlers/vjudge/wwwlsmcom',

0 commit comments

Comments
 (0)