Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ type ConfigEnvKeys =
| 'ZSXQ_ACCESS_TOKEN'
| 'SMZDM_COOKIE'
| 'REMOTE_CONFIG'
| 'REMOTE_CONFIG_AUTH';
| 'REMOTE_CONFIG_AUTH'
| 'JAAuthCookie';

export type ConfigEnv = Partial<Record<ConfigEnvKeys, string | undefined>>;

Expand Down Expand Up @@ -570,6 +571,9 @@ export type Config = {
sis001: {
baseUrl?: string;
};
sjtu: {
JAAuthCookie?: string;
};
skeb: {
bearerToken?: string;
};
Expand Down Expand Up @@ -1048,6 +1052,9 @@ const calculateValue = () => {
sis001: {
baseUrl: envs.SIS001_BASE_URL || 'https://sis001.com',
},
sjtu: {
JAAuthCookie: envs.JAAuthCookie,
},
skeb: {
bearerToken: envs.SKEB_BEARER_TOKEN,
},
Expand Down
94 changes: 94 additions & 0 deletions lib/routes/sjtu/publicoa.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { CookieJar } from 'tough-cookie';

import { config } from '@/config';
import type { Route } from '@/types';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';

const urlRoot = 'https://publicoa.sjtu.edu.cn';

export const route: Route = {
path: '/publicoa',
categories: ['university'],
example: '/sjtu/publicoa',
parameters: {},
features: {
requireConfig: [
{
name: 'JAAuthCookie',
description: 'JAAuthCookie, 登陆后提取自jaccount.sjtu.edu.cn',
},
],
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
name: '上海交通大学公文系统',
maintainers: [''],
handler,
description: `需要用户认证`,
};

const cookieJar = new CookieJar();

async function handler() {
if (!config.sjtu?.JAAuthCookie) {
throw 'JAAuthCookie needs to be set to use this route.';
}

cookieJar.setCookieSync(`JAAuthCookie=${config.sjtu.JAAuthCookie}; Domain=.jaccount.sjtu.edu.cn; Path=/`, 'https://jaccount.sjtu.edu.cn');
async function getPublicOAList() {
return await ofetch(`${urlRoot}/api/doc/list`, {
headers: {
cookie: (await cookieJar.getCookieString(urlRoot)) as string,
},
});
}
const list: any = await new Promise((resolve) => {
resolve(
getPublicOAList().catch(async (error) => {
if (error.response?.status === 401) {
let requestUrl = urlRoot;
while (true) {
// eslint-disable-next-line no-await-in-loop
const res = await ofetch.raw(requestUrl, {
headers: {
// eslint-disable-next-line no-await-in-loop
cookie: (await cookieJar.getCookieString(requestUrl)) as string,

Check failure

Code scanning / ESLint

Disallow `await` inside of loops Error

Unexpected await inside a loop.
},
redirect: 'manual',
});
Comment on lines +57 to +63

Check failure

Code scanning / ESLint

Disallow `await` inside of loops Error

Unexpected await inside a loop.
const setCookies = res.headers.getSetCookie();
for (const c of setCookies) {
cookieJar.setCookieSync(c, requestUrl);
}

if (res.status >= 300 && res.status < 400) {
const location = res.headers.get('location');
if (typeof location === 'string') {
requestUrl = new URL(location, requestUrl).href;
}
} else {
break;
}
}
return await getPublicOAList();
}
})
);
});

return {
title: '上海交通大学公文系统',
item: list.entities.map((item) => ({
title: item.title,
author: item.doccode,
pubDate: timezone(parseDate(item.qfdate), +8),
link: item.pdfpath,
})),
link: urlRoot,
};
}
Loading