Skip to content

Commit

Permalink
feat(crawler): add user_id support for luogu
Browse files Browse the repository at this point in the history
Now it supports both username and user_id.

We are not able to debug the username since luogu does not provide it on
UI anymore.

fixes: #3051
  • Loading branch information
Liu233w committed Dec 28, 2024
1 parent 74fc252 commit 4bc2d2b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion crawler/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ crawlers:
luogu:
meta:
title: 洛谷
description:
description: Both username and user_id are supported
url: https://www.luogu.com.cn
server_only: true
nowcoder:
Expand Down
28 changes: 18 additions & 10 deletions crawler/crawlers/luogu.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
const request = require('superagent')

module.exports = async function (config, username) {

if (!username) {
throw new Error('Please enter username')
}

async function getUserId(username) {
const uidRes = await request
.get('https://www.luogu.com.cn/api/user/search')
.query({keyword: username})
Expand All @@ -20,12 +15,25 @@ module.exports = async function (config, username) {
throw new Error('The user does not exist')
}

const uid = uidJSON.users[0].uid
const res = await request
.get('https://www.luogu.com.cn/user/' + uid)
return uidJSON.users[0].uid
}

module.exports = async function (config, username) {

if (!username) {
throw new Error('Please enter username')
}

let res = await request
.get('https://www.luogu.com.cn/user/' + username)
if (!res.ok) {
throw new Error(`Server Response Error: ${res.status}`)
const uid = await getUserId(username)
res = await request
.get('https://www.luogu.com.cn/user/' + uid)

if (!res.ok) {
throw new Error(`Server Response Error: ${res.status}`)
}
}

try {
Expand Down

0 comments on commit 4bc2d2b

Please sign in to comment.