feat: 果壳 物种日历 (#2768)

* feat:果壳 物种日历

* feat:将‘果壳 物种日历’扩展至吃货研究所,美丽也是技术活

* 全文获取使用缓存
This commit is contained in:
Jimmy Wang
2019-08-05 11:32:37 +08:00
committed by DIYgod
parent cba113f703
commit f26cafb6c7
3 changed files with 55 additions and 0 deletions

View File

@@ -539,6 +539,14 @@ type 为 all 时category 参数不支持 cost 和 free
<Route author="alphardex" example="/guokr/scientific" path="/guokr/scientific"/>
### 果壳网专栏
<Route author="DHPO" example="/guokr/calendar" path="/guokr/:category" :paramsDesc="['专栏类别']">
| 物种日历 | 吃货研究所 | 美丽也是技术活 |
| ------- | ---------| ------------ |
| calendar | institute | beauty |
</Route>
## 后续
### Live

View File

@@ -696,6 +696,7 @@ router.get('/ifanr/:channel?', require('./routes/ifanr/index'));
// 果壳网
router.get('/guokr/scientific', require('./routes/guokr/scientific'));
router.get('/guokr/:category', require('./routes/guokr/calendar'));
// 联合早报
router.get('/zaobao/realtime/:type?', require('./routes/zaobao/realtime'));

View File

@@ -0,0 +1,46 @@
const got = require('@/utils/got');
async function loadFullPage(ctx, id) {
const link = `https://apis.guokr.com/minisite/article/${id}.json`;
const content = await ctx.cache.tryGet(link, async () => {
const res = await got.get(link);
return res.data.result.content;
});
return content;
}
const categoryMap = {
calendar: '物种日历',
institute: '吃货研究所',
beauty: '美丽也是技术活',
};
module.exports = async (ctx) => {
const category = ctx.params.category;
if (categoryMap[category] === undefined) {
throw new Error(`Unknown category ${category}`);
}
const response = await got.get(`https://www.guokr.com/${category}`);
const rule = /(?<=<script>\s*window\.INITIAL_STORE=)[\s\S]*?(?=<\/script>)/g; // 在某个script标签下可以找到文章信息
const data = JSON.parse(rule.exec(response.data)[0]);
const items = data[`${category}ArticleListStore`].articleList;
const result = await Promise.all(
items.map(async (item) => ({
title: item.title,
description: await loadFullPage(ctx, item.id), // Mercury 无法正确解析全文,故这里手动加载
pubDate: item.date_published,
link: item.url,
author: item.external_author.nickname,
}))
);
ctx.state.data = {
title: `果壳网 ${categoryMap[category]}`,
link: 'https://www.guokr.com/calendar',
description: `果壳网 ${categoryMap[category]}`,
item: result,
};
};