From 94214a9d5e1c1cf93dc52643c5e49a021485c8bc Mon Sep 17 00:00:00 2001 From: jinkin Date: Tue, 5 Jan 2021 11:22:31 +0800 Subject: [PATCH 1/2] add mcdonalds news --- docs/shopping.md | 11 ++++++++ lib/router.js | 3 +++ lib/routes/mcdonalds/news.js | 51 ++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 lib/routes/mcdonalds/news.js diff --git a/docs/shopping.md b/docs/shopping.md index eae0a16575..caba5c7a34 100644 --- a/docs/shopping.md +++ b/docs/shopping.md @@ -347,3 +347,14 @@ For instance, in + +## 麦当劳 + +### 麦当劳活动资讯 + + +| 全部分类 | 社会责任 | 人员品牌 | 产品故事 | 优惠 | 品牌文化 | 活动速报 | +| -------- | ------- | --- | ------- | ------ | ------- | -------- | +| news_list | responsibility | brand | product | sales | culture | event | + + diff --git a/lib/router.js b/lib/router.js index 5bb0f656c3..2f05b7b278 100644 --- a/lib/router.js +++ b/lib/router.js @@ -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; diff --git a/lib/routes/mcdonalds/news.js b/lib/routes/mcdonalds/news.js new file mode 100644 index 0000000000..93949dcc7b --- /dev/null +++ b/lib/routes/mcdonalds/news.js @@ -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, + }; + +}; \ No newline at end of file From ae1ceb6c6057de36150a571d752d7ffd3300c26d Mon Sep 17 00:00:00 2001 From: Jinkin Date: Tue, 5 Jan 2021 18:21:35 +0800 Subject: [PATCH 2/2] Update news.js --- lib/routes/mcdonalds/news.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/routes/mcdonalds/news.js b/lib/routes/mcdonalds/news.js index 93949dcc7b..f62e83a77b 100644 --- a/lib/routes/mcdonalds/news.js +++ b/lib/routes/mcdonalds/news.js @@ -28,7 +28,7 @@ module.exports = async (ctx) => { const result = await got.get(news_url); const $ = cheerio.load(result.data); - console.log($('h3 ~ time').text(), news_url); +// console.log($('h3 ~ time').text(), news_url); return { title: $('h3').text(), @@ -48,4 +48,4 @@ module.exports = async (ctx) => { item: out, }; -}; \ No newline at end of file +};