Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
feat(edge-cases): using proxy to get better default
Browse files Browse the repository at this point in the history
  • Loading branch information
beetcb committed Mar 21, 2021
1 parent 9d54a43 commit 43cf1a7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
7 changes: 4 additions & 3 deletions crawler/casLogIn.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const ocr = require('./captcha')
*/
module.exports = async (school, user) => {
// improve school campatibility with defaults and edge-cases
const schoolEdgeCases = require('./school-edge-cases')[school.name] || {}
const schoolEdgeCases = require('./school-edge-cases')(school.name)

const headers = {
'Cache-control': 'max-age=0',
Expand Down Expand Up @@ -44,7 +44,7 @@ module.exports = async (school, user) => {
// create document for crawling
const body = await res.text()
const $ = cheerio.load(body)
const form = $('form[method=post]').get(schoolEdgeCases.formIdx || 0)
const form = $('form[method=post]').get(schoolEdgeCases.formIdx)
const hiddenInputList = $('input[type=hidden]', form)

// grab hidden input name-value, this maybe error-prone, but compatible
Expand All @@ -66,10 +66,11 @@ module.exports = async (school, user) => {
username: user.username,
password: pwdSalt ? new AES(user.password, pwdSalt).encrypt() : user.password,
...hiddenInputNameValueMap,
rememberMe: schoolEdgeCases.rememberMe,
})

// check captcha is needed
if (schoolEdgeCases.supportCaptcha || true) {
if (schoolEdgeCases.supportCaptcha) {
res = await fetch(`${school.checkCaptcha}?username=${user.username}`, {
headers,
})
Expand Down
26 changes: 23 additions & 3 deletions crawler/school-edge-cases.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
// @ts-check
module.exports = {
const schoolEdgeCases = {
武汉轻工大学: {
formIdx: 2, // default 0
formIdx: 2,
},
宁波大学: {
supportCaptcha: false, // default true
supportCaptcha: false,
rememberMe: 'on',
},
}

// we will using proxy to get the default properties
const defaultProps = {
rememberMe: true,
formIdx: 0,
supportCaptcha: true,
}

module.exports = (schoolName) =>
schoolName
? new Proxy(schoolEdgeCases[schoolName], {
get(target, prop, receiver) {
if (target[prop] === undefined) {
return defaultProps[prop]
}
return Reflect.get(target, prop, receiver)
},
})
: {}

0 comments on commit 43cf1a7

Please sign in to comment.