feat: add 马蜂窝自由行攻略 (#4848)

This commit is contained in:
Ethan Shen
2020-05-24 12:57:56 +08:00
committed by GitHub
parent 148e017f31
commit bf67a675e4
3 changed files with 58 additions and 0 deletions
+8
View File
@@ -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>
## 中国美术馆
### 美术馆新闻
+1
View File
@@ -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'));
+49
View File
@@ -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,
};
};