feat(route/tongji/sem): Add description for articles (#17528)

This commit is contained in:
XU Ke
2024-11-11 11:52:14 +08:00
committed by GitHub
parent ef86602853
commit 47a9bc50f0
2 changed files with 37 additions and 5 deletions
+27 -1
View File
@@ -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;
}
+10 -4
View File
@@ -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,
};
}