diff --git a/assets/radar-rules.js b/assets/radar-rules.js index d46951fb5c..b7e92ec488 100644 --- a/assets/radar-rules.js +++ b/assets/radar-rules.js @@ -908,6 +908,26 @@ 'ithome.com': { _name: 'IT 之家', + '.': [ + { + title: '24 小时阅读榜', + docs: 'https://docs.rsshub.app/new-media.html#it-zhi-jia', + source: ['', '/*'], + target: '/ithome/ranking/24h', + }, + { + title: '7 天最热', + docs: 'https://docs.rsshub.app/new-media.html#it-zhi-jia', + source: ['', '/*'], + target: '/ithome/ranking/7days', + }, + { + title: '月榜', + docs: 'https://docs.rsshub.app/new-media.html#it-zhi-jia', + source: ['', '/*'], + target: '/ithome/ranking/monthly', + }, + ], it: [ { title: 'IT 资讯', diff --git a/docs/new-media.md b/docs/new-media.md index 606132e956..aa468da218 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -162,11 +162,11 @@ pageClass: routes ### 热榜 - + -| 1 | 2 | 3 | 4 | -| ------------- | ---- | -------- | ---- | -| 24 小时阅读榜 | 周榜 | 7 天热评 | 月榜 | +| 24h | 7days | monthly | +| ------------- | -------- | ------- | +| 24 小时阅读榜 | 7 天最热 | 月榜 | diff --git a/lib/router.js b/lib/router.js index 5cf205e653..4067099414 100644 --- a/lib/router.js +++ b/lib/router.js @@ -2245,7 +2245,7 @@ router.get('/pianyuan/:media?', require('./routes/pianyuan/app')); // ITHome router.get('/ithome/:caty', require('./routes/ithome/index')); -router.get('/ithome/ranking/:type?', require('./routes/ithome/ranking')); +router.get('/ithome/ranking/:type', require('./routes/ithome/ranking')); // 巴哈姆特 router.get('/bahamut/creation/:author/:category?', require('./routes/bahamut/creation')); diff --git a/lib/routes/ithome/ranking.js b/lib/routes/ithome/ranking.js index 46e84162c3..4f11fd07e3 100644 --- a/lib/routes/ithome/ranking.js +++ b/lib/routes/ithome/ranking.js @@ -1,28 +1,38 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); +const iconv = require('iconv-lite'); module.exports = async (ctx) => { - const option = ctx.params.type || '1'; - const link = `https://www.ithome.com/`; + const option = ctx.params.type; - const response = await got.get(link); - const $ = cheerio.load(response.data); + const response = await got({ + method: 'get', + url: 'https://www.ithome.com/block/rank.html', + encoding: 'binary', + }); + const content = iconv.decode(Buffer.from(response.data, 'binary'), 'gb2312'); + const $ = cheerio.load(content); const config = { - 1: '24小时阅读榜', - 2: '周榜', - 3: '7天热评', - 4: '月榜', + '24h': '24 小时最热', + '7days': '7 天最热', + monthly: '月榜', + }; + + const type2id = { + '24h': 'd-1', + '7days': 'd-2', + monthly: 'd-3', }; const title = config[option]; + const id = type2id[option]; - let elem = $('.lst.lst-2.hot-list').find('.bx').first(); - for (let i = 0; i < Number(option) - 1; ++i) { - elem = elem.next(); + if (!id) { + throw Error('Bad type. See https://docs.rsshub.app/new-media.html#it-zhi-jia'); } - const list = elem - .find('li') + + const list = $(`#${id} > li`) .map(function () { const info = { title: $(this).find('a').text(), @@ -41,8 +51,7 @@ module.exports = async (ctx) => { url: item.link, }); const content = cheerio.load(res.data); - const post = content('div#con div.content'); - const paragraph = post.find('div#paragraph'); + const paragraph = content('#paragraph'); paragraph.find('img[data-original]').each((_, ele) => { ele = $(ele); ele.attr('src', ele.attr('data-original')); @@ -50,7 +59,7 @@ module.exports = async (ctx) => { ele.removeAttr('data-original'); }); item.description = paragraph.html(); - item.pubDate = new Date(post.find('span#pubtime_baidu').text() + ' GMT+8').toUTCString(); + item.pubDate = new Date(content('#pubtime_baidu').text() + ' GMT+8').toUTCString(); return item; }) ) @@ -58,7 +67,7 @@ module.exports = async (ctx) => { ctx.state.data = { title: `IT之家-${title}`, - link: link, + link: 'https://www.ithome.com', item: items, }; };