Skip to content

Commit

Permalink
fix(route/whu): Fix the failedness of whu route (DIYgod#16830)
Browse files Browse the repository at this point in the history
* 🐛 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
  • Loading branch information
frostime committed Sep 21, 2024
1 parent a9de202 commit d5f9d45
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
14 changes: 11 additions & 3 deletions lib/routes/whu/cs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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');
}
Expand All @@ -100,6 +107,7 @@ async function handler(ctx) {
})
)
);
items = items.filter((item) => item !== null);

return {
title: $('title').first().text(),
Expand Down
3 changes: 2 additions & 1 deletion lib/routes/whu/gs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
29 changes: 27 additions & 2 deletions lib/routes/whu/news.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit d5f9d45

Please sign in to comment.