feat: add 什么值得买好文分类 (#2202)

This commit is contained in:
Chenyang Shi
2019-05-22 11:50:51 +08:00
committed by DIYgod
parent 33d5e8df0a
commit 9d5235fcc7
3 changed files with 83 additions and 0 deletions

View File

@@ -102,6 +102,16 @@ pageClass: routes
<Route author="LogicJake" example="/smzdm/haowen/1" path="/smzdm/haowen/:day" :paramsDesc="['以天为时间跨度默认为all其余可以选择1730365']"/>
### 好文分类
<Route author="LogicJake" example="/smzdm/haowen/fenlei/shenghuodianqi" path="/smzdm/haowen/fenlei/:name/:sort?" :paramsDesc="['分类名,可在 URL 中查看','排序方式,默认为最新']">
| 最新 | 周排行 | 月排行 |
| ---- | ------ | ------ |
| 0 | 7 | 30 |
</Route>
## 甩甩尾巴
### 分类

View File

@@ -302,6 +302,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('/smzdm/haowen/fenlei/:name/:sort?', require('./routes/smzdm/haowen_fenlei'));
// 新京报
router.get('/bjnews/:cat', require('./routes/bjnews/news'));

View File

@@ -0,0 +1,72 @@
const axios = require('@/utils/axios');
const cheerio = require('cheerio');
const date = require('@/utils/date');
module.exports = async (ctx) => {
const name = ctx.params.name;
const sort = ctx.params.sort || '0';
let link;
if (sort === '0') {
link = `https://post.smzdm.com/fenlei/${name}/`;
} else {
link = `https://post.smzdm.com/fenlei/${name}/hot_${sort}/`;
}
const response = await axios.get(link);
const $ = cheerio.load(response.data);
const title = $('div.crumbs.nav-crumbs')
.text()
.split('>')
.pop();
const list = $('div.list.post-list')
.slice(0, 10)
.map(function() {
const info = {
title: $(this)
.find('h2.item-name a')
.text(),
link: $(this)
.find('h2.item-name a')
.attr('href'),
pubdate: $(this)
.find('span.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').html();
const single = {
title: title,
link: itemUrl,
description: description,
pubDate: date(pubdate),
};
ctx.cache.set(itemUrl, JSON.stringify(single));
return Promise.resolve(single);
})
);
ctx.state.data = {
title: `${title}-什么值得买好文分类`,
link: link,
item: out,
};
};