From bf67a675e467ca88356707d08ca7f2b6d2c6f65d Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Sun, 24 May 2020 12:57:56 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20=E9=A9=AC=E8=9C=82=E7=AA=9D?= =?UTF-8?q?=E8=87=AA=E7=94=B1=E8=A1=8C=E6=94=BB=E7=95=A5=20(#4848)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/travel.md | 8 ++++++ lib/router.js | 1 + lib/routes/mafengwo/ziyouxing.js | 49 ++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 lib/routes/mafengwo/ziyouxing.js diff --git a/docs/travel.md b/docs/travel.md index 795381e83a..1fe24de187 100644 --- a/docs/travel.md +++ b/docs/travel.md @@ -102,6 +102,14 @@ IATA 国际航空运输协会机场代码,参见[维基百科 国际航空运 +### 自由行 + + + +目的地代码请参见 [这里](http://www.mafengwo.cn/mdd/) + + + ## 中国美术馆 ### 美术馆新闻 diff --git a/lib/router.js b/lib/router.js index a518b0bcf1..6cf467481a 100644 --- a/lib/router.js +++ b/lib/router.js @@ -517,6 +517,7 @@ router.get('/hopper/:lowestOnly/:from/:to?', require('./routes/hopper/index')); // 马蜂窝 router.get('/mafengwo/note/:type', require('./routes/mafengwo/note')); +router.get('/mafengwo/ziyouxing/:code', require('./routes/mafengwo/ziyouxing')); // 中国地震局震情速递(与地震台网同步更新) router.get('/earthquake/:region?', require('./routes/earthquake')); diff --git a/lib/routes/mafengwo/ziyouxing.js b/lib/routes/mafengwo/ziyouxing.js new file mode 100644 index 0000000000..13a44cf0d9 --- /dev/null +++ b/lib/routes/mafengwo/ziyouxing.js @@ -0,0 +1,49 @@ +const url = require('url'); +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const rootUrl = `http://www.mafengwo.cn/gonglve/ziyouxing/list/list_page?mddid=${ctx.params.code}&page=1`; + + const response = await got({ + method: 'get', + url: rootUrl, + }); + const $ = cheerio.load(response.data.html); + const list = $('div.item') + .slice(0, 10) + .map((_, item) => { + item = $(item); + const a = item.find('a'); + return { + title: item.find('h3').text(), + link: url.resolve(`http://www.mafengwo.cn`, a.attr('href')), + }; + }) + .get(); + + const items = await Promise.all( + list.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const res = await got({ method: 'get', url: item.link }); + const content = cheerio.load(res.data); + item.pubDate = new Date(content('span.time').eq(1).find('em').text()).toUTCString(); + item.description = content('div.sideL').html(); + return item; + }) + ) + ); + + const titleResponse = await got({ + method: 'get', + url: `http://www.mafengwo.cn/gonglve/ziyouxing/mdd_${ctx.params.code}/`, + }); + const title = cheerio.load(titleResponse.data); + + ctx.state.data = { + title: title('title').text(), + link: rootUrl, + item: items, + }; +};