更新 OSChina (#1978)

* 支持 OSChina 按板块订阅
* 修复了订阅中出现 undefined 条目的问题
This commit is contained in:
zengxs 2019-04-24 16:17:38 +08:00 committed by DIYgod
parent def00b1f8d
commit a1ea616ee9
3 changed files with 61 additions and 9 deletions

View File

@ -102,7 +102,22 @@ GitHub 官方也提供了一些 RSS:
## 开源中国
<Route name="资讯" author="tgly307" example="/oschina/news" path="/oschina/news"/>
<Route name="资讯" author="tgly307 zengxs" example="/oschina/news/project" path="/oschina/news/:category?" :paramsDesc="['板块名']">
| [综合资讯][osc_gen] | [软件更新资讯][osc_proj] | [行业资讯][osc_ind] | [编程语言资讯][osc_pl] |
| ------------------- | ------------------------ | ------------------- | ---------------------- |
| industry | project | industry-news | programming |
订阅[全部板块资讯][osc_all]可以使用 <https://rsshub.app/oschina/news>
[osc_all]: https://www.oschina.net/news '开源中国-全部资讯'
[osc_gen]: https://www.oschina.net/news/industry '开源中国-综合资讯'
[osc_proj]: https://www.oschina.net/news/project '开源中国-软件更新资讯'
[osc_ind]: https://www.oschina.net/news/industry-news '开源中国-行业资讯'
[osc_pl]: https://www.oschina.net/news/programming '开源中国-编程语言资讯'
</Route>
<Route name="用户博客" author="dxmpalb" example="/oschina/user/xxiaobian" path="/oschina/user/:id" :paramsDesc="['用户 id, 可通过查看用户博客网址得到,如果博客以 u/数字结尾,使用下一条路由']">
| 小小编辑 |

View File

@ -489,7 +489,7 @@ router.get('/greasyfork/:language/:domain?', require('./routes/greasyfork/script
router.get('/linkedkeeper/:type/:id?', require('./routes/linkedkeeper/index'));
// 开源中国
router.get('/oschina/news', require('./routes/oschina/news'));
router.get('/oschina/news/:category?', require('./routes/oschina/news'));
router.get('/oschina/user/:id', require('./routes/oschina/user'));
router.get('/oschina/u/:id', require('./routes/oschina/u'));

View File

@ -1,16 +1,52 @@
const axios = require('../../utils/axios');
const cheerio = require('cheerio');
const USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0';
const configs = {
all: {
name: '最新资讯',
link: 'https://www.oschina.net/news/project',
ajaxUrl: 'https://www.oschina.net/news/widgets/_news_index_all_list?p=1&type=ajax',
},
industry: {
name: '综合资讯',
link: 'https://www.oschina.net/news/industry',
ajaxUrl: 'https://www.oschina.net/news/widgets/_news_index_generic_list?p=1&type=ajax',
},
project: {
name: '软件更新资讯',
link: 'https://www.oschina.net/news/project',
ajaxUrl: 'https://www.oschina.net/news/widgets/_news_index_project_list?p=1&type=ajax',
},
'industry-news': {
name: '行业资讯',
link: 'https://www.oschina.net/news/industry-news',
ajaxUrl: 'https://www.oschina.net/news/widgets/_news_index_industry_list?p=1&type=ajax',
},
programming: {
name: '编程语言资讯',
link: 'https://www.oschina.net/news/programming',
ajaxUrl: 'https://www.oschina.net/news/widgets/_news_index_programming_language_list?p=1&type=ajax',
},
};
module.exports = async (ctx) => {
const category = ctx.params.category || 'all';
const config = configs[category];
const res = await axios({
method: 'get',
url: 'https://www.oschina.net/news/',
url: config.ajaxUrl,
headers: {
Referer: 'https://www.oschina.net/news/',
'User-Agent': USER_AGENT,
Referer: config.link,
'X-Requested-With': 'XMLHttpRequest',
},
});
const $ = cheerio.load(res.data);
const list = $('#newsList').find('.news-item');
$('.ad-wrap').remove();
const list = $('.items').find('.news-item');
const count = [];
for (let i = 0; i < Math.min(list.length, 10); i++) {
count.push(i);
@ -24,7 +60,7 @@ module.exports = async (ctx) => {
description: each.find('p', '.description').text(),
link: encodeURI(originalUrl),
};
if (/^https:\/\/www.oschina.net\/news\/.*$/.test(originalUrl)) {
if (/^https?:\/\/www.oschina.net\/news\/.*$/.test(originalUrl)) {
const key = 'oschina' + item.link;
const value = await ctx.cache.get(key);
@ -35,7 +71,8 @@ module.exports = async (ctx) => {
method: 'get',
url: item.link,
headers: {
Referer: item.link,
'User-Agent': USER_AGENT,
Referer: config.link,
},
});
const content = cheerio.load(detail.data);
@ -52,8 +89,8 @@ module.exports = async (ctx) => {
);
ctx.state.data = {
title: '开源中国-资讯',
link: 'https://www.oschina.net/news/',
title: `开源中国-${config.name}`,
link: config.link,
item: resultItem,
};
};