diff --git a/docs/programming.md b/docs/programming.md index f045f6c2f..f7c2244b1 100644 --- a/docs/programming.md +++ b/docs/programming.md @@ -117,6 +117,33 @@ GitHub 官方也提供了一些 RSS: +## Hacker News + +### 分类 + + + +网站地址: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 上的讨论页面 | + + + ## LeetCode ### 文章 diff --git a/lib/router.js b/lib/router.js index bd1a882da..588662fa5 100644 --- a/lib/router.js +++ b/lib/router.js @@ -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')); diff --git a/lib/routes/hackernews/story.js b/lib/routes/hackernews/story.js new file mode 100644 index 000000000..dd514548c --- /dev/null +++ b/lib/routes/hackernews/story.js @@ -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: ${commURL} `; + } else { + item.description = `Link: ${item.title} `; + item.link = commURL; + } + }); + + let title = `Hacker News: ${section}`; + if (!showStory) { + title += '/comments'; + } + + ctx.state.data = { + title: title, + link: url, + item: items, + }; +};