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:
parent
a9de2028db
commit
d5f9d45123
|
|
@ -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(),
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue