add dcard posts

This commit is contained in:
DIYgod
2019-02-28 12:14:12 +08:00
parent 2551a5fa78
commit 6057c18963
4 changed files with 65 additions and 0 deletions

View File

@@ -622,6 +622,10 @@ RSSHub 提供下列 API 接口:
<route name="虎扑BBS步行街" author="LogicJake" example="/hupu/bxj/bxj/2" path="/hupu/bxj/:id/:order?" :paramsDesc="['栏目id可在栏目 URL 找到', '排序方式1最新回帖默认2最新发帖']"/>
### Dcard
<route name="帖子" author="DIYgod" example="/dcard/posts/popular" path="/dcard/posts/:type?" :paramsDesc="['排序popular 熱門latest 最新,默認為 latest']"/>
## 编程
### 掘金

View File

@@ -1093,4 +1093,7 @@ router.get('/instapaper/person/:name', require('./routes/instapaper/person'));
router.get('/ui-cn/article', require('./routes/ui-cn/article'));
router.get('/ui-cn/user/:id', require('./routes/ui-cn/user'));
// Dcard
router.get('/dcard/posts/:type?', require('./routes/dcard/posts'));
module.exports = router;

24
lib/routes/dcard/posts.js Normal file
View File

@@ -0,0 +1,24 @@
const axios = require('../../utils/axios');
const utils = require('./utils');
module.exports = async (ctx) => {
const type = ctx.params.type || 'latest';
const response = await axios({
method: 'get',
url: `https://www.dcard.tw/_api/posts?popular=${type === 'popular' ? 'true' : 'false'}&limit=30`,
headers: {
Referer: `https://www.dcard.tw/f?latest=${type === 'popular' ? 'false' : 'true'}`,
},
});
const data = response.data;
console.log(`https://www.dcard.tw/_api/posts?popular=${type === 'popular' ? 'true' : 'false'}&limit=30`, `https://www.dcard.tw/f?latest=${type === 'popular' ? 'false' : 'true'}`);
ctx.state.data = {
title: `Dcard ${type === 'popular' ? '熱門' : '最新'}`,
link: 'https://www.dcard.tw',
description: '不想錯過任何有趣的話題嗎?趕快加入我們吧!',
item: await utils.ProcessFeed(data, ctx.cache),
};
};

34
lib/routes/dcard/utils.js Normal file
View File

@@ -0,0 +1,34 @@
const axios = require('../../utils/axios');
module.exports = {
ProcessFeed: async (list, cache) => {
const result = await Promise.all(
list.map(async (item) => {
const link = `https://www.dcard.tw/f/funny/p/${item.id}-${encodeURIComponent(item.title)}`;
const content = await cache.tryGet(`dcard${item.id}`, async () => {
const response = await axios({
method: 'get',
url: `https://www.dcard.tw/_api/posts/${item.id}`,
headers: {
Referer: link,
},
});
return response.data.content.replace(/(https?:\/\/i\.imgur\.com\/(.*?)\.(jpg|png))/g, (match, p1) => `<img src="${p1}">`).replace(/(\r\n|\r|\n)+/g, '<br>');
});
const single = {
title: item.title,
link: link,
description: content,
author: item.school || '匿名',
guid: item.id,
};
return Promise.resolve(single);
})
);
return result;
},
};