fix(route/whu): Fix the failedness of whu route (#16830)

* 🐛 fix: failed when try to fetch from whu

1. exception in cs
2. bug in parsing category in news

* 🔥 rm doc header

* ️ perf(cs): add try catch in `cs` casue it might fetch a not existed url and cause exception

* 🎨 misc: resolve the reviewer's suggestion
This commit is contained in:
Frostime 2024-09-21 15:48:45 +08:00 committed by GitHub
parent a9de2028db
commit d5f9d45123
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 40 additions and 6 deletions

View File

@ -71,10 +71,16 @@ async function handler(ctx) {
};
});
const items = await Promise.all(
let items = await Promise.all(
list.map((item) =>
cache.tryGet(item.link, async () => {
const response = await got(item.link);
let response;
try {
// 实测发现有些链接无法访问
response = await got(item.link);
} catch {
return null;
}
const $ = load(response.data);
if ($('.prompt').length) {
@ -87,7 +93,8 @@ async function handler(ctx) {
content.find('img').each((_, e) => {
e = $(e);
if (e.attr('orisrc')) {
e.attr('src', new URL(e.attr('orisrc'), response.url).href);
const newUrl = new URL(e.attr('orisrc'), 'https://cs.whu.edu.cn');
e.attr('src', newUrl.href);
e.removeAttr('orisrc');
e.removeAttr('vurl');
}
@ -100,6 +107,7 @@ async function handler(ctx) {
})
)
);
items = items.filter((item) => item !== null);
return {
title: $('title').first().text(),

View File

@ -46,7 +46,8 @@ export const route: Route = {
async function handler(ctx) {
const host = 'https://gs.whu.edu.cn/';
const type = (ctx.params && Number.parseInt(ctx.req.param('type'))) || 0;
const paremType = ctx.req.param('type');
const type = paremType ? Number.parseInt(paremType) : 0;
const response = await got(host + gsIndexMap.get(type));
const $ = load(response.data);

View File

@ -13,15 +13,40 @@ import { domain, processMeta, getMeta, processItems } from './util';
export const route: Route = {
path: '/news/:category{.+}?',
name: 'Unknown',
categories: ['university'],
example: '/whu/news',
parameters: { category: '新闻栏目,可选' },
name: '新闻网',
maintainers: [],
handler,
description: `
category :
| | | | | |
| -------- | -------- | -------- | -------- | -------- |
| | 0 \`wdzx/wdyw\` | 1 或 \`kydt\` | 2 或 \`stkj/ljyx\` | 3 或 \`stkj/wdsp\` |
route \`?limit=n\` 的查询参数,表示只获取前 n 条新闻;如果不指定默认为 10。
`,
};
const parseCategory = (category: string | number) => {
const outputs = ['wdzx/wdyw', 'kydt', 'stkj/ljyx', 'stkj/wdsp'];
if (['0', '1', '2', '3'].includes(category)) {
return outputs[category];
}
if (outputs.includes(category)) {
return category;
}
return 'wdzx/wdyw';
};
async function handler(ctx) {
const { category = 'wdzx/wdyw' } = ctx.req.param();
let { category } = ctx.req.param();
const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 10;
category = parseCategory(category);
const rootUrl = `https://news.${domain}`;
const currentUrl = new URL(`${category}.htm`, rootUrl).href;