diff --git a/docs/shopping.md b/docs/shopping.md index 55cc7bc7f..4b0c03d71 100644 --- a/docs/shopping.md +++ b/docs/shopping.md @@ -102,6 +102,16 @@ pageClass: routes +### 好文分类 + + + +| 最新 | 周排行 | 月排行 | +| ---- | ------ | ------ | +| 0 | 7 | 30 | + + + ## 甩甩尾巴 ### 分类 diff --git a/lib/router.js b/lib/router.js index 01f170cc9..63bc35cf4 100644 --- a/lib/router.js +++ b/lib/router.js @@ -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')); diff --git a/lib/routes/smzdm/haowen_fenlei.js b/lib/routes/smzdm/haowen_fenlei.js new file mode 100644 index 000000000..eebfadade --- /dev/null +++ b/lib/routes/smzdm/haowen_fenlei.js @@ -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, + }; +};