From a4bed8741be213a3e01fd87c7a8169b3c96a51e9 Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Wed, 29 Jul 2020 21:29:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20=E6=A2=85=E8=8A=B1=E7=BD=91?= =?UTF-8?q?=E6=96=87=E7=AB=A0=E4=B8=8E=E4=BD=9C=E5=93=81=20(#5208)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/new-media.md | 22 ++++++++++++++ lib/router.js | 4 +++ lib/routes/meihua/article.js | 55 +++++++++++++++++++++++++++++++++ lib/routes/meihua/shots.js | 59 ++++++++++++++++++++++++++++++++++++ 4 files changed, 140 insertions(+) create mode 100644 lib/routes/meihua/article.js create mode 100644 lib/routes/meihua/shots.js diff --git a/docs/new-media.md b/docs/new-media.md index 71b6315dc..6c35bff61 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -753,6 +753,28 @@ area 分区选项 +## 梅花网 + +### 作品 + + + +| 最新 | 热门 | 推荐 | +| ------ | ---- | --------- | +| latest | hot | recommend | + + + +### 文章 + + + +| 最新 | 热门 | +| ------ | ---- | +| latest | hot | + + + ## 镁客网 im2maker ### 镁客网频道 diff --git a/lib/router.js b/lib/router.js index 5f897a645..4d7058436 100644 --- a/lib/router.js +++ b/lib/router.js @@ -2980,6 +2980,10 @@ router.get('/12379', require('./routes/12379/index')); router.get('/ngbj/today', require('./routes/niaogebiji/today')); router.get('/ngbj/cat/:cat', require('./routes/niaogebiji/cat')); +// 梅花网 +router.get('/meihua/shots/:caty', require('./routes/meihua/shots')); +router.get('/meihua/article/:caty', require('./routes/meihua/article')); + // 看点快报 router.get('/kuaibao', require('./routes/kuaibao/index')); diff --git a/lib/routes/meihua/article.js b/lib/routes/meihua/article.js new file mode 100644 index 000000000..f46317af0 --- /dev/null +++ b/lib/routes/meihua/article.js @@ -0,0 +1,55 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +const config = { + latest: { + link: 'article!2', + title: '最新', + }, + hot: { + link: 'article!3', + title: '热门', + }, +}; + +module.exports = async (ctx) => { + const cfg = config[ctx.params.caty]; + if (!cfg) { + throw Error('Bad category. See docs'); + } + + const currentUrl = `https://www.meihua.info/${cfg.link}`; + const response = await got({ + method: 'get', + url: currentUrl, + }); + const data = JSON.parse(response.data.match(/"compositionStore":(.*?),"detailStore":/)[1]); + + const shotList = data.compositionsData.list.map((item) => ({ + title: item.title, + link: `https://www.meihua.info/article/${item.compositionId}`, + pubDate: new Date(item.gmtPublish).toUTCString(), + })); + + const items = await Promise.all( + shotList.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const contentResponse = await got({ + method: 'get', + url: item.link, + }); + const content = cheerio.load(contentResponse.data); + + item.description = content('#article-content-html').html(); + return item; + }) + ) + ); + + ctx.state.data = { + title: `${cfg.title}文章 - 梅花网`, + link: currentUrl, + item: items, + }; +}; diff --git a/lib/routes/meihua/shots.js b/lib/routes/meihua/shots.js new file mode 100644 index 000000000..c2fd079f4 --- /dev/null +++ b/lib/routes/meihua/shots.js @@ -0,0 +1,59 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +const config = { + recommend: { + link: 'shots!1!0!0!0!0', + title: '推荐', + }, + latest: { + link: 'shots!2!0!0!0!0', + title: '最新', + }, + hot: { + link: 'shots!3!0!0!0!0', + title: '热门', + }, +}; + +module.exports = async (ctx) => { + const cfg = config[ctx.params.caty]; + if (!cfg) { + throw Error('Bad category. See docs'); + } + + const currentUrl = `https://www.meihua.info/${cfg.link}`; + const response = await got({ + method: 'get', + url: currentUrl, + }); + const data = JSON.parse(response.data.match(/"shotsStore":(.*?),"commentStore":/)[1]); + + const shotList = data.shotsData.list.map((item) => ({ + title: item.title, + link: `https://www.meihua.info/shots/${item.compositionId}`, + pubDate: new Date(item.gmtPublish).toUTCString(), + })); + + const items = await Promise.all( + shotList.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const contentResponse = await got({ + method: 'get', + url: item.link, + }); + const content = cheerio.load(contentResponse.data); + + item.description = content('div.summary').html(); + return item; + }) + ) + ); + + ctx.state.data = { + title: `${cfg.title}作品 - 梅花网`, + link: currentUrl, + item: items, + }; +};