From 3d72eec3c255c52744ce75494a423ff25bc8d44a Mon Sep 17 00:00:00 2001 From: Henry Wang Date: Wed, 28 Aug 2019 04:14:46 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20add=20=E6=90=9C=E7=8B=90=E5=8F=B7=20(#2?= =?UTF-8?q?946)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: add 搜狐号 Close #2926 * docs --- docs/social-media.md | 11 ++++++++++ lib/router.js | 3 +++ lib/routes/sohu/mp.js | 48 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 lib/routes/sohu/mp.js diff --git a/docs/social-media.md b/docs/social-media.md index d03e719e0..7a75a5d95 100644 --- a/docs/social-media.md +++ b/docs/social-media.md @@ -606,6 +606,17 @@ pageClass: routes +## 搜狐 + +### 搜狐号 + + + +1. 通过浏览器搜索相关搜狐号 `果壳 site: mp.sohu.com`。 +1. 通过浏览器控制台执行 `cfgs.author_id`,返回的即为搜狐号 ID。 + + + ## 贴吧 ### 帖子列表 diff --git a/lib/router.js b/lib/router.js index c25842eb5..6893b3b68 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1669,4 +1669,7 @@ router.get('/nciae/xsxx', require('./routes/universities/nciae/xsxx')); // cfan router.get('/cfan/news', require('./routes/cfan/news')); +// 搜狐 - 搜狐号 +router.get('/sohu/mp/:id', require('./routes/sohu/mp')); + module.exports = router; diff --git a/lib/routes/sohu/mp.js b/lib/routes/sohu/mp.js new file mode 100644 index 000000000..c72951d87 --- /dev/null +++ b/lib/routes/sohu/mp.js @@ -0,0 +1,48 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const date = require('@/utils/date'); + +module.exports = async (ctx) => { + const { id } = ctx.params; + + const response = await got.get(`https://v2.sohu.com/author-page-api/author-articles/pc/${id}`); + + const list = response.data.data.pcArticleVOS.splice(0, 10); + let author, link; + + const items = await Promise.all( + list.map(async (e) => { + const response = await ctx.cache.tryGet(e.link, async () => (await got.get(e.link)).data); + const $ = cheerio.load(response); + + if (!author) { + const meta = $('#article-container h4 > a'); + author = meta.text(); + link = meta.attr('href').split('==')[0]; + } + + const article = $('#mp-editor'); + + article.find('#backsohucom, p[data-role="editor-name"]').each((i, e) => { + $(e).remove(); + }); + + const single = { + title: e.title, + link: e.link, + description: article.html(), + pubDate: date(e.publicTimeStr, 8), + author, + }; + + return Promise.resolve(single); + }) + ); + + ctx.state.data = { + title: `搜狐号 - ${author}`, + link, + description: '', + item: items, + }; +};