add 什么值得买好文 (#1792)

* add 什么值得买好文

* update

* fix
This commit is contained in:
Chenyang Shi
2019-03-22 17:46:38 +08:00
committed by DIYgod
parent 02c7b212cd
commit fca5bc50fc
3 changed files with 65 additions and 0 deletions

View File

@@ -2422,6 +2422,8 @@ IATA 国际航空运输协会机场代码, 参见[维基百科 国际航空运
</route>
<route name="好文" author="LogicJake" example="/smzdm/haowen/1" path="/smzdm/haowen/:day" :paramsDesc="['以天为时间跨度默认为all其余可以选择1730365']"/>
### 小米
<route name="小米众筹" author="DIYgod" example="/mi/crowdfunding" path="/mi/crowdfunding"/>

View File

@@ -374,6 +374,7 @@ router.get('/eztv/torrents/:imdb_id', require('./routes/eztv/imdb'));
// 什么值得买
router.get('/smzdm/keyword/:keyword', require('./routes/smzdm/keyword'));
router.get('/smzdm/ranking/:rank_type/:rank_id/:hour', require('./routes/smzdm/ranking'));
router.get('/smzdm/haowen/:day?', require('./routes/smzdm/haowen'));
// 新京报
router.get('/bjnews/:cat', require('./routes/bjnews/news'));

View File

@@ -0,0 +1,62 @@
const axios = require('../../utils/axios');
const cheerio = require('cheerio');
const date = require('../../utils/date');
module.exports = async (ctx) => {
const day = ctx.params.day || 'all';
const link = `https://post.smzdm.com/hot_${day}`;
const response = await axios.get(link);
const $ = cheerio.load(response.data);
const title = $('li.filter-tab.active').text();
const list = $('li.feed-row-wide')
.slice(0, 10)
.map(function() {
const info = {
title: $(this)
.find('h5.z-feed-title a')
.text(),
link: $(this)
.find('h5.z-feed-title a')
.attr('href'),
pubdate: $(this)
.find('span.z-publish-time')
.text(),
};
return info;
})
.get();
const out = await Promise.all(
list.map(async (info) => {
const title = info.title;
const pubdate = info.pubdate;
const itemUrl = info.link;
const cache = await ctx.cache.get(itemUrl);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const response = await axios.get(itemUrl);
const $ = cheerio.load(response.data);
const description = $('article > div').html();
const single = {
title: title,
link: itemUrl,
description: description,
pubDate: date(pubdate),
};
ctx.cache.set(itemUrl, JSON.stringify(single), 24 * 60 * 60);
return Promise.resolve(single);
})
);
ctx.state.data = {
title: `${title}-什么值得买好文`,
link: link,
item: out,
};
};