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

@@ -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,
};
};