fix: update guokr channel api (#4178)

This commit is contained in:
hoilc
2020-03-08 01:17:25 +08:00
committed by GitHub
parent b6b1cf3d45
commit f38dd3cc56
3 changed files with 18 additions and 17 deletions

View File

@@ -431,7 +431,7 @@ Supported sub-sites
### 果壳网专栏
<Route author="DHPO" example="/guokr/calendar" path="/guokr/:category" :paramsDesc="['专栏类别']">
<Route author="DHPO hoilc" example="/guokr/calendar" path="/guokr/:channel" :paramsDesc="['专栏类别']">
| 物种日历 | 吃货研究所 | 美丽也是技术活 |
| ------- | ---------| ------------ |
| calendar | institute | beauty |

View File

@@ -752,7 +752,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('/guokr/:channel', require('./routes/guokr/calendar'));
// 联合早报
router.get('/zaobao/realtime/:section?', require('./routes/zaobao/realtime'));

View File

@@ -9,23 +9,25 @@ async function loadFullPage(ctx, id) {
return content;
}
const categoryMap = {
calendar: '物种日历',
institute: '吃货研究所',
beauty: '美丽也是技术活',
const channelMap = {
calendar: 'pac',
institute: 'predator',
foodlab: 'predator',
pretty: 'beauty',
};
module.exports = async (ctx) => {
const category = ctx.params.category;
if (categoryMap[category] === undefined) {
throw new Error(`Unknown category ${category}`);
const channel = channelMap[ctx.params.channel] ? channelMap[ctx.params.channel] : ctx.params.channel;
const response = await got.get(`https://www.guokr.com/apis/minisite/article.json?retrieve_type=by_wx&channel_key=${channel}&offset=0&limit=10`);
const items = response.data.result;
if (items.length === 0) {
throw 'Unknown channel';
}
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 channel_name = items[0].channels[0].name;
const channel_url = items[0].channels[0].url;
const result = await Promise.all(
items.map(async (item) => ({
@@ -38,9 +40,8 @@ module.exports = async (ctx) => {
);
ctx.state.data = {
title: `果壳网 ${categoryMap[category]}`,
link: 'https://www.guokr.com/calendar',
description: `果壳网 ${categoryMap[category]}`,
title: `果壳网 ${channel_name}`,
link: channel_url,
item: result,
};
};