Skip to content

Commit

Permalink
feat(route): add patagonia new arrivals (DIYgod#12060)
Browse files Browse the repository at this point in the history
* feat(route): add patagonia new arrivals

* Update lib/v2/patagonia/new-arrivals.js

* fix: description

* feat: better image display

---------
  • Loading branch information
EthanWng97 authored Mar 8, 2023
1 parent 197610d commit c395099
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docs/en/shopping.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,19 @@ Language

</RouteEn>

## Patagonia

### New Arrivals

<RouteEn author="NavePnow" example="/patagonia/new-arrivals/mens" path="/patagonia/new-arrivals/:category" :paramsDesc="['category, see below']">

| Men's | Women's | Kids' & Baby | Packs & Gear |
| ----- | ------- | ------------ | ------------ |
| mens | womens | kids | luggage |

</RouteEn>


## ShopBack

### Store
Expand Down
12 changes: 12 additions & 0 deletions docs/shopping.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,18 @@ For instance, in <https://www.leboncoin.fr/recherche/?**category=10&locations=Pa

</Route>

## Patagonia

### New Arrivals

<Route author="NavePnow" example="/patagonia/new-arrivals/mens" path="/patagonia/new-arrivals/:category" :paramsDesc="['分类, 见下表']">

| Men's | Women's | Kids' & Baby | Packs & Gear |
| ----- | ------- | ------------ | ------------ |
| mens | womens | kids | luggage |

</Route>

## ShopBack

### Store
Expand Down
3 changes: 3 additions & 0 deletions lib/v2/patagonia/maintainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
'/patagonia/new-arrivals/:category': ['NavePnow'],
};
61 changes: 61 additions & 0 deletions lib/v2/patagonia/new-arrivals.js
Original file line number Diff line number Diff line change
@@ -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,
})),
};
};
23 changes: 23 additions & 0 deletions lib/v2/patagonia/radar.js
Original file line number Diff line number Diff line change
@@ -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}`;
}
},
},
],
},
};
3 changes: 3 additions & 0 deletions lib/v2/patagonia/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function (router) {
router.get('/new-arrivals/:category', require('./new-arrivals'));
};
4 changes: 4 additions & 0 deletions lib/v2/patagonia/templates/product-description.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div>
<img src={{imgUrl}}>
</div>

0 comments on commit c395099

Please sign in to comment.