Merge branch 'occupy5-guanchazhe' into HEAD

This commit is contained in:
DIYgod
2019-05-12 21:47:49 +08:00
3 changed files with 63 additions and 0 deletions

View File

@@ -290,6 +290,10 @@ type 为 all 时category 参数不支持 cost 和 free
<Route name="首页推荐" author="LogicJake" example="/gushiwen/recommend" path="/gushiwen/recommend"/> <Route name="首页推荐" author="LogicJake" example="/gushiwen/recommend" path="/gushiwen/recommend"/>
## 观察者风闻话题
<Route name="观察者风闻话题" author="occupy5" example="/guancha/topic/113" path="/guancha/topic/:id" :paramsDesc="['话题id 可在URL中找到']" />
## 果壳网 ## 果壳网
<Route name="科学人" author="alphardex" example="/guokr/scientific" path="/guokr/scientific"/> <Route name="科学人" author="alphardex" example="/guokr/scientific" path="/guokr/scientific"/>

View File

@@ -1354,4 +1354,7 @@ router.get('/boc/whpj', require('./routes/boc/whpj'));
// 漫画db // 漫画db
router.get('/manhuadb/comics/:id', require('./routes/manhuadb/comics')); router.get('/manhuadb/comics/:id', require('./routes/manhuadb/comics'));
// 观察者风闻话题
router.get('/guanchazhe/topic/:id', require('./routes/guanchazhe/topic'));
module.exports = router; module.exports = router;

View File

@@ -0,0 +1,56 @@
const cheerio = require('cheerio');
const axios = require('../../utils/axios');
const date = require('../../utils/date');
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 cache = await ctx.cache.get(absoluteUrl);
// 判断缓存
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const res = await axios.get(absoluteUrl);
const $ = cheerio.load(res.data);
const dateStr = $('.user-main .time1').text();
const item = {
title: $('.article-content h1').text(),
author: $('.user-main h4')
.first()
.find('a')
.text(),
link: absoluteUrl,
description: $('.article-txt-content').html(),
// 格式化日期
pubDate: date(dateStr),
};
ctx.cache.set(absoluteUrl, JSON.stringify(item), 24 * 60 * 60);
return Promise.resolve(item);
})
);
ctx.state.data = {
title: `风闻话题-${title}`,
link: link,
item: out,
};
};