feat: update ikea 上新狂魔 (#4666)

This commit is contained in:
Henry Wang 2020-05-06 12:58:58 +01:00 committed by GitHub
parent 1146ef1b30
commit d41766582e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 39 deletions

View File

@ -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 src='${$(p).find('.range-image-claim-height > 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: `<img src="${p.mainImageUrl}">`,
link: p.pipUrl,
})),
};
};

View File

@ -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');