Skip to content

Commit 3bfc74f

Browse files
authored
Merge pull request #3065 from dreamerblue/master
feat(crawler): add sdutoj crawler
2 parents 1b6bf56 + af9c0fe commit 3bfc74f

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

crawler/__test__/crawlers.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const nbut = require('../crawlers/nbut')
2828
const nod = require('../crawlers/nod')
2929
const nit = require('../crawlers/nit')
3030
const dmoj = require('../crawlers/dmoj')
31+
const sdutoj = require('../crawlers/sdutoj')
3132

3233
const { readConfigs } = require('../lib/configReader')
3334

@@ -607,6 +608,24 @@ describe('dmoj', () => {
607608

608609
})
609610

611+
describe('sdutoj', () => {
612+
613+
test('should throw when user does not exist', async () => {
614+
await expect(sdutoj(null, notExistUsername)).rejects.toThrow('The user does not exist')
615+
})
616+
617+
test('can recognize username with space', async () => {
618+
await expect(sdutoj(null, ' ' + notExistUsername)).rejects.toThrow('The user does not exist')
619+
})
620+
621+
test('should work correctly', async () => {
622+
const res = await sdutoj(null, 'root')
623+
checkRes(res)
624+
expect(res.solvedList).toContain('1000')
625+
})
626+
627+
})
628+
610629
function checkRes(res) {
611630
expect(typeof res.solved).toBe('number')
612631
expect(typeof res.submissions).toBe('number')

crawler/config.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ crawler_order:
2626
- nod
2727
# - nit
2828
- dmoj
29+
- sdutoj
2930
- vjudge
3031

3132
crawlers:
@@ -169,6 +170,11 @@ crawlers:
169170
title: DMOJ
170171
description:
171172
url: https://dmoj.ca/
173+
sdutoj:
174+
meta:
175+
title: SDUT OJ
176+
description:
177+
url: https://oj.sdutacm.cn/
172178
vjudge:
173179
meta:
174180
title: VJudge

crawler/crawlers/sdutoj.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const request = require('superagent')
2+
3+
async function fetchSDUTOJ(api, data) {
4+
const res = await request
5+
.post(`https://oj.sdutacm.cn/onlinejudge3/api/${api}`)
6+
.set('Content-Type', 'application/json;charset=utf-8')
7+
.send(data)
8+
if (!(res.ok && res.body && res.body.success)) {
9+
throw new Error(`Server Response Error: ${res.status}, code: ${res.body ? res.body.code : ''}`)
10+
}
11+
return res.body.data
12+
}
13+
14+
module.exports = async function (config, username) {
15+
16+
if (!username) {
17+
throw new Error('Please enter username')
18+
}
19+
20+
const userSearchRes = await fetchSDUTOJ('getUserList', {
21+
username,
22+
page: 1,
23+
order: [['accepted','DESC']],
24+
limit: 1000,
25+
})
26+
const user = userSearchRes.rows.find(user => user.username === username)
27+
if (!user) {
28+
throw new Error('The user does not exist')
29+
}
30+
const userId = user.userId
31+
32+
const [statsRes, detailRes] = await Promise.all([
33+
fetchSDUTOJ('getUserProblemResultStats', { userId }),
34+
fetchSDUTOJ('getUserDetail', { userId }),
35+
])
36+
37+
return {
38+
submissions: detailRes.submitted,
39+
solved: detailRes.accepted,
40+
solvedList: statsRes.acceptedProblemIds.map(pid => `${pid}`),
41+
}
42+
}

0 commit comments

Comments
 (0)