From bf5d21bc474c4c2c193c86241e396667fd10841d Mon Sep 17 00:00:00 2001 From: Chenyang Shi Date: Sun, 21 Apr 2019 17:46:08 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E5=88=9B=E4=B8=9A=E9=82=A6label=20(#1958?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit closes #1953 --- docs/other.md | 2 ++ lib/router.js | 1 + lib/routes/cyzone/label.js | 53 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 lib/routes/cyzone/label.js diff --git a/docs/other.md b/docs/other.md index dabe7d75e..2848b410b 100644 --- a/docs/other.md +++ b/docs/other.md @@ -329,6 +329,8 @@ board 和 build 可在[这里](http://api.ineal.me/tss/status)查看 + + ## 惠誉评级 diff --git a/lib/router.js b/lib/router.js index 97a1643e3..53244057d 100755 --- a/lib/router.js +++ b/lib/router.js @@ -1047,6 +1047,7 @@ router.get('/duozhuayu/search/:wd', require('./routes/duozhuayu/search')); // 创业邦 router.get('/cyzone/author/:id', require('./routes/cyzone/author')); +router.get('/cyzone/label/:name', require('./routes/cyzone/label')); // 政府 router.get('/gov/zhengce/zuixin', require('./routes/gov/zhengce/zuixin')); diff --git a/lib/routes/cyzone/label.js b/lib/routes/cyzone/label.js new file mode 100644 index 000000000..2349a7da7 --- /dev/null +++ b/lib/routes/cyzone/label.js @@ -0,0 +1,53 @@ +const cheerio = require('cheerio'); +const axios = require('../../utils/axios'); + +module.exports = async (ctx) => { + const name = ctx.params.name; + const url = `https://www.cyzone.cn/label/${encodeURIComponent(name)}/`; + const res = await axios.get(url); + const $ = cheerio.load(res.data); + + const list = $('.article-item'); + const out = await Promise.all( + list + .map(async (index, elem) => { + const $elem = $(elem); + const $title = $elem.find('.item-title'); + const link = 'https:' + $title.attr('href'); + const pubDate = new Date(+$elem.find('.time').attr('data-time') * 1000).toUTCString(); + const item = { + title: $title.text(), + pubDate, + link, + }; + const key = `cyzone-${link}`; + const value = await ctx.cache.get(key); + if (value) { + item.description = value; + } else { + const storyDetail = await axios.get(item.link); + const data = storyDetail.data; + const $ = cheerio.load(data); + $('.article-content img').each(function() { + const $img = $(this); + const src = $img.attr('src'); + + if (!src.startsWith('http')) { + $img.attr('src', 'https:' + src); + } + }); + item.description = $('.article-content').html() || '内容已删除或未通过审核'; + ctx.cache.set(key, item.description, 12 * 60 * 60); + } + + return Promise.resolve(item); + }) + .get() + ); + + ctx.state.data = { + title: `创业邦-#${name}#`, + link: url, + item: out, + }; +};