Files
RSSHub/lib/v2/pku/nsd.js
MisLink df6f4caf8b feat(route): add 北京大学国家发展研究院 - 观点 (#9804)
* feat(route): add 北京大学国家发展研究院 - 观点

Signed-off-by: MisLink <gjq.uoiai@outlook.com>

* Fix https issue

Signed-off-by: MisLink <gjq.uoiai@outlook.com>

* fetch full text from wechat-mp and pku news

Signed-off-by: MisLink <gjq.uoiai@outlook.com>

* Fix ci

Signed-off-by: MisLink <gjq.uoiai@outlook.com>

* refactor: sort new route
2022-05-27 19:12:04 +08:00

70 lines
2.4 KiB
JavaScript

const got = require('@/utils/got');
const cheerio = require('cheerio');
const { finishArticleItem } = require('@/utils/wechat-mp');
const { parseDate } = require('@/utils/parse-date');
const baseUrl = 'https://nsd.pku.edu.cn/sylm/gd/';
const pageType = (href) => {
if (!href.startsWith('http')) {
return 'in-site';
}
const url = new URL(href);
if (url.hostname === 'mp.weixin.qq.com') {
return 'wechat-mp';
} else if (url.hostname === 'news.pku.edu.cn') {
return 'pku-news';
} else {
return 'unknown';
}
};
module.exports = async (ctx) => {
const response = await got({ url: baseUrl, https: { rejectUnauthorized: false } });
const $ = cheerio.load(response.data);
const list = $('div.maincontent > ul > li')
.map((_index, item) => {
const href = $(item).find('a').attr('href');
const type = pageType(href);
return {
title: $(item).find('a').text().trim(),
link: type === 'in-site' ? baseUrl + href : href,
pubDate: parseDate($(item).find('span').first().text(), 'YYYY-MM-DD'),
type,
};
})
.get();
const items = await Promise.all(
list.map((item) => {
switch (item.type) {
case 'wechat-mp':
return finishArticleItem(ctx, item);
case 'pku-news':
return ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got({ url: item.link, https: { rejectUnauthorized: false } });
const content = cheerio.load(detailResponse.data);
item.description = content('div.pageArticle > div.col.lf').html();
return item;
});
case 'in-site':
return ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got({ url: item.link, https: { rejectUnauthorized: false } });
const content = cheerio.load(detailResponse.data);
item.description = content('div.article').html();
return item;
});
default:
return item;
}
})
);
ctx.state.data = {
title: '观点 - 北京大学国家发展研究院',
link: baseUrl,
item: items,
};
};