mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-09 14:40:23 +08:00
feat: add kaggle 讨论区 (#3275)
This commit is contained in:
69
lib/routes/kaggle/discussion.js
Normal file
69
lib/routes/kaggle/discussion.js
Normal 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,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user