feat: 增加 Dcard 板块支持 (#1655)

close #1614
This commit is contained in:
Henry Wang
2019-03-04 12:12:07 +08:00
committed by DIYgod
parent 394fb82899
commit 7a89b9de5f
5 changed files with 91 additions and 48 deletions
+3 -1
View File
@@ -636,7 +636,9 @@ RSSHub 提供下列 API 接口:
### Dcard
<route name="帖子" author="DIYgod" example="/dcard/posts/popular" path="/dcard/posts/:type?" :paramsDesc="['排序,popular 熱門;latest 最新,默認為 latest']"/>
<route name="首頁帖子" author="DIYgod" example="/dcard/posts/popular" path="/dcard/posts/:type?" :paramsDesc="['排序,popular 熱門;latest 最新,默認為 latest']"/>
<route name="板塊帖子" author="HenryQW" example="/dcard/funny/popular" path="/dcard/:section/:type?" :paramsDesc="['板塊名稱,URL 中獲得', '排序,popular 熱門;latest 最新,默認為 latest']"/>
## 编程
+1 -1
View File
@@ -1099,7 +1099,7 @@ 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'));
router.get('/dcard/:section/:type?', require('./routes/dcard/section'));
// 12306
router.get('/12306/zxdt/:id?', require('./routes/12306/zxdt'));
-23
View File
@@ -1,23 +0,0 @@
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;
ctx.state.data = {
title: `Dcard ${type === 'popular' ? '熱門' : '最新'}`,
link: 'https://www.dcard.tw',
description: '不想錯過任何有趣的話題嗎?趕快加入我們吧!',
item: await utils.ProcessFeed(data, ctx.cache),
};
};
+49
View File
@@ -0,0 +1,49 @@
const axios = require('../../utils/axios');
const utils = require('./utils');
module.exports = async (ctx) => {
const { type, section = 'posts' } = ctx.params;
let link = `https://www.dcard.tw/f`;
let api = `https://www.dcard.tw/_api`;
let title = `Dcard - `;
if (section !== 'posts' && section !== 'popular' && section !== 'latest') {
link += `/${section}`;
api += `/forums/${section}`;
title += `${section} - `;
}
api += `/posts`;
if (type === 'popular') {
link += '?latest=false';
api += '?popular=true';
title += '熱門';
} else if (type === 'posts' || !type) {
link += '?latest=true';
api += '?popular=false';
title += '最新';
}
const response = await axios({
method: 'get',
url: `${api}&limit=30`,
headers: {
Referer: link,
},
});
const data = response.data;
const items = await utils.ProcessFeed(data, ctx.cache);
ctx.state.data = {
title,
link,
description: '不想錯過任何有趣的話題嗎?趕快加入我們吧!',
item: items,
};
};
+38 -23
View File
@@ -1,34 +1,49 @@
const axios = require('../../utils/axios');
const cheerio = require('cheerio');
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 ProcessFeed = async (list, cache) => {
const result = await Promise.all(
list.map(async (item) => {
const link = `https://www.dcard.tw/f/${item.forumAlias}/p/${item.id}`;
const content = await cache.tryGet(`dcard${item.id}`, async () => {
const response = await axios({
const content = await cache.tryGet(`dcard${item.id}`, async () => {
let response;
// try catch 处理被删除的帖子
try {
response = await axios({
method: 'get',
url: `https://www.dcard.tw/_api/posts/${item.id}`,
url: link,
headers: {
Referer: link,
Referer: 'https://www.dcard.tw/f',
},
});
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,
};
const $ = cheerio.load(response.data);
return Promise.resolve(single);
})
);
return $('.Post_content_NKEl9d > div:nth-child(1)').html();
} catch (error) {
return '';
}
});
return result;
},
const single = {
title: `「${item.forumName}」${item.title}`,
link,
description: content,
author: item.school || '匿名',
pubDate: item.createdAt,
guid: item.id,
};
return Promise.resolve(single);
})
);
return result.filter((f) => f.description);
};
module.exports = {
ProcessFeed,
};