From 3864e04c077845c63a360eb06d45954a2b10a090 Mon Sep 17 00:00:00 2001 From: beetcb <63141491+beetcb@users.noreply.github.com> Date: Sun, 5 Dec 2021 15:05:19 +0800 Subject: [PATCH] fix(addr): exclude bus addr returned from baidu --- core/src/conf.ts | 36 +++++++++++++++++++----------------- core/src/crawler/login.ts | 3 +-- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/core/src/conf.ts b/core/src/conf.ts index ed9095b..79959e6 100644 --- a/core/src/conf.ts +++ b/core/src/conf.ts @@ -34,12 +34,14 @@ export async function getSchoolInfos({ const isSchoolAddrNeeded = users.find((e) => e.addr.length === 1) for (const abbreviation of schoolNamesSet) { res = (await fetch( - `https://mobile.campushoy.com/v6/config/guest/tenant/info?ids=${abbreviation}`, + `https://mobile.campushoy.com/v6/config/guest/tenant/info?ids=${abbreviation}` ).catch(log.error)) as Response - const data = JSON.parse((await res.text().catch(log.error)) as string) - .data[0] as StringKV - + const data = (await res.json().catch(log.error)).data?.[0] as StringKV + if (!data) { + log.error(`学校 ID 有误,请使用 https://cea.beetcb.com/ 检查学校 ID`) + return null + } let origin = new URL(data.ampUrl).origin const authOrigin = data.idsUrl @@ -49,25 +51,23 @@ export async function getSchoolInfos({ } if (isSchoolAddrNeeded) { res = (await fetch( - `https://api.map.baidu.com/?qt=s&wd=${ - encodeURIComponent( - data.name, - ) - }&ak=E4805d16520de693a3fe707cdc962045&rn=10&ie=utf-8&oue=1&fromproduct=jsapi&res=api`, + `https://api.map.baidu.com/?qt=s&wd=${encodeURIComponent( + data.name + )}&ak=E4805d16520de693a3fe707cdc962045&rn=10&ie=utf-8&oue=1&fromproduct=jsapi&res=api` ).catch(log.error)) as Response - const addrInfo = (await res.json()) as any - defaultAddr = addrInfo.content[0].addr + const addrInfo = (await res.json()) as { + content: Array<{ addr: string; blinfo?: Array }> + } + defaultAddr = addrInfo?.content?.find((c) => !c.blinfo)?.addr ?? '' log.success({ message: `学校 ${data.name} 默认签到地址:${defaultAddr}` }) } const isCloud = data.joinType === 'CLOUD' // Get Edge-cases const edgeCaseRes = await fetch( - `https://cea.beetcb.com/api/edge-case?name=${ - encodeURIComponent( - data.name, - ) - }&c=${isCloud ? 'true' : ''}`, + `https://cea.beetcb.com/api/edge-case?name=${encodeURIComponent( + data.name + )}&c=${isCloud ? 'true' : ''}` ).catch(log.error) if (edgeCaseRes?.ok) { const edgeCase = (await edgeCaseRes.json()) as SchoolEdgeCase @@ -82,7 +82,9 @@ export async function getSchoolInfos({ edgeCase, } log.success( - `学校 ${data.name} 已完成设定,接入方式为 ${isCloud ? 'CLOUD' : 'NOTCLOUD'}`, + `学校 ${data.name} 已完成设定,接入方式为 ${ + isCloud ? 'CLOUD' : 'NOTCLOUD' + }` ) } else { throw new Error('Failed to get school edge case!') diff --git a/core/src/crawler/login.ts b/core/src/crawler/login.ts index dc79cb9..691dd3e 100644 --- a/core/src/crawler/login.ts +++ b/core/src/crawler/login.ts @@ -199,8 +199,7 @@ export default async function login( } } else { log.error({ - message: `登录失败,请尝试本地登录此地址以确保账号密码正确 @${name} - ${school.preAuthURL}`, + message: `登录失败,请尝试本地登录 ${school.preAuthURL} 以确保账号密码正确 @${name}`, }) } return