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 | | 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/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')); router.get('/zaobao/realtime/:section?', require('./routes/zaobao/realtime'));

View File

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