fix(route): 网易新闻排行榜 (#8364)

This commit is contained in:
Ethan Shen
2021-11-27 16:33:03 +08:00
committed by GitHub
parent 2872ca8aec
commit 7be811e355
5 changed files with 47 additions and 21 deletions

View File

@@ -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'));
// 网易 - 网易号

View File

@@ -0,0 +1,3 @@
module.exports = {
'/news/rank/:category?/:type?/:time?': ['nczitzk'],
};

13
lib/v2/netease/radar.js Normal file
View File

@@ -0,0 +1,13 @@
module.exports = {
'163.com': {
_name: '网易新闻',
news: [
{
title: '排行榜',
docs: 'https://docs.rsshub.app/.html#',
source: ['/:category', '/'],
target: '//:category?',
},
],
},
};

View File

@@ -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 <a href="https://docs.rsshub.app/new-media.html#wang-yi-xin-wen-pai-hang-bang">docs</a>');
} 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 <a href="https://docs.rsshub.app/new-media.html#wang-yi-xin-wen-pai-hang-bang">docs</a>');
} else if ((category !== 'whole' && type === 'click' && time === 'month') || (category === 'whole' && type === 'click' && time === 'hour') || (type === 'follow' && time === 'hour')) {
throw Error('Bad timeRange range. See <a href="https://docs.rsshub.app/new-media.html#wang-yi-xin-wen-pai-hang-bang">docs</a>');
}
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,
};

3
lib/v2/netease/router.js Normal file
View File

@@ -0,0 +1,3 @@
module.exports = function (router) {
router.get('/news/rank/:category?/:type?/:time?', require('./rank'));
};