diff --git a/docs/programming.md b/docs/programming.md index 521dace2f..a7ed42c2a 100644 --- a/docs/programming.md +++ b/docs/programming.md @@ -102,7 +102,22 @@ GitHub 官方也提供了一些 RSS: ## 开源中国 - + + +| [综合资讯][osc_gen] | [软件更新资讯][osc_proj] | [行业资讯][osc_ind] | [编程语言资讯][osc_pl] | +| ------------------- | ------------------------ | ------------------- | ---------------------- | +| industry | project | industry-news | programming | + +订阅[全部板块资讯][osc_all]可以使用 + +[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 '开源中国-编程语言资讯' + + + | 小小编辑 | diff --git a/lib/router.js b/lib/router.js index b3e8e2d89..1eeb75722 100755 --- a/lib/router.js +++ b/lib/router.js @@ -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')); diff --git a/lib/routes/oschina/news.js b/lib/routes/oschina/news.js index 13f293a0f..a44d1ddae 100644 --- a/lib/routes/oschina/news.js +++ b/lib/routes/oschina/news.js @@ -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, }; };