diff --git a/lib/router.js b/lib/router.js index 0824622cbb..c3c653a2dc 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3085,7 +3085,7 @@ router.get('/jike/topic/text/:id', lazyloadRouteHandler('./routes/jike/topicText router.get('/jike/user/:id', lazyloadRouteHandler('./routes/jike/user')); // 网易新闻 -router.get('/netease/news/rank/:category?/:type?/:time?', lazyloadRouteHandler('./routes/netease/news/rank')); +// router.get('/netease/news/rank/:category?/:type?/:time?', lazyloadRouteHandler('./routes/netease/news/rank')); router.get('/netease/news/special/:type?', lazyloadRouteHandler('./routes/netease/news/special')); // 网易 - 网易号 diff --git a/lib/v2/netease/maintainer.js b/lib/v2/netease/maintainer.js new file mode 100644 index 0000000000..ec51c4fc96 --- /dev/null +++ b/lib/v2/netease/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/news/rank/:category?/:type?/:time?': ['nczitzk'], +}; diff --git a/lib/v2/netease/radar.js b/lib/v2/netease/radar.js new file mode 100644 index 0000000000..fa231d9dd7 --- /dev/null +++ b/lib/v2/netease/radar.js @@ -0,0 +1,13 @@ +module.exports = { + '163.com': { + _name: '网易新闻', + news: [ + { + title: '排行榜', + docs: 'https://docs.rsshub.app/.html#', + source: ['/:category', '/'], + target: '//:category?', + }, + ], + }, +}; diff --git a/lib/routes/netease/news/rank.js b/lib/v2/netease/rank.js similarity index 71% rename from lib/routes/netease/news/rank.js rename to lib/v2/netease/rank.js index 8f82f0815c..2c51fcc3d6 100644 --- a/lib/routes/netease/news/rank.js +++ b/lib/v2/netease/rank.js @@ -1,9 +1,9 @@ -const url = require('url'); const got = require('@/utils/got'); const cheerio = require('cheerio'); const iconv = require('iconv-lite'); +const { parseDate } = require('@/utils/parse-date'); -const rootUrl = 'http://news.163.com/'; +const rootUrl = 'http://news.163.com'; const config = { whole: { @@ -56,7 +56,7 @@ const config = { }, }; -const time = { +const timeRange = { hour: { index: 0, title: '1小时', @@ -76,34 +76,32 @@ const time = { }; module.exports = async (ctx) => { - ctx.params.category = ctx.params.category ? ctx.params.category : 'whole'; - ctx.params.type = ctx.params.type ? ctx.params.type : 'click'; - ctx.params.time = ctx.params.time ? ctx.params.time : 'day'; + const category = ctx.params.category || 'whole'; + const type = ctx.params.type || 'click'; + const time = ctx.params.time || 'day'; - const cfg = config[ctx.params.category]; + const cfg = config[category]; if (!cfg) { throw Error('Bad category. See docs'); - } else if ( - (ctx.params.category !== 'whole' && ctx.params.type === 'click' && ctx.params.time === 'month') || - (ctx.params.category === 'whole' && ctx.params.type === 'click' && ctx.params.time === 'hour') || - (ctx.params.type === 'follow' && ctx.params.time === 'hour') - ) { - throw Error('Bad time range. See docs'); + } else if ((category !== 'whole' && type === 'click' && time === 'month') || (category === 'whole' && type === 'click' && time === 'hour') || (type === 'follow' && time === 'hour')) { + throw Error('Bad timeRange range. See docs'); } - const currentUrl = ctx.params.category === 'money' ? cfg.link : url.resolve(rootUrl, cfg.link); + const currentUrl = category === 'money' ? cfg.link : `${rootUrl}${cfg.link}`; const response = await got({ method: 'get', url: currentUrl, responseType: 'buffer', }); + const $ = cheerio.load(iconv.decode(response.data, 'gbk')); + const list = $('div.tabContents') - .eq(time[ctx.params.time].index + (ctx.params.category === 'whole' ? (ctx.params.type === 'click' ? -1 : 2) : ctx.params.type === 'click' ? 0 : 2)) + .eq(timeRange[time].index + (category === 'whole' ? (type === 'click' ? -1 : 2) : type === 'click' ? 0 : 2)) .find('table tbody tr td a') - .slice(0, 15) .map((_, item) => { item = $(item); + return { link: item.attr('href'), }; @@ -116,6 +114,7 @@ module.exports = async (ctx) => { try { const category = item.link.split('.163.com')[0].split('//').pop().split('.').pop(); const link = `https://3g.163.com/${category}/article/${item.link.split('/').pop()}`; + const detailResponse = await got({ method: 'get', url: link, @@ -128,19 +127,27 @@ module.exports = async (ctx) => { }); item.title = content('meta[property="og:title"]').attr('content').replace('_手机网易网', ''); - item.pubDate = content('meta[property="og:release_date"]').attr('content'); + item.pubDate = parseDate(content('meta[property="og:release_date"]').attr('content')); item.description = content('.content').html(); - - return item; } catch (err) { return Promise.resolve(''); } + + return item; }) ) ); + for (let i = 0; i < items.length; i++) { + if (items[i] === undefined) { + items.splice(i, 1); + } else if (items[i].title === undefined) { + items.splice(i, 1); + } + } + ctx.state.data = { - title: `网易新闻${time[ctx.params.time].title}${ctx.params.type === 'click' ? '点击' : '跟帖'}榜 - ${cfg.title}`, + title: `网易新闻${timeRange[time].title}${type === 'click' ? '点击' : '跟帖'}榜 - ${cfg.title}`, link: currentUrl, item: items, }; diff --git a/lib/v2/netease/router.js b/lib/v2/netease/router.js new file mode 100644 index 0000000000..fcba58222c --- /dev/null +++ b/lib/v2/netease/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/news/rank/:category?/:type?/:time?', require('./rank')); +};