From d5f9d45123a04bf096dc34b40a76b949aaa587c1 Mon Sep 17 00:00:00 2001 From: Frostime Date: Sat, 21 Sep 2024 15:48:45 +0800 Subject: [PATCH] fix(route/whu): Fix the failedness of whu route (#16830) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🐛 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 --- lib/routes/whu/cs.ts | 14 +++++++++++--- lib/routes/whu/gs/index.ts | 3 ++- lib/routes/whu/news.ts | 29 +++++++++++++++++++++++++++-- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/lib/routes/whu/cs.ts b/lib/routes/whu/cs.ts index 8b22e8caaf476..c7c8dabd81259 100644 --- a/lib/routes/whu/cs.ts +++ b/lib/routes/whu/cs.ts @@ -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(), diff --git a/lib/routes/whu/gs/index.ts b/lib/routes/whu/gs/index.ts index 250f0102388de..a48e696818371 100644 --- a/lib/routes/whu/gs/index.ts +++ b/lib/routes/whu/gs/index.ts @@ -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); diff --git a/lib/routes/whu/news.ts b/lib/routes/whu/news.ts index e544656bb69fd..5830ef4033db3 100644 --- a/lib/routes/whu/news.ts +++ b/lib/routes/whu/news.ts @@ -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;