From 73c9e1fdfd15e3f7f3ec6a3f1c1ca4fd2606be87 Mon Sep 17 00:00:00 2001 From: Ishirai Date: Tue, 28 May 2024 10:40:15 +0800 Subject: [PATCH] fix(route): route jiaowu and xxgk for Donghua University (#15721) * fix: update name url for Donghua University * fix: route /dhu/jiaowu/news and /dhu/jiaowu/xxgk/news and fetch content as description * fix: camelCase / fetch content and meta at the same time * fix: return item using cache.tryGet --- lib/routes/dhu/jiaowu/news.ts | 75 ++++++++++++++++++++++++----------- lib/routes/dhu/namespace.ts | 2 +- lib/routes/dhu/xxgk/news.ts | 66 +++++++++++++++++++++--------- 3 files changed, 100 insertions(+), 43 deletions(-) diff --git a/lib/routes/dhu/jiaowu/news.ts b/lib/routes/dhu/jiaowu/news.ts index 4e94d1103..704096d5d 100644 --- a/lib/routes/dhu/jiaowu/news.ts +++ b/lib/routes/dhu/jiaowu/news.ts @@ -2,13 +2,15 @@ import { Route } from '@/types'; import got from '@/utils/got'; import { load } from 'cheerio'; import { parseDate } from '@/utils/parse-date'; -import timezone from '@/utils/timezone'; +import cache from '@/utils/cache'; -const base_url = 'https://jw.dhu.edu.cn'; +const baseUrl = 'https://jw.dhu.edu.cn'; const map = { student: '/tzggwxszl/list.htm', teacher: '/tzggwjszl/list.htm', + class: '/tzggwxkzl_19850/list.htm', + fxzy: '/fxzy/list.htm', }; export const route: Route = { path: '/jiaowu/news/:type?', @@ -26,32 +28,59 @@ export const route: Route = { name: '教务处通知', maintainers: ['KiraKiseki'], handler, - description: `| 学生专栏 | 教师专栏 | - | -------- | -------- | - | student | teacher |`, + description: `| 学生专栏 | 教师专栏 | 选课专栏(仅选课期间开放) | 辅修专业 | + | -------- | -------- | -------- | -------- | + | student | teacher | class | fxzy |`, }; async function handler(ctx) { - const type = ctx.req.param('type'); - const link = Object.hasOwn(map, type) ? `${base_url}${map[type]}` : `${base_url}/tzggwxszl/list.htm`; - const response = await got({ - method: 'get', - url: link, - headers: { - Referer: base_url, - }, - }); + const type = ctx.req.param('type') || 'student'; + const link = `${baseUrl}${map[type]}`; + const { data: response } = await got(link); + + const $ = load(response); + + const items = await Promise.all( + $('.list2 > li') + .toArray() + .map(async (item) => { + item = $(item); + const newsTitle = item.find('.news_title > a'); + const newsMeta = item.find('.news_meta'); + + // article meta + const link = newsTitle.attr('href'); + const title = newsTitle.text(); + const pubDate = parseDate(newsMeta.text(), 'YYYY-MM-DD', 'zh-cn'); + + // fetch article content and return item using cache.tryGet + // url as cache key + const url = `${baseUrl}${link}`; + return await cache.tryGet(url, async () => { + // fetch article content + // some contents are only available for internal network + let description = ''; + try { + const { data: response } = await got(url); + const $ = load(response); + description = $('.wp_articlecontent').first().html() ?? ''; + } catch { + description = ''; + } + + return { + title, + link, + pubDate, + description, + }; + }); + }) + ); - const $ = load(response.data); return { - link: base_url, title: '东华大学教务处-' + $('.col_title').text(), - item: $('.list_item') - .map((_, elem) => ({ - link: new URL($('a', elem).attr('href'), base_url), - title: $('a', elem).attr('title'), - pubDate: timezone(parseDate($('.Article_PublishDate', elem).text()), +8), - })) - .get(), + link, + item: items, }; } diff --git a/lib/routes/dhu/namespace.ts b/lib/routes/dhu/namespace.ts index 6a77a9b47..af17525bc 100644 --- a/lib/routes/dhu/namespace.ts +++ b/lib/routes/dhu/namespace.ts @@ -2,5 +2,5 @@ import type { Namespace } from '@/types'; export const namespace: Namespace = { name: '东华大学', - url: 'gs.dhu.edu.cn', + url: 'www.dhu.edu.cn', }; diff --git a/lib/routes/dhu/xxgk/news.ts b/lib/routes/dhu/xxgk/news.ts index 4a1b15e34..d911c41f5 100644 --- a/lib/routes/dhu/xxgk/news.ts +++ b/lib/routes/dhu/xxgk/news.ts @@ -2,9 +2,10 @@ import { Route } from '@/types'; import got from '@/utils/got'; import { load } from 'cheerio'; import { parseDate } from '@/utils/parse-date'; -import timezone from '@/utils/timezone'; +import cache from '@/utils/cache'; -const base_url = 'https://xxgk.dhu.edu.cn/1737/list.htm'; +const siteUrl = 'https://xxgk.dhu.edu.cn'; +const baseUrl = 'https://xxgk.dhu.edu.cn/1737/list.htm'; export const route: Route = { path: '/xxgk/news', @@ -25,25 +26,52 @@ export const route: Route = { }; async function handler() { - const link = base_url; - const response = await got({ - method: 'get', - url: link, - headers: { - Referer: base_url, - }, - }); + const link = baseUrl; + const { data: response } = await got(link); + + const $ = load(response); + + const items = await Promise.all( + $('.cols_list > li') + .toArray() + .map(async (item) => { + item = $(item); + const colsTitle = item.find('.cols_title > a'); + const colsMeta = item.find('.cols_meta'); + + // article meta + const link = colsTitle.attr('href'); + const title = colsTitle.text(); + const pubDate = parseDate(colsMeta.text(), 'YYYY-MM-DD', 'zh-cn'); + + // fetch article content and return item using cache.tryGet + // url as cache key + const url = `${siteUrl}${link}`; + return await cache.tryGet(url, async () => { + // fetch article content + // some contents are only available for internal network + let description = ''; + try { + const { data: response } = await got(url); + const $ = load(response); + description = $('.wp_articlecontent').first().html() ?? ''; + } catch { + description = ''; + } + + return { + title, + link, + pubDate, + description, + }; + }); + }) + ); - const $ = load(response.data); return { - link: base_url, title: '东华大学信息公开网-最新公开信息', - item: $('.cols') - .map((_, elem) => ({ - link: new URL($('a', elem).attr('href'), base_url), - title: $('a', elem).attr('title'), - pubDate: timezone(parseDate($('.cols_meta', elem).text()), +8), - })) - .get(), + link, + item: items, }; }