add 观察者风闻话题

This commit is contained in:
Fan Shen
2019-05-11 13:38:42 +08:00
parent e978b81f04
commit b75fcee064
3 changed files with 59 additions and 0 deletions

View File

@@ -12,6 +12,10 @@
<Route name="7x24小时快讯" author="occupy5" example="/fx678/kx" path="/fx678/kx" />
## 观察者风闻话题
<Route name="观察者风闻话题" author="occupy5" example="/guancha/topic/113" path="/guancha/topic/:id" :paramsDesc="['话题id 可在URL中找到']" />
## 99% Invisible
<Route name="Transcript" author="Ji4n1ng" example="/99percentinvisible/transcript" path="/99percentinvisible/transcript"/>

View File

@@ -1347,4 +1347,7 @@ router.get('/paidai', require('./routes/paidai/index'));
router.get('/paidai/bbs', require('./routes/paidai/bbs'));
router.get('/paidai/news', require('./routes/paidai/news'));
// 观察者风闻话题
router.get('/guancha/topic/:id', require('./routes/guancha/topic'));
module.exports = router;

View File

@@ -0,0 +1,52 @@
const cheerio = require('cheerio');
const axios = require('../../utils/axios');
const url = require('url');
const host = 'https://user.guancha.cn';
module.exports = async (ctx) => {
const id = ctx.params.id;
const link = `https://user.guancha.cn/topic/attention-topic?follow-topic-id=${id}`;
const res = await axios.get(encodeURI(link));
const $ = cheerio.load(res.data);
// 话题名称
const title = $('.topic-main').text();
// 文章列表
const list = $('.article-list .list-item h4')
.find('a')
.map((i, e) => $(e).attr('href'))
.get();
const out = await Promise.all(
list.map(async (itemUrl) => {
// 将相对链接替换为绝对链接
const absoluteUrl = url.resolve(host, itemUrl);
const res = await axios.get(absoluteUrl);
const $ = cheerio.load(res.data);
const item = {
title: $('.article-content h1').text(),
author: $('.user-main h4')
.first()
.find('a')
.text(),
link: absoluteUrl,
description: $('.article-txt-content').html(),
pubDate: $('.user-main .time1').text(),
};
const cache = await ctx.cache.get(absoluteUrl);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
ctx.cache.set(absoluteUrl, JSON.stringify(item), 24 * 60 * 60);
return Promise.resolve(item);
})
);
ctx.state.data = {
title: `风闻话题-${title}`,
link: link,
item: out,
};
};