开源中国资讯全文RSS (#443)

来源:https://www.oschina.net/news/
每次取前10条的全文内容并缓存,有效期一天
This commit is contained in:
tgly307 2018-08-10 01:21:21 +08:00 committed by DIYgod
parent a19b872f73
commit 9769c550de
4 changed files with 72 additions and 0 deletions

View File

@ -218,6 +218,8 @@ RSSHub 是一个轻量、易于扩展的 RSS 生成器,可以给任何奇奇
- 脚本更新
- LinkedKeeper
- 博文
- 开源中国
- 资讯
</details>

View File

@ -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`
参数:无

View File

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

57
routes/oschina/news.js Normal file
View File

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