feat: 增加 Hacker News 内容 (#2253)
* 增加 Hacker News 的热门内容 * 增加 Hacker News 的分类
This commit is contained in:
@@ -117,6 +117,33 @@ GitHub 官方也提供了一些 RSS:
|
||||
|
||||
</Route>
|
||||
|
||||
## Hacker News
|
||||
|
||||
### 分类
|
||||
|
||||
<Route author="cf020031308" example="/hackernews/best/comments" path="/hackernews/:section/:type?" :paramsDesc="['内容分区', '链接类型(可不填)']">
|
||||
|
||||
网站地址:https://news.ycombinator.com/
|
||||
|
||||
| 内容分区 | section |
|
||||
| -------- | ----------------------------------- |
|
||||
| index | https://news.ycombinator.com/ |
|
||||
| new | https://news.ycombinator.com/newest |
|
||||
| past | https://news.ycombinator.com/front |
|
||||
| ask | https://news.ycombinator.com/ask |
|
||||
| show | https://news.ycombinator.com/show |
|
||||
| jobs | https://news.ycombinator.com/jobs |
|
||||
| best | https://news.ycombinator.com/best |
|
||||
|
||||
> 网站有默认的 RSS:https://news.ycombinator.com/rss 内容同 index,应优先考虑
|
||||
|
||||
| 链接类型 | type |
|
||||
| -------- | ----------------------------- |
|
||||
| story | 默认值,链向用户分享的地址 |
|
||||
| comments | 链向 Hacker News 上的讨论页面 |
|
||||
|
||||
</Route>
|
||||
|
||||
## LeetCode
|
||||
|
||||
### 文章
|
||||
|
||||
@@ -1067,6 +1067,9 @@ router.get('/whalegogo/home', require('./routes/whalegogo/home'));
|
||||
router.get('/aisixiang/column/:id', require('./routes/aisixiang/column'));
|
||||
router.get('/aisixiang/ranking/:type?/:range?', require('./routes/aisixiang/ranking'));
|
||||
|
||||
// Hacker News
|
||||
router.get('/hackernews/:section/:type?', require('./routes/hackernews/story'));
|
||||
|
||||
// LeetCode
|
||||
router.get('/leetcode/articles', require('./routes/leetcode/articles'));
|
||||
router.get('/leetcode/submission/:country/:user', require('./routes/leetcode/submission'));
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
const axios = require('@/utils/axios');
|
||||
const cheerio = require('cheerio');
|
||||
|
||||
const host = 'https://news.ycombinator.com';
|
||||
const paths = {
|
||||
index: '/',
|
||||
new: '/newest',
|
||||
past: '/front',
|
||||
ask: '/ask',
|
||||
show: '/show',
|
||||
jobs: '/jobs',
|
||||
best: '/best',
|
||||
};
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const section = ctx.params.section;
|
||||
const showStory = ctx.params.type !== 'comments';
|
||||
const path = paths[section];
|
||||
const url = `${host}${path}`;
|
||||
const response = await axios.get(url);
|
||||
const $ = cheerio.load(response.data);
|
||||
|
||||
const items = $('a.storylink')
|
||||
.map(function() {
|
||||
return {
|
||||
title: $(this).text(),
|
||||
link: $(this).attr('href'),
|
||||
author: 'Hacker News',
|
||||
description: '',
|
||||
};
|
||||
})
|
||||
.get();
|
||||
|
||||
$('a.hnuser').each(function(i) {
|
||||
items[i].author = $(this).text();
|
||||
});
|
||||
|
||||
$('tr.athing').each(function(i) {
|
||||
const commURL = `${host}/item?id=` + $(this).attr('id');
|
||||
const item = items[i];
|
||||
|
||||
if (showStory) {
|
||||
item.description = `Comments: <a href="${commURL}"> ${commURL} </a>`;
|
||||
} else {
|
||||
item.description = `Link: <a href="${item.link}"> ${item.title} </a>`;
|
||||
item.link = commURL;
|
||||
}
|
||||
});
|
||||
|
||||
let title = `Hacker News: ${section}`;
|
||||
if (!showStory) {
|
||||
title += '/comments';
|
||||
}
|
||||
|
||||
ctx.state.data = {
|
||||
title: title,
|
||||
link: url,
|
||||
item: items,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user