diff --git a/campusphere/app.js b/campusphere/app.js index 26dbffa..0f8eb48 100644 --- a/campusphere/app.js +++ b/campusphere/app.js @@ -53,7 +53,6 @@ exports.signApp = class signApp extends ( headers, body: JSON.stringify({}), }) - log.object(this.headers) if (res.headers.hasOwnProperty('set-cookie')) return true const signQ = await res.json() this.curTask = signQ.datas.unSignedTasks[0] diff --git a/crawler/casLogIn.js b/crawler/casLogIn.js index e8cd238..d151189 100644 --- a/crawler/casLogIn.js +++ b/crawler/casLogIn.js @@ -103,7 +103,6 @@ module.exports = async (school, user) => { log.success(`用户${name}: Login Success`) } else { log.error(`${res.statusText}: ${name}`) - log.object(res.headers) return null } diff --git a/index.js b/index.js index ddab26a..d2ab778 100644 --- a/index.js +++ b/index.js @@ -23,26 +23,38 @@ let cookie // purely for handleCookie func let storeCookiePath, sign + /* get|store|update cookie synchronizedly */ async function handleCookie() { for (let i of users) { storeCookiePath = `cookie.${i.alias || i.username}` + await handleLogin(i, conf.get(storeCookiePath)) + } +} + +async function handleLogin(i, storedCookie) { + const name = i.alias || i.username - if (!conf.get(storeCookiePath)) { - await reLogin(i) + // Check if the cookie is user-provided + if (!i.cookie) { + // Check if the cookie is stored + if (!storedCookie) { + cookie = await login(school, i) + storeCookie(storeCookiePath, i, cookie) } else { storeCookie(storeCookiePath, i) } + + // Check if the cookie is eligible, if not, reLogin 1 more time sign = new signApp(school, i) const isNeedLogIn = await sign.signInfo(cookie) if (isNeedLogIn) { - await reLogin(i) - try { - cookie.campusphere - } catch (err) { - log.error(`${name}: exit(1)`) - } + log.warning(`${name}: cookie is not eligible, reLogin`) + cookie = await login(school, i) + storeCookie(storeCookiePath, i, cookie) } + } else { + log.success(`${name}: Using user provided cookie`) } } @@ -55,27 +67,17 @@ async function signIn(i) { await sign.signWithForm() } -async function reLogin(i) { +function storeCookie(path, i, set) { const name = i.alias || i.username - - if (!i.cookie) { - cookie = await login(school, i) - if (cookie) { - conf.set(storeCookiePath, cookie) - log.success(`${name}: Cookie stored to local storage`) - } + if (set) { + conf.set(storeCookiePath, set) + log.success(`${name}: Cookie stored to local storage`) } else { - cookie = { campusphere: i.cookie } - log.success(`${name}: Using user provided cookie`) + cookie = conf.get(path) + log.success(`${name}: Using stored cookie`) } } -function storeCookie(path, i) { - const name = i.alias || i.username - cookie = conf.get(path) - log.success(`${name}: Using stored cookie`) -} - async function sleep(timeout) { return new Promise(r => setTimeout(r, timeout * 1000 * 60)) } diff --git a/init.js b/init.js index 92eed49..a891aca 100755 --- a/init.js +++ b/init.js @@ -203,6 +203,8 @@ class School { school = new School(conf).init() } if (process.argv[2].match(/(rm|--remove)/)) { - conf.delete(process.argv[3]) + const target = process.argv[3] + if (target === 'all') conf.clear() + conf.delete(target) } })()