Files
RSSHub/lib/v2/ustc/jwc.js
Zhiyang Guo f88e58b493 feat(route): 中国科学技术大学-研究生院/信息科学技术学院/电子工程与信息科学系 (#9666)
* fix(route): 中科大官网通知公告正文内容获取

* refactor: migrate to v2

* feat(route): 中国科学技术大学-研究生院/信息科学技术学院/电子工程与信息科学系

* fix: sort router

* fix: deprecated url.resolve

* fix: fulltext cache

fix: nullish coalescing

fix: data.link

fix: cache.tryGet

* fix: deepscan
2022-05-01 17:36:33 +08:00

58 lines
2.4 KiB
JavaScript

const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const timezone = require('@/utils/timezone');
// const UA = require('@/utils/rand-user-agent')({ browser: 'chrome', os: 'windows', device: 'desktop' });
const noticeUrl = 'https://www.teach.ustc.edu.cn/category/notice';
const noticeType = { teaching: '教学', info: '信息', exam: '考试', exchange: '交流' };
module.exports = async (ctx) => {
const type = ctx.params.type ?? '';
// 发起 HTTP GET 请求
const response = await got({
method: 'get',
/* headers: {
'user-agent': UA,
}, */
url: `${noticeUrl}${type === '' ? '' : '-' + type}`,
});
const $ = cheerio.load(response.data);
let items = $(type === '' ? 'ul[class="article-list with-tag"] > li' : 'ul[class=article-list] > li')
.map(function () {
const child = $(this).children();
const info = {
title: type === '' ? $(child[0]).find('a').text() + ' - ' + $(child[1]).find('a').text() : $(child[0]).find('a').text(),
link: type === '' ? $(child[1]).find('a').attr('href') : $(child[0]).find('a').attr('href'),
pubDate: timezone(parseDate($(this).find('.date').text().trim(), 'YYYY-MM-DD'), +8),
};
return info;
})
.get();
items = await Promise.all(
items
.filter((item) => item.link)
.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const response = await got(item.link);
const $ = cheerio.load(response.data);
// www.teach ?? pms.cmet ?? news
item.description = $('main[class=single]').html() ?? $('.card-footer').html() ?? $('.v_news_content').html();
item.pubDate = $('li[class=meta-date]').text() ? timezone(parseDate($('li[class=meta-date]').text(), 'YYYY-MM-DD HH:mm'), +8) : item.pubDate;
return item;
})
)
);
const desc = type === '' ? '中国科学技术大学教务处 - 通知新闻' : `中国科学技术大学教务处 - ${noticeType[type]}类通知`;
ctx.state.data = {
title: desc,
description: desc,
link: `${noticeUrl}${type === '' ? '' : '-' + type}`,
item: items,
};
};