From 47a9bc50f0d7afb3caa723019f4943b043cb7b29 Mon Sep 17 00:00:00 2001 From: XU Ke Date: Mon, 11 Nov 2024 11:52:14 +0800 Subject: [PATCH] feat(route/tongji/sem): Add description for articles (#17528) --- lib/routes/tongji/sem/_utils.ts | 28 +++++++++++++++++++++++++++- lib/routes/tongji/sem/notice.ts | 14 ++++++++++---- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/lib/routes/tongji/sem/_utils.ts b/lib/routes/tongji/sem/_utils.ts index 2a45f9c2c..7a1d6c0d2 100644 --- a/lib/routes/tongji/sem/_utils.ts +++ b/lib/routes/tongji/sem/_utils.ts @@ -3,7 +3,7 @@ import { load } from 'cheerio'; import { parseDate } from '@/utils/parse-date'; import { config } from '@/config'; -export async function getNotifByPage() { +export async function getNotifByPage(): Promise<{ title: string; link: string; pubDate: Date }[]> { const pageUrl: string = `https://sem.tongji.edu.cn/semch/category/frontpage/notice`; try { @@ -37,3 +37,29 @@ export async function getNotifByPage() { } return []; } + +export async function getArticle(item) { + const articleUrl: string = item.link; + + if (articleUrl.includes('sem.tongji.edu.cn/semch')) { + // console.log(articleUrl); + + try { + const response = await got.get(articleUrl, { + headers: { + 'User-Agent': config.ua, + }, + }); + + const html = response.body; + const $ = load(html); + + const articleContentElement = $('#page-wrap > div.maim_pages > div > div.leftmain_page > div'); + item.description = articleContentElement ? articleContentElement.html() : ''; + } catch { + // console.error(error); + } + } + + return item; +} diff --git a/lib/routes/tongji/sem/notice.ts b/lib/routes/tongji/sem/notice.ts index 55651dad9..97772afc5 100644 --- a/lib/routes/tongji/sem/notice.ts +++ b/lib/routes/tongji/sem/notice.ts @@ -1,6 +1,7 @@ // Warning: The author still knows nothing about javascript! import { Route } from '@/types'; -import { getNotifByPage } from './_utils'; +import { getNotifByPage, getArticle } from './_utils'; +import cache from '@/utils/cache'; export const route: Route = { path: '/sem', @@ -23,12 +24,17 @@ export const route: Route = { }; async function handler() { - const results = await getNotifByPage(); + const results: { title: string; link: string; pubDate: Date }[] = await getNotifByPage(); + const resultsWithContent = await Promise.all(results.map((item) => cache.tryGet(item.link, () => getArticle(item)))); // feed the data to rss return { title: '同济大学经济与管理学院', - link: 'https://sem.tongji.edu.cn/semch/category/frontpage/notice', - item: results, + description: '同济大学经济与管理学院官网通知', + language: 'zh-cn', + image: 'https://sem.tongji.edu.cn/semch/wp-content/themes/wood-themes-cn/images/pages/page_banner.gif', + logo: 'https://tongji.edu.cn/images/badge.png', + link: 'https://sem.tongji.edu.cn/semch', + item: resultsWithContent, }; }