Skip to content

Commit

Permalink
feat(route/tongji/sem): Add description for articles (DIYgod#17528)
Browse files Browse the repository at this point in the history
  • Loading branch information
sitdownkevin authored Nov 11, 2024
1 parent ef86602 commit 47a9bc5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
28 changes: 27 additions & 1 deletion lib/routes/tongji/sem/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
14 changes: 10 additions & 4 deletions lib/routes/tongji/sem/notice.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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,
};
}

0 comments on commit 47a9bc5

Please sign in to comment.