From d41766582e92fc0c6688f9b24af58d828a8ddea3 Mon Sep 17 00:00:00 2001 From: Henry Wang Date: Wed, 6 May 2020 12:58:58 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20update=20ikea=20=E4=B8=8A=E6=96=B0?= =?UTF-8?q?=E7=8B=82=E9=AD=94=20(#4666)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/routes/ikea/uk/new.js | 45 ++++++------------------------------- lib/routes/ikea/uk/offer.js | 8 ++++++- 2 files changed, 14 insertions(+), 39 deletions(-) diff --git a/lib/routes/ikea/uk/new.js b/lib/routes/ikea/uk/new.js index 57b516d3e..d2fbc36f5 100644 --- a/lib/routes/ikea/uk/new.js +++ b/lib/routes/ikea/uk/new.js @@ -1,51 +1,20 @@ const got = require('@/utils/got'); -const cheerio = require('cheerio'); module.exports = async (ctx) => { const link = 'https://www.ikea.com/gb/en/news/product-news-gallery/'; - const response = await got.get(link); + const count = (await got.get('https://sik.search.blue.cdtapps.com/gb/en/product-list-page?category=products&toggles2=new_product')).data.productListPage.productCount; - const $ = cheerio.load(response.data); - - const list = $('.card__link') - .get() - .map((e) => $(e).attr('href')); - const items = []; - - await Promise.all( - list.map(async (sectionUrl) => { - const response = await got.get(sectionUrl); - - const $ = cheerio.load(response.data); - - const products = $('.product-compact').get(); - - if (products.length > 0) { - for (const p of products) { - let title = $(p).find('.product-compact__name').text() + ' '; - - title += $(p).find('.product-compact__type').text().trim() + ' '; - - title += `[${$(p).find('.product-compact__price').text().trim()}]`; - - const link = $(p).find('.product-compact__spacer > a').attr('href'); - - items.push({ - title, - description: ` img').attr('src')}'>`, - link, - }); - } - } - return Promise.resolve(); - }) - ); + const products = (await got.get(`http://sik.search.blue.cdtapps.com/gb/en/product-list-page/more-products?category=products&start=1&end=${count}`)).data.moreProducts.productWindow; ctx.state.data = { title: 'IKEA UK - New Products', link, description: 'New products released by IKEA UK.', - item: items, + item: products.map((p) => ({ + title: `${p.name} ${p.typeName} ${p.itemMeasureReferenceText}`, + description: ``, + link: p.pipUrl, + })), }; }; diff --git a/lib/routes/ikea/uk/offer.js b/lib/routes/ikea/uk/offer.js index 44f190dd8..839c2b126 100644 --- a/lib/routes/ikea/uk/offer.js +++ b/lib/routes/ikea/uk/offer.js @@ -12,11 +12,17 @@ module.exports = async (ctx) => { if (products.length > 0) { for (const p of products) { + const price = $(p).find('.product-compact__comparable-price-element').text(); + + if (!price) { + continue; + } + let title = $(p).find('.product-compact__name').text() + ' '; title += $(p).find('.product-compact__type').text().trim() + ' '; - title += `[${$(p).find('.product-compact__comparable-price-element').text()} ▶️ ${$(p).find('.product-compact__price.product-compact__price--family').text()}]`; + title += `[${price} ▶️ ${$(p).find('.product-compact__price.product-compact__price--family').text()}]`; const link = $(p).find('.product-compact__spacer > a').attr('href');