From 057ea01f47f0e1e5750b978554d70e97b371f0d2 Mon Sep 17 00:00:00 2001 From: ueiu <39592269+ueiu@users.noreply.github.com> Date: Mon, 22 Apr 2024 00:51:43 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20=E5=A2=9E=E5=8A=A0=E9=BB=91?= =?UTF-8?q?=E9=BE=99=E6=B1=9F=E5=85=AB=E4=B8=80=E5=86=9C=E5=9E=A6=E5=A4=A7?= =?UTF-8?q?=E5=AD=A6=E6=96=B0=E9=97=BB=E7=BD=91=20RSS=20(#14824)=20(#15321?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/routes/byau/namespace.ts | 6 +++ lib/routes/byau/xinwen/index.ts | 72 +++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 lib/routes/byau/namespace.ts create mode 100644 lib/routes/byau/xinwen/index.ts diff --git a/lib/routes/byau/namespace.ts b/lib/routes/byau/namespace.ts new file mode 100644 index 000000000..9b0569213 --- /dev/null +++ b/lib/routes/byau/namespace.ts @@ -0,0 +1,6 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '黑龙江八一农垦大学', + url: 'byau.edu.cn', +}; diff --git a/lib/routes/byau/xinwen/index.ts b/lib/routes/byau/xinwen/index.ts new file mode 100644 index 000000000..4fec6eee0 --- /dev/null +++ b/lib/routes/byau/xinwen/index.ts @@ -0,0 +1,72 @@ +import { Route } from '@/types'; +import cache from '@/utils/cache'; +import got from '@/utils/got'; +import { load } from 'cheerio'; +import { parseDate } from '@/utils/parse-date'; +import timezone from '@/utils/timezone'; + +export const route: Route = { + path: '/news/:type_id', + categories: ['university'], + example: '/byau/news/3674', + parameters: { type_id: '栏目类型(从菜单栏获取对应 ID)' }, + radar: [ + { + source: ['xinwen.byau.edu.cn/:type_id/list.htm'], + target: '/news/:type_id', + }, + ], + name: '新闻网', + maintainers: ['ueiu'], + handler, + url: 'xinwen.byau.edu.cn', + description: `| 学校要闻 | 校园动态 | + | ---- | ----------- | + | 3674 | 3676 |`, +}; + +async function handler(ctx) { + const baseUrl = 'http://xinwen.byau.edu.cn/'; + + const typeID = ctx.req.param('type_id'); + const url = `${baseUrl}${typeID}/list.htm`; + + const response = await got(url); + const $ = load(response.data); + + const list = $('.news') + .toArray() + .map((item) => { + const $$ = load(item); + + const originalItemUrl = $$('a').attr('href'); + // 因为学校要闻的头两个像是固定了跳转专栏页面的,不能相同处理 + const startsWithHttp = originalItemUrl.startsWith('http'); + const itemUrl = startsWithHttp ? originalItemUrl : new URL(originalItemUrl, baseUrl).href; + + return { + title: $$('a').text(), + link: itemUrl, + pubDate: timezone(parseDate($$('.news_meta').text()), +8), + }; + }); + + const items = await Promise.all( + list.map((item) => + cache.tryGet(item.link, async () => { + const response = await got(item.link); + const $ = load(response.data); + + item.description = $('.col_news_con').html(); + + return item; + }) + ) + ); + + return { + title: $('title').text(), + link: url, + item: items, + }; +}