Skip to content

Commit

Permalink
feat(/scu/scupi): Add route for SCUPI (#17542)
Browse files Browse the repository at this point in the history
* Add route for SCUPI

* Update _utils.ts

Add image for each article
  • Loading branch information
sitdownkevin authored Nov 11, 2024
1 parent 1f05d49 commit 947429a
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/routes/scu/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: '四川大学',
url: 'www.scu.edu.cn',
lang: 'zh-CN',
};
63 changes: 63 additions & 0 deletions lib/routes/scu/scupi/_utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import got from '@/utils/got';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import { config } from '@/config';

export async function getNotifList() {
try {
const response = await got.get('https://scupi.scu.edu.cn/activities/notice', {
headers: {
'User-Agent': config.ua,
},
});
const html = response.body;
const $ = load(html);

const listElement = $('body > div.wrapper > main > section > div > div > div.news > div > ul');
return listElement
.find('article')
.toArray()
.map((articleElement) => {
const titleElement = $(articleElement).find('li > div > div.news-text > h4 > a');
const timeElement = $(articleElement).find('li > div > div.news-text > span');
const imageElement = $(articleElement).find('li > div > div.news-img > a > img');

const link = titleElement.attr('href');
const title = titleElement.attr('title');
const pubDate = timeElement.text().trim();

return {
title,
link,
itunes_item_image: imageElement.attr('src'),
pubDate: parseDate(pubDate, 'YYYY-MM-DD'),
};
});
} catch {
// console.error(error);
}

return [];
}

export async function getArticle(item) {
try {
const response = await got.get(item.link, {
headers: {
'User-Agent': config.ua,
},
});
const html = response.body;
const $ = load(html);
const articleContentElement = $('body > div > main > section > div > div > div.post-content-contaier > div');
const content = articleContentElement.html();
const modifiedContent = content?.replace(/\n/g, '<br>');

item.description = modifiedContent;
return item;
} catch {
// console.error(error);
}

return item;
}
40 changes: 40 additions & 0 deletions lib/routes/scu/scupi/notice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Warning: The author still knows nothing about javascript!

import { getNotifList, getArticle } from './_utils';
import { Route } from '@/types';
import cache from '@/utils/cache';

export const route: Route = {
path: '/scupi',
categories: ['university'],
example: '/scu/scupi',
parameters: {},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
name: '匹兹堡学院通知',
maintainers: ['sitdownkevin'],
url: 'scupi.scu.edu.cn/activities/notice',
handler,
description: ``,
};

async function handler() {
// feed the data to rss
const items = await getNotifList();
const itemsWithContent = await Promise.all(items.map((item) => cache.tryGet(item.link, () => getArticle(item))));

return {
title: '四川大学匹兹堡学院',
description: '四川大学匹兹堡学院官网通知',
language: 'zh-cn',
image: 'https://scupi.scu.edu.cn/wp-content/themes/scupi/img/logo.png',
link: 'https://scupi.scu.edu.cn/',
item: itemsWithContent,
};
}

0 comments on commit 947429a

Please sign in to comment.