Files
RSSHub/lib/v2/shu/index.js
T
东风有意 c41d23d043 fix(route): shu 上海大学 (#12803)
* fix(route): shu 上海大学

* Optimize code and update documentation

- shu/index.js & shu/jwc.js
    - use @/utils/parse-date to parse date
    - change "item.description" from text to HTML
    - change "url.resolve" (deprecated) to "new URL"
    - change the way of mapping "ctx.params.type" to link
        - now it accepts more route types
- university.md
    - update doc for new types
- router.js
    - add an alias because the website changed its host

* Update lib/routes/universities/shu/index.js

* Update lib/routes/universities/shu/jwc.js

* Update docs/university.md

* refactor: migrate to v2

---------
2023-07-18 21:29:18 +08:00

49 lines
1.5 KiB
JavaScript

const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const host = 'https://www.shu.edu.cn/';
const alias = new Map([
['news', 'zhxw'], // 综合新闻
['research', 'kydt1'], // 科研动态
['kydt', 'kydt1'], // 科研动态
['notice', 'tzgg'], // 通知公告
['important', 'zyxw'], // 重要新闻
]);
module.exports = async (ctx) => {
const type = ctx.params.type || 'news';
const link = `https://www.shu.edu.cn/${alias.get(type) || type}.htm`;
const respond = await got.get(link);
const $ = cheerio.load(respond.data);
const title = $('title').text();
const list = $('.ej_main .list')
.find('li')
.slice(0, 5)
.toArray()
.map((ele) => ({
title: $(ele).find('.bt').text(),
link: new URL($(ele).find('a').attr('href'), host).href,
date: $(ele).find('.sj').text(),
}));
const all = await Promise.all(
list.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const response = await got.get(item.link);
const $ = cheerio.load(response.data);
item.author = $('.xx>:nth-child(2)').text().trim().slice(3); // 投稿:xxx
item.pubDate = parseDate(item.date, 'YYYY.MM.DD');
item.description = $('.v_news_content').html() || item.title;
return item;
})
)
);
ctx.state.data = {
title,
link,
item: all,
};
};