feat: add kaggle 讨论区 (#3275)

This commit is contained in:
Chenyang Shi
2019-10-18 12:02:41 +08:00
committed by DIYgod
parent 36b870543c
commit 7f41277971
3 changed files with 83 additions and 0 deletions

View File

@@ -154,6 +154,17 @@ GitHub 官方也提供了一些 RSS:
</Route>
## kaggle
### Discussion
<Route author="LogicJake" example="/kaggle/discussion/387811/active" path="/kaggle/discussion/:forumId/:sort?" :paramsDesc="['讨论区 id, 打开网页请求, 搜索 forumId', '排序方式见下表, 默认为 hot']">
| hot | recent | new | top | active |
| ------- | --------------- | --------------- | ---------- | ------------- |
| Hotness | Recent Comments | Recently Posted | Most Votes | Most Comments |
</Route>
## LeetCode
### 文章

View File

@@ -1847,4 +1847,7 @@ router.get('/4gamers/tag/:tag', require('./routes/4gamers/tag'));
// 大麦网
router.get('/damai/activity/:city/:category/:subcategory/:keyword?', require('./routes/damai/activity'));
// kaggle
router.get('/kaggle/discussion/:forumId/:sort?', require('./routes/kaggle/discussion'));
module.exports = router;

View File

@@ -0,0 +1,69 @@
const got = require('@/utils/got');
const url = require('url');
const rootUrl = 'https://www.kaggle.com/';
const sortCodes = {
hot: 'Hotness',
recent: 'Recent Comments',
new: 'Recently Posted',
top: 'Most Votes',
active: 'Most Comments',
};
module.exports = async (ctx) => {
const sort = ctx.params.sort || 'hot';
const forumId = ctx.params.forumId;
const sortText = sortCodes[sort];
const response = await got({
method: 'get',
url: `https://www.kaggle.com/forums/${forumId}/topics.json?sortBy=${sort}&group=all&page=1&pageSize=20&category=all`,
headers: {
Referer: rootUrl,
},
});
const topics = response.data.topics;
let name;
const out = await Promise.all(
topics.map(async (item) => {
const title = item.title;
const id = item.id;
const author = item.userAvatar.displayName;
const topicUrl = url.resolve(rootUrl, item.topicUrl.split('#')[0]);
name = item.topicUrl.split('/')[2];
const apiUrl = `https://www.kaggle.com/topics/${id}.json`;
const cache = await ctx.cache.get(apiUrl);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const response = await got({
method: 'get',
url: apiUrl,
headers: {
Referer: topicUrl,
},
});
const content = response.data.comment.content;
const single = {
link: topicUrl,
title: title,
author: author,
description: content,
};
ctx.cache.set(apiUrl, JSON.stringify(single));
return Promise.resolve(single);
})
);
ctx.state.data = {
title: `${name} Topic-${sortText}`,
link: url.resolve(rootUrl, `c/${name}/discussion`),
item: out,
};
};