diff --git a/docs/new-media.md b/docs/new-media.md index 3b10f5d3d1..0890e13bd1 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -121,6 +121,16 @@ pageClass: routes +### 热榜 + + + +| 1 | 2 | 3 | 4 | +| ------------- | ---- | -------- | ---- | +| 24 小时阅读榜 | 周榜 | 7 天热评 | 月榜 | + + + ## IT 桔子 ### 投融资事件 diff --git a/lib/router.js b/lib/router.js index 5b490bc630..c21c916a70 100644 --- a/lib/router.js +++ b/lib/router.js @@ -2105,8 +2105,9 @@ router.get('/nyaa/search/:query?', require('./routes/nyaa/search')); // 片源网 router.get('/pianyuan/:media?', require('./routes/pianyuan/app')); -// IT home +// ITHome router.get('/ithome/:caty', require('./routes/ithome/index')); +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 new file mode 100644 index 0000000000..35ef2b784f --- /dev/null +++ b/lib/routes/ithome/ranking.js @@ -0,0 +1,70 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const option = ctx.params.type || '1'; + const link = `https://www.ithome.com/`; + + const response = await got.get(link); + const $ = cheerio.load(response.data); + + const config = { + 1: '24小时阅读榜', + 2: '周榜', + 3: '7天热评', + 4: '月榜', + }; + + const title = config[option]; + + let elem = $('.lst.lst-2.hot-list') + .find('.bx') + .first(); + for (let i = 0; i < Number(option) - 1; ++i) { + elem = elem.next(); + } + const list = elem + .find('li') + .map(function() { + const info = { + title: $(this) + .find('a') + .text(), + link: $(this) + .find('a') + .attr('href'), + }; + return info; + }) + .get(); + + const items = await Promise.all( + list.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const res = await got({ + method: 'get', + url: item.link, + }); + const content = cheerio.load(res.data); + const post = content('div#con div.content'); + const paragraph = post.find('div#paragraph'); + paragraph.find('img[data-original]').each((_, ele) => { + ele = $(ele); + ele.attr('src', ele.attr('data-original')); + ele.removeAttr('class'); + ele.removeAttr('data-original'); + }); + item.description = paragraph.html(); + item.pubDate = new Date(post.find('span#pubtime_baidu').text() + ' GMT+8').toUTCString(); + return item; + }) + ) + ); + + ctx.state.data = { + title: `IT之家-${title}`, + link: link, + item: items, + }; +};