feat: add 吹牛部落栏目更新 (#3045)

* feat: add 吹牛部落栏目更新

* add cache
This commit is contained in:
Chenyang Shi
2019-09-12 12:07:02 +08:00
committed by DIYgod
parent c0c1c990ff
commit 2094c53c05
5 changed files with 110 additions and 0 deletions

View File

@@ -415,3 +415,7 @@ RSSHub 支持 `memory` 和 `redis` 两种缓存方式
- 邮箱 邮件列表路由:
- `EMAIL_CONFIG_{email}`: 邮箱设置,替换 `{email}` 为 邮箱账号,邮件账户的 `@` 替换为 `.`,例如 `EMAIL_CONFIG_xxx.qq.com`。内容格式为 `password=密码&host=服务器&port=端口`,例如 `password=123456&host=imap.qq.com&port=993`
- 吹牛部落 栏目更新
- `CHUINIU_MEMBER`: 吹牛部落登录后的 x-member获取方式1. 登陆后点开文章正文 2. 打开控制台 3. 刷新 4. 找到 <http://api.duanshu.com/h5/content/detail/> 开头的请求 5. 找到请求头中的 x-member

View File

@@ -52,6 +52,18 @@ pageClass: routes
</Route>
## 吹牛部落
### 栏目
<Route author="LogicJake" example="/chuiniu/column/0b1c4cf6e7f24e8da11e37882de73704" path="/chuiniu/column/:id" :paramsDesc="['栏目 id, 可在对应栏目页 URL 中找到']">
::: warning 注意
正文内容需要用户登录后的 x-member 值,详情见部署页面的配置模块。若无相关配置或 x-member 失效,则只显示文章摘要。
:::
</Route>
## 刺猬猫
### 章节

View File

@@ -89,4 +89,7 @@ module.exports = {
config: email_config,
},
sentry: process.env.SENTRY,
chuiniu: {
member: process.env.CHUINIU_MEMBER,
},
};

View File

@@ -1710,4 +1710,7 @@ router.get('/pintu360/:type?', require('./routes/pintu360/index'));
// engadget中国版
router.get('/engadget-cn', require('./routes/engadget-cn/home'));
// 吹牛部落
router.get('/chuiniu/column/:id', require('./routes/chuiniu/column'));
module.exports = router;

View File

@@ -0,0 +1,88 @@
const got = require('@/utils/got');
const config = require('@/config');
module.exports = async (ctx) => {
const url = 'http://chuiniu.duanshu.com';
const column_id = ctx.params.id;
let response = await got({
method: 'get',
url: 'http://api.duanshu.com/fairy/api/v1/shop/identifier/',
headers: {
Origin: url,
Referer: url,
},
});
const shop_id = response.data.shop_id;
response = await got({
method: 'get',
url: `http://api.duanshu.com/h5/content/free/column/detail/${column_id}?shop_id=${shop_id}`,
headers: {
Referer: url,
},
});
const title = response.data.response.data.title;
const description = response.data.response.data.brief;
response = await got({
method: 'get',
url: `http://api.duanshu.com/h5/content/column/contents?page=1&count=10&column_id=${column_id}&shop_id=${shop_id}`,
headers: {
Referer: url,
},
});
const list = response.data.response.data;
const out = await Promise.all(
list.map(async (item) => {
const title = item.title;
const brief = item.brief;
const up_time = item.up_time;
const content_id = item.content_id;
const item_link = `http://chuiniu.duanshu.com/#/brief/article/${content_id}`;
const cache = await ctx.cache.get(item_link);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const member = config.chuiniu.member;
let content;
if (member === undefined) {
content = brief + '<br>...<br>正文内容需会员登录后查看';
} else {
try {
const response = await got({
method: 'get',
url: `http://api.duanshu.com/h5/content/detail/${content_id}?shop_id=${shop_id}`,
headers: {
Referer: url,
'x-member': member,
},
});
content = response.data.response.data.content;
} catch (error) {
content = brief + '<br>...<br>正文内容需会员登录后查看';
}
}
const single = {
pubDate: new Date(up_time).toUTCString(),
link: item_link,
title: title,
description: content,
};
ctx.cache.set(item_link, JSON.stringify(single));
return Promise.resolve(single);
})
);
ctx.state.data = {
title,
description,
link: url,
item: out,
};
};