From d10cd08e04b7059db24ef2f3bb087129a643530e Mon Sep 17 00:00:00 2001 From: You Ziang <75403952+Chi-hong22@users.noreply.github.com> Date: Wed, 13 Nov 2024 00:36:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20route=20for=20HRBEU=20School=20of?= =?UTF-8?q?=20Naval=20Architecture=EF=BC=88=E5=93=88=E5=B0=94=E6=BB=A8?= =?UTF-8?q?=E5=B7=A5=E7=A8=8B=E5=A4=A7=E5=AD=A6=E8=88=B9=E8=88=B6=E5=B7=A5?= =?UTF-8?q?=E7=A8=8B=E5=AD=A6=E9=99=A2=EF=BC=89=20(#17513)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: add route for HRBEU School of Naval Architecture(哈尔滨工程大学船舶工程学院) * feat: add route for HRBEU School of Naval Architecture(哈尔滨工程大学船舶工程学院) by Chi-hong22 * remove the position of .toArray() * Modify bug to make local validation successful * use .toArray() instead. * 测试白天外网许可进入情况 * use .toArray() before .map() * update the code by the suggestions with Collaborator TonyRL * update the code by the suggestions with Collaborator TonyRL * update the code by the suggestions with Collaborator TonyRL --- lib/routes/hrbeu/sec/list.ts | 80 ++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 lib/routes/hrbeu/sec/list.ts diff --git a/lib/routes/hrbeu/sec/list.ts b/lib/routes/hrbeu/sec/list.ts new file mode 100644 index 00000000000000..795f861908aa28 --- /dev/null +++ b/lib/routes/hrbeu/sec/list.ts @@ -0,0 +1,80 @@ +import { Route } from '@/types'; +import cache from '@/utils/cache'; +import got from '@/utils/got'; +import { load } from 'cheerio'; +import { parseDate } from '@/utils/parse-date'; +const rootUrl = 'http://sec.hrbeu.edu.cn'; + +export const route: Route = { + path: '/sec/:id', + categories: ['university'], + example: '/hrbeu/sec/xshd', + parameters: { id: '栏目编号,由 `URL` 中获取。' }, + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['sec.hrbeu.edu.cn/:id/list.htm'], + }, + ], + name: '船舶工程学院', + maintainers: ['Chi-hong22'], + handler, + description: `| 学院要闻 | 学术活动 | 通知公告 | 学科方向 | +| :------: | :------: |:------: | :------: | +| xyyw | xshd | 229 | xkfx |`, +}; + +async function handler(ctx) { + const id = ctx.req.param('id'); + const response = await got(`${rootUrl}/${id}/list.htm`, { + headers: { + Referer: rootUrl, + }, + }); + + const $ = load(response.data); + + const bigTitle = $('div [class=lanmuInnerMiddleBigClass_right]').find('div [portletmode=simpleColumnAttri]').text().replaceAll(/[\s·]/g, '').trim(); + + const list = $('li.list_item') + .toArray() + .map((item) => { + let link = $(item).find('a').attr('href'); + if (link && link.includes('page.htm')) { + link = `${rootUrl}${link}`; + } + return { + title: $(item).find('a').attr('title'), + pubDate: parseDate($(item).find('span.Article_PublishDate').text()), + link, + }; + }); + + const items = await Promise.all( + list.map((item) => + cache.tryGet(item.link, async () => { + if (item.link.includes('page.htm')) { + const detailResponse = await got(item.link); + const content = load(detailResponse.data); + item.description = content('div.wp_articlecontent').html(); + } else { + item.description = '本文需跳转,请点击标题后阅读'; + } + return item; + }) + ) + ); + + return { + title: '船舶工程学院 - ' + bigTitle, + link: rootUrl.concat('/', id, '/list.htm'), + item: items, + }; +}