From c3950998a778c576cfec6fdec1643797d30cb9c6 Mon Sep 17 00:00:00 2001 From: Evan Date: Wed, 8 Mar 2023 12:04:17 -0800 Subject: [PATCH] feat(route): add patagonia new arrivals (#12060) * feat(route): add patagonia new arrivals * Update lib/v2/patagonia/new-arrivals.js * fix: description * feat: better image display --------- --- docs/en/shopping.md | 13 ++++ docs/shopping.md | 12 ++++ lib/v2/patagonia/maintainer.js | 3 + lib/v2/patagonia/new-arrivals.js | 61 +++++++++++++++++++ lib/v2/patagonia/radar.js | 23 +++++++ lib/v2/patagonia/router.js | 3 + .../templates/product-description.art | 4 ++ 7 files changed, 119 insertions(+) create mode 100644 lib/v2/patagonia/maintainer.js create mode 100644 lib/v2/patagonia/new-arrivals.js create mode 100644 lib/v2/patagonia/radar.js create mode 100644 lib/v2/patagonia/router.js create mode 100644 lib/v2/patagonia/templates/product-description.art diff --git a/docs/en/shopping.md b/docs/en/shopping.md index 354f922c4..b33928bec 100644 --- a/docs/en/shopping.md +++ b/docs/en/shopping.md @@ -200,6 +200,19 @@ Language +## Patagonia + +### New Arrivals + + + +| Men's | Women's | Kids' & Baby | Packs & Gear | +| ----- | ------- | ------------ | ------------ | +| mens | womens | kids | luggage | + + + + ## ShopBack ### Store diff --git a/docs/shopping.md b/docs/shopping.md index 626f58bea..61b19c2cb 100644 --- a/docs/shopping.md +++ b/docs/shopping.md @@ -244,6 +244,18 @@ For instance, in +## Patagonia + +### New Arrivals + + + +| Men's | Women's | Kids' & Baby | Packs & Gear | +| ----- | ------- | ------------ | ------------ | +| mens | womens | kids | luggage | + + + ## ShopBack ### Store diff --git a/lib/v2/patagonia/maintainer.js b/lib/v2/patagonia/maintainer.js new file mode 100644 index 000000000..4a52de0a9 --- /dev/null +++ b/lib/v2/patagonia/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/patagonia/new-arrivals/:category': ['NavePnow'], +}; diff --git a/lib/v2/patagonia/new-arrivals.js b/lib/v2/patagonia/new-arrivals.js new file mode 100644 index 000000000..d6947ed57 --- /dev/null +++ b/lib/v2/patagonia/new-arrivals.js @@ -0,0 +1,61 @@ +const got = require('@/utils/got'); +const { art } = require('@/utils/render'); +const path = require('path'); +const cheerio = require('cheerio'); +const host = 'https://www.patagonia.com'; +const categoryMap = { + mens: ['mens-new', 'mens-new-arrivals'], + womens: ['womens-new', 'womens-new-arrivals'], + kids: ['kids-new-arrivals', 'kids-baby-new-arrivals'], + luggage: ['luggage-new-arrivals', 'luggage-new-arrivals'], +}; +function extractSfrmUrl(url) { + const urlObj = new URL(url); + const sfrmValue = urlObj.searchParams.get('sfrm'); + urlObj.search = new URLSearchParams({ sfrm: sfrmValue }).toString(); + return urlObj.toString(); +} +module.exports = async (ctx) => { + const { category } = ctx.params; + const url = `${host}/on/demandware.store/Sites-patagonia-us-Site/en_US/Search-LazyGrid`; + const response = await got({ + method: 'get', + url, + searchParams: { + cgid: categoryMap[category][0], + isLazyGrid: true, + }, + }); + const data = response.data; + + const $ = cheerio.load(data); + const list = $('.product') + .map(function () { + const data = {}; + data.title = $(this).find('.product-tile').data('tealium').product_name[0]; + let imgUrl = new URL($(this).find('[itemprop="image"]').attr('content')); + imgUrl = extractSfrmUrl(imgUrl); + + const price = $(this).find('[itemprop="price"]').eq(0).text(); + data.link = host + '/' + $(this).find('[itemprop="url"]').attr('href'); + data.description = + price + + art(path.join(__dirname, 'templates/product-description.art'), { + imgUrl, + }); + data.category = $(this).find('[itemprop="category"]').attr('content'); + return data; + }) + .get(); + ctx.state.data = { + title: `Patagonia - New Arrivals - ${category.toUpperCase()}`, + link: `${host}/shop/${categoryMap[category][1]}`, + description: `Patagonia - New Arrivals - ${category.toUpperCase()}`, + item: list.map((item) => ({ + title: item.title, + description: item.description, + link: item.link, + category: item.category, + })), + }; +}; diff --git a/lib/v2/patagonia/radar.js b/lib/v2/patagonia/radar.js new file mode 100644 index 000000000..0d52cd249 --- /dev/null +++ b/lib/v2/patagonia/radar.js @@ -0,0 +1,23 @@ +module.exports = { + 'patagonia.com': { + _name: 'Patagonia', + '.': [ + { + title: 'New Arrivals', + docs: 'https://docs.rsshub.app/shopping.html#patagonia', + source: ['/shop/*new-arrivals'], + target: (_, url) => { + const param = new URL(url).pathname.split('/').pop().replace('-new-arrivals', ''); + if (param === 'new-arrivals') { + return ''; + } + if (param === 'kids-baby') { + return '/patagonia/new-arrivals/kids'; + } else { + return `/patagonia/new-arrivals/${param}`; + } + }, + }, + ], + }, +}; diff --git a/lib/v2/patagonia/router.js b/lib/v2/patagonia/router.js new file mode 100644 index 000000000..ed678b5aa --- /dev/null +++ b/lib/v2/patagonia/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/new-arrivals/:category', require('./new-arrivals')); +}; diff --git a/lib/v2/patagonia/templates/product-description.art b/lib/v2/patagonia/templates/product-description.art new file mode 100644 index 000000000..3ed24c4a4 --- /dev/null +++ b/lib/v2/patagonia/templates/product-description.art @@ -0,0 +1,4 @@ +
+ +
+