feat: add IT之家热榜 (#4180)

This commit is contained in:
Immmortal
2020-03-08 01:19:34 +08:00
committed by GitHub
parent 08ed767a1a
commit c7f3fdfc27
3 changed files with 82 additions and 1 deletions

View File

@@ -121,6 +121,16 @@ pageClass: routes
</Route> </Route>
### 热榜
<Route author="immmortal" example="/ithome/ranking/1" path="/ithome/ranking/:type" :paramsDesc="['类别']">
| 1 | 2 | 3 | 4 |
| ------------- | ---- | -------- | ---- |
| 24 小时阅读榜 | 周榜 | 7 天热评 | 月榜 |
</Route>
## IT 桔子 ## IT 桔子
### 投融资事件 ### 投融资事件

View File

@@ -2105,8 +2105,9 @@ router.get('/nyaa/search/:query?', require('./routes/nyaa/search'));
// 片源网 // 片源网
router.get('/pianyuan/:media?', require('./routes/pianyuan/app')); router.get('/pianyuan/:media?', require('./routes/pianyuan/app'));
// IT home // ITHome
router.get('/ithome/:caty', require('./routes/ithome/index')); 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')); router.get('/bahamut/creation/:author/:category?', require('./routes/bahamut/creation'));

View File

@@ -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,
};
};