From 958e060badc58c7e08aa4c0fd620d51db15b4af7 Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Fri, 10 May 2024 02:46:35 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E6=B8=85=E5=8D=8E?= =?UTF-8?q?=E5=A4=A7=E5=AD=A6=E5=9B=BE=E4=B9=A6=E9=A6=86=E8=B5=84=E6=BA=90?= =?UTF-8?q?=E5=8A=A8=E6=80=81=20(#15533)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/routes/tsinghua/lib/zydt.ts | 129 +++++++++++++++++++++++++++++++ lib/routes/tsinghua/namespace.ts | 8 ++ 2 files changed, 137 insertions(+) create mode 100644 lib/routes/tsinghua/lib/zydt.ts create mode 100644 lib/routes/tsinghua/namespace.ts diff --git a/lib/routes/tsinghua/lib/zydt.ts b/lib/routes/tsinghua/lib/zydt.ts new file mode 100644 index 000000000..3fcd057ff --- /dev/null +++ b/lib/routes/tsinghua/lib/zydt.ts @@ -0,0 +1,129 @@ +import { Route } from '@/types'; + +import cache from '@/utils/cache'; +import got from '@/utils/got'; +import { load } from 'cheerio'; +import { parseDate } from '@/utils/parse-date'; + +export const handler = async (ctx) => { + const { category } = ctx.req.param(); + const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 10; + + const rootUrl = 'https://lib.tsinghua.edu.cn'; + const currentUrl = new URL(`zydt${category ? `/${category}` : ''}.htm`, rootUrl).href; + + const { data: response } = await got(currentUrl); + + const $ = load(response); + + const language = $('html').prop('lang'); + + let items = $('ul.notice-list li') + .slice(0, limit) + .toArray() + .map((item) => { + item = $(item); + + return { + title: item.find('div.notice-list-tt a').text(), + pubDate: parseDate(item.find('div.notice-date').text(), 'YYYY/MM/DD'), + link: new URL(item.find('div.notice-list-tt a').prop('href'), rootUrl).href, + category: item + .find('div.notice-label') + .toArray() + .map((c) => $(c).text()), + language, + }; + }); + + items = await Promise.all( + items.map((item) => + cache.tryGet(item.link, async () => { + const { data: detailResponse } = await got(item.link); + + const $$ = load(detailResponse); + + const title = $$('h2').text(); + const description = $$('div.v_news_content').html(); + + item.title = title; + item.description = description; + item.content = { + html: description, + text: $$('div.v_news_content').text(), + }; + item.language = language; + + return item; + }) + ) + ); + + const title = $('title').text(); + const image = new URL($('div.logo a img').prop('href'), rootUrl).href; + + return { + title, + description: $('META[Name="keywords"]').prop('Content'), + link: currentUrl, + item: items, + allowEmpty: true, + image, + author: title.split(/-/).pop(), + language, + }; +}; + +export const route: Route = { + path: '/lib/zydt/:category?', + name: '图书馆资源动态', + url: 'lib.tsinghua.edu.cn', + maintainers: ['nczitzk'], + handler, + example: '/tsinghua/lib/zydt', + parameters: { category: '分类,默认为空,即全部,可在对应分类页 URL 中找到' }, + description: `:::tip + 若订阅 [清华大学图书馆已购资源动态](https://lib.tsinghua.edu.cn/zydt/yg.htm),网址为 \`https://lib.tsinghua.edu.cn/zydt/yg.htm\`。截取 \`https://lib.tsinghua.edu.cn/zydt\` 到末尾 \`.htm\` 的部分 \`yg\` 作为参数填入,此时路由为 [\`/tsinghua/lib/zydt/yg\`](https://rsshub.app/tsinghua/lib/zydt/yg)。 + ::: + + | 已购 | 试用 | + | ---- | ---- | + | yg | sy | + `, + categories: ['university'], + + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportRadar: true, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['lib.tsinghua.edu.cn/zydt/:category?'], + target: (params) => { + const category = params.category?.replace(/\.htm$/, ''); + + return `/tsinghua/lib/zydt${category ? `/${category}` : ''}`; + }, + }, + { + title: '图书馆资源动态', + source: ['lib.tsinghua.edu.cn/zydt'], + target: '/lib/zydt', + }, + { + title: '图书馆已购资源动态', + source: ['lib.tsinghua.edu.cn/zydt/yg'], + target: '/lib/zydt/yg', + }, + { + title: '图书馆试用资源动态', + source: ['lib.tsinghua.edu.cn/zydt/sy'], + target: '/lib/zydt/sy', + }, + ], +}; diff --git a/lib/routes/tsinghua/namespace.ts b/lib/routes/tsinghua/namespace.ts new file mode 100644 index 000000000..854394d41 --- /dev/null +++ b/lib/routes/tsinghua/namespace.ts @@ -0,0 +1,8 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '清华大学', + url: 'tsinghua.edu.cn', + categories: ['university'], + description: '', +};