From 9d5235fcc7c57edcf903e0a7fe9769c46ef0def9 Mon Sep 17 00:00:00 2001 From: Chenyang Shi Date: Wed, 22 May 2019 11:50:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20=E4=BB=80=E4=B9=88=E5=80=BC?= =?UTF-8?q?=E5=BE=97=E4=B9=B0=E5=A5=BD=E6=96=87=E5=88=86=E7=B1=BB=20(#2202?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/shopping.md | 10 +++++ lib/router.js | 1 + lib/routes/smzdm/haowen_fenlei.js | 72 +++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 lib/routes/smzdm/haowen_fenlei.js 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, + }; +};