添加掘金标签支持 (#506)

兼容旧的分类获取方式,改用tags标签获取更多掘金主题内容。
ps. 首页分类数据与标签数据一致
This commit is contained in:
Jiean Zeng
2018-08-22 11:58:38 +08:00
committed by DIYgod
parent 1f75832940
commit b020298ea5
4 changed files with 84 additions and 0 deletions

View File

@@ -71,6 +71,7 @@ RSSHub 是一个轻量、易于扩展的 RSS 生成器,可以给任何奇奇
- 电台节目
- 掘金
- 分类
- 标签
- 简书
- 首页
- 7 日热门

View File

@@ -665,6 +665,14 @@ rid: 排行榜分区 id默认 0
| -------- | ------- | --- | ------- | ------ | ------- | -------- | ------- | -------- |
| frontend | android | ios | backend | design | product | freebie | article | ai |
### 标签
举例: [https://rsshub.app/juejin/tag/架构](https://juejin.im/tag/架构)
路由: `/juejin/tag/:tag`
参数: tag标签名可在标签 URL 中找到
## 简书
### 首页

View File

@@ -126,6 +126,7 @@ router.get('/ncm/djradio/:id', require('./routes/ncm/djradio'));
// 掘金
router.get('/juejin/category/:category', require('./routes/juejin/category'));
router.get('/juejin/tag/:tag', require('./routes/juejin/tag'));
// 自如
router.get('/ziroom/room/:city/:iswhole/:room/:keyword', require('./routes/ziroom/room'));

74
routes/juejin/tag.js Normal file
View File

@@ -0,0 +1,74 @@
const axios = require('../../utils/axios');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const tag = ctx.params.tag;
const idResponse = await axios({
method: 'get',
url: 'https://gold-tag-ms.juejin.im/v1/tags',
headers: {
Referer: `https://juejin.im/tag/${encodeURI(tag)}`,
'X-Juejin-Client': '',
'X-Juejin-Src': 'web',
'X-Juejin-Token': '',
'X-Juejin-Uid': '',
},
});
const cat = idResponse.data.d.tags.filter((item) => item.title === tag)[0];
const id = cat.id;
const response = await axios({
method: 'get',
url: `https://timeline-merger-ms.juejin.im/v1/get_tag_entry?src=web&tagId=${id}&page=0&pageSize=10&sort=rankIndex`,
headers: {
Referer: `https://juejin.im/tag/${encodeURI(tag)}`,
},
});
let originalData = [];
if (response.data.d && response.data.d.entrylist) {
originalData = response.data.d && response.data.d.entrylist.slice(0, 10);
}
const resultItems = await Promise.all(
originalData.map(async (item) => {
const resultItem = {
title: item.title,
description: `${(item.content || item.summaryInfo || '无描述').replace(/[\x00-\x08\x0b-\x0c\x0e-\x1f\x7f]/g, '')}`,
pubDate: new Date(item.createdAt).toUTCString(),
link: item.originalUrl,
};
if (item.type === 'post') {
const key = 'juejin' + resultItem.link;
const value = await ctx.cache.get(key);
if (value) {
resultItem.description = value;
} else {
const detail = await axios({
method: 'get',
url: item.originalUrl,
headers: {
Referer: item.originalUrl,
},
});
const content = cheerio.load(detail.data);
resultItem.description = content('.article-content')
.html()
.replace(/[\x00-\x08\x0b-\x0c\x0e-\x1f\x7f]/g, '')
.replace(/(<img.*?)(data-src)(.*?>)/g, '$1src$3');
ctx.cache.set(key, resultItem.description, 24 * 60 * 60);
}
}
return Promise.resolve(resultItem);
})
);
ctx.state.data = {
title: `掘金${cat.title}`,
link: `https://juejin.im/tag/${encodeURI(tag)}`,
description: `掘金${cat.title}`,
item: resultItems,
};
};