feat(route/sjtu/yzb): Full text fetch (#16084)
* feat(route/sjtu/yzb): Full text fetch * .
This commit is contained in:
+36
-16
@@ -1,7 +1,9 @@
|
||||
import { Route } from '@/types';
|
||||
import got from '@/utils/got';
|
||||
import cache from '@/utils/cache';
|
||||
import { load } from 'cheerio';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
import { fetchArticle } from '@/utils/wechat-mp';
|
||||
import ofetch from '@/utils/ofetch';
|
||||
|
||||
const baseTitle = '上海交通大学研究生招生网招考信息';
|
||||
const baseUrl = 'https://yzb.sjtu.edu.cn/index/zkxx/';
|
||||
@@ -30,25 +32,43 @@ export const route: Route = {
|
||||
async function handler(ctx) {
|
||||
const pageUrl = `${baseUrl}${ctx.req.param('type')}.htm`;
|
||||
|
||||
const response = await got({
|
||||
method: 'get',
|
||||
url: pageUrl,
|
||||
headers: {
|
||||
Referer: pageUrl,
|
||||
},
|
||||
});
|
||||
const response = await ofetch(pageUrl);
|
||||
|
||||
const $ = load(response.data);
|
||||
const $ = load(response);
|
||||
|
||||
const list = $('li[id^="line"] a')
|
||||
.toArray()
|
||||
.map((elem) => ({
|
||||
link: new URL(elem.attribs.href, pageUrl).href,
|
||||
title: $(elem).text(),
|
||||
pubDate: parseDate($(elem.next?.next).text().trim()),
|
||||
}));
|
||||
|
||||
const items = await Promise.all(
|
||||
list.map((item) =>
|
||||
cache.tryGet(item.link, async () => {
|
||||
if (new URL(item.link).hostname === 'mp.weixin.qq.com') {
|
||||
return await fetchArticle(item.link);
|
||||
} else if (new URL(item.link).hostname === 'www.shmeea.edu.cn') {
|
||||
const detailResponse = await ofetch(item.link.replace('http://', 'https://'));
|
||||
const content = load(detailResponse);
|
||||
item.description = content('.Article_content').html();
|
||||
return item;
|
||||
} else if (new URL(item.link).hostname === 'yzb.sjtu.edu.cn') {
|
||||
const detailResponse = await ofetch(item.link);
|
||||
const content = load(detailResponse);
|
||||
item.description = content('[id^=vsb_content]').html();
|
||||
return item;
|
||||
} else {
|
||||
return item;
|
||||
}
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
return {
|
||||
link: pageUrl,
|
||||
title: `${baseTitle} -- ${$('title').text().split('-')[0]}`,
|
||||
item: $('li[id^="line"] a')
|
||||
.toArray()
|
||||
.map((elem) => ({
|
||||
link: new URL(elem.attribs.href, pageUrl).href,
|
||||
title: $(elem).text(),
|
||||
pubDate: parseDate($(elem.next?.next).text().trim()),
|
||||
})),
|
||||
item: items,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user