add mcdonalds news

This commit is contained in:
jinkin
2021-01-05 11:22:31 +08:00
parent d9514090a5
commit 94214a9d5e
3 changed files with 65 additions and 0 deletions
+11
View File
@@ -347,3 +347,14 @@ For instance, in <https://www.leboncoin.fr/recherche/?**category=10&locations=Pa
### 商品上新
<Route author="LogicJake" example="/youzan/goods/13328377" path="/youzan/goods/:id" :paramsDesc="['商铺id']"/>
## 麦当劳
### 麦当劳活动资讯
<Route author="huyyi" example="/mcdonalds/sales+event" path="/mcdonalds/:category" :paramsDesc="['分类名(可用+连接多个分类)']">
| 全部分类 | 社会责任 | 人员品牌 | 产品故事 | 优惠 | 品牌文化 | 活动速报 |
| -------- | ------- | --- | ------- | ------ | ------- | -------- |
| news_list | responsibility | brand | product | sales | culture | event |
</Route>
+3
View File
@@ -3780,4 +3780,7 @@ router.get('/nace/blog/:sort?', require('./routes/nace/blog'));
// Caixin Latest
router.get('/caixin/latest', require('./routes/caixin/latest'));
// Mcdonalds
router.get('/mcdonalds/:category', require('./routes/mcdonalds/news'));
module.exports = router;
+51
View File
@@ -0,0 +1,51 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const category_param = ctx.params.category || 'news_list';
const categories = category_param.split('+');
const baseUrl = "https://www.mcdonalds.com.cn/news/";
const get_news_list = async (cates) => await Promise.all(
cates.map(async (cate) => {
const response = await got.get(baseUrl + cate);
const $ = cheerio.load(response.data);
// console.log($('title').text());
const news = $('.news_list .box-container > div').slice(0, 10).map((idx, item) => {
item = $(item);
return item.find('a[target]').attr('href');
})
.get();
// console.log(news);
return Promise.resolve(news);
})
);
const all_news = [].concat.apply([], await get_news_list(categories));
const out = await Promise.all(
all_news.map(async (news_url) => {
const news_detail = await ctx.cache.tryGet(news_url, async () => {
const result = await got.get(news_url);
const $ = cheerio.load(result.data);
console.log($('h3 ~ time').text(), news_url);
return {
title: $('h3').text(),
link: news_url,
// author: author,
pubDate: new Date($('h3 ~ time').text()).toUTCString(),
description: $('.cmsPage').html(),
};
});
return Promise.resolve(news_detail);
})
);
ctx.state.data = {
title: '麦当劳资讯',
link: baseUrl,
item: out,
};
};