From 9769c550de95bda318c19b3cd6675d3773a13539 Mon Sep 17 00:00:00 2001 From: tgly307 Date: Fri, 10 Aug 2018 01:21:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E6=BA=90=E4=B8=AD=E5=9B=BD=E8=B5=84?= =?UTF-8?q?=E8=AE=AF=E5=85=A8=E6=96=87RSS=20(#443)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 来源:https://www.oschina.net/news/ 每次取前10条的全文内容并缓存,有效期一天 --- README.md | 2 ++ docs/README.md | 10 ++++++++ router.js | 3 +++ routes/oschina/news.js | 57 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+) create mode 100644 routes/oschina/news.js diff --git a/README.md b/README.md index d0785fb00..587e55361 100644 --- a/README.md +++ b/README.md @@ -218,6 +218,8 @@ RSSHub 是一个轻量、易于扩展的 RSS 生成器,可以给任何奇奇 - 脚本更新 - LinkedKeeper - 博文 +- 开源中国 + - 资讯 diff --git a/docs/README.md b/docs/README.md index b124788ca..590ab2973 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1754,3 +1754,13 @@ domain,按脚本生效域名过滤,可选 type,博文分类,为 URL 中 `.action` 的文件名 id,可选,分区或标签的 ID,对应 URL 中的 `sid` 或 `tid` + +## 开源中国 + +### 资讯 + +举例: [https://rsshub.app/oschina/news](https://rsshub.app/oschina/news) + +路由: `/oschina/news` + +参数:无 diff --git a/router.js b/router.js index f00a3acf4..dafad880d 100755 --- a/router.js +++ b/router.js @@ -396,4 +396,7 @@ router.get('/greasyfork/:language/:domain?', require('./routes/greasyfork/script // LinkedKeeper router.get('/linkedkeeper/:type/:id?', require('./routes/linkedkeeper/index')); +// 开源中国 +router.get('/oschina/news', require('./routes/oschina/news')); + module.exports = router; diff --git a/routes/oschina/news.js b/routes/oschina/news.js new file mode 100644 index 000000000..aed3b64e0 --- /dev/null +++ b/routes/oschina/news.js @@ -0,0 +1,57 @@ +const axios = require('../../utils/axios'); +const cheerio = require('cheerio'); +const config = require('../../config'); + +module.exports = async (ctx) => { + const res = await axios({ + method: 'get', + url: 'https://www.oschina.net/news/', + headers: { + 'User-Agent': config.ua, + Referer: 'https://www.oschina.net/news/', + }, + }); + const data = res.data; + const $ = cheerio.load(data); + const list = $('#newsList').find('.news-item'); + const count = []; + for (let i = 0; i < Math.min(list.length, 10); i++) { + count.push(i); + } + const resultItem = await Promise.all( + count.map(async (i) => { + const each = $(list[i]); + const url = `https://www.oschina.net${each.find('a', 'h3').attr('href')}`; + const item = { + title: each.find('a', 'h3').attr('title'), + description: '', + link: url, + }; + const key = 'oschina' + url; + const value = await ctx.cache.get(key); + + if (value) { + item.description = value; + } else { + const detail = await axios({ + method: 'get', + url: url, + headers: { + 'User-Agent': config.ua, + Referer: url, + }, + }); + const content = cheerio.load(detail.data); + item.description = content('#articleContent').html(); + ctx.cache.set(key, item.description, 24 * 60 * 60); + } + return Promise.resolve(item); + }) + ); + + ctx.state.data = { + title: '开源中国-资讯', + link: 'https://www.oschina.net/news/', + item: resultItem, + }; +};