cea-core
Version:
basic cea api
58 lines • 1.92 kB
JavaScript
import sstore from '@beetcb/sstore';
import fetch from 'node-fetch';
import login from './crawler/login.js';
import log from './utils/logger.js';
export * from './utils/cookie-helper.js';
export * from './conf.js';
export { log };
export { sstore };
export { CampusphereEndpoint } from './types/helper.js';
export async function handleCookie() {
const users = sstore.get('users');
if (users === null || users === void 0 ? void 0 : users.length) {
await Promise.all(users.map(async (i) => {
const storeCookiePath = `cookie.${i.alias}`;
await handleLogin(i, storeCookiePath);
}));
}
else {
log.error('请先加载用户 <cea load>');
}
}
async function handleLogin(i, storeCookiePath) {
let cookie = sstore.get(storeCookiePath);
const name = i.alias;
const school = sstore.get('schools')[i.school];
if (!cookie) {
const result = await login(school, i);
if (result) {
sstore.set(storeCookiePath, result);
log.success({
message: `已成功获取并缓存 COOKIE`,
suffix: `@${name}`,
});
}
}
else {
const authCookieIdx = new URL(school.auth).host;
const test = await fetch(`${school.auth}/login`, {
headers: {
cookie: cookie[authCookieIdx],
},
redirect: 'manual',
});
if (test.headers.get('set-cookie')) {
log.warn({
message: `COOKIE 失效,准备刷新`,
suffix: `@${name}`,
});
sstore.del(storeCookiePath);
await handleLogin(i, storeCookiePath);
}
log.success({
message: `尝试使用缓存中的 COOKIE`,
suffix: `@${name}`,
});
}
}
//# sourceMappingURL=index.js.map