Skip to content

Commit

Permalink
feat: add 马蜂窝自由行攻略 (DIYgod#4848)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan Shen authored May 24, 2020
1 parent 148e017 commit bf67a67
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/travel.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ IATA 国际航空运输协会机场代码,参见[维基百科 国际航空运

<Route author="sinchang" example="/mafengwo/note/hot" path="/mafengwo/note/:type" :paramsDesc="['目前支持两种, `hot` 代表热门游记, `latest` 代表最新游记']"/>

### 自由行

<Route author="nczitzk" example="/mafengwo/ziyouxing/10186" path="/mafengwo/ziyouxing/:code" :paramsDesc="['目的地代码,可在该目的地页面的 URL 中找到']">

目的地代码请参见 [这里](http://www.mafengwo.cn/mdd/)

</Route>

## 中国美术馆

### 美术馆新闻
Expand Down
1 change: 1 addition & 0 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
49 changes: 49 additions & 0 deletions lib/routes/mafengwo/ziyouxing.js
Original file line number Diff line number Diff line change
@@ -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,
};
};

0 comments on commit bf67a67

Please sign in to comment.