diff --git a/lib/routes/whu/cs.ts b/lib/routes/whu/cs.ts index 8b22e8caaf4765..c7c8dabd812594 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 250f0102388de0..a48e696818371c 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 e544656bb69fd1..5830ef4033db3c 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;