From bbac98491f5d607210874e6fd05e576d870e4882 Mon Sep 17 00:00:00 2001 From: Evan Date: Thu, 9 Mar 2023 13:46:49 -0800 Subject: [PATCH] feat(route): zagg new arrivals (#12059) * feat(route): zagg new arrivals * Update docs/en/shopping.md Co-authored-by: Tony * chore: use web api instead * chore: use query instead of params * fix: type * fix: link * Update lib/v2/zagg/new-arrivals.js --------- --- docs/en/shopping.md | 9 +++++ docs/shopping.md | 8 ++++ lib/v2/zagg/maintainer.js | 3 ++ lib/v2/zagg/new-arrivals.js | 52 ++++++++++++++++++++++++++ lib/v2/zagg/radar.js | 16 ++++++++ lib/v2/zagg/router.js | 3 ++ lib/v2/zagg/templates/new-arrivals.art | 3 ++ 7 files changed, 94 insertions(+) create mode 100644 lib/v2/zagg/maintainer.js create mode 100644 lib/v2/zagg/new-arrivals.js create mode 100644 lib/v2/zagg/radar.js create mode 100644 lib/v2/zagg/router.js create mode 100644 lib/v2/zagg/templates/new-arrivals.art diff --git a/docs/en/shopping.md b/docs/en/shopping.md index 206384114..fd007d2df 100644 --- a/docs/en/shopping.md +++ b/docs/en/shopping.md @@ -234,3 +234,12 @@ Language ### PS5 stock UK + +## Zagg + +### New Arrivals + + + +For instance, in , the query is `brand=164&cat=3038%2C3041` + diff --git a/docs/shopping.md b/docs/shopping.md index 62bfab693..f08572498 100644 --- a/docs/shopping.md +++ b/docs/shopping.md @@ -284,6 +284,14 @@ For instance, in +## Zagg + +### New Arrivals + + + +For instance, in , the query is `brand=164&cat=3038%2C3041` + ## 大麦网 ### 票务更新 diff --git a/lib/v2/zagg/maintainer.js b/lib/v2/zagg/maintainer.js new file mode 100644 index 000000000..df9ce629f --- /dev/null +++ b/lib/v2/zagg/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/new-arrivals/:query?': ['NavePnow'], +}; diff --git a/lib/v2/zagg/new-arrivals.js b/lib/v2/zagg/new-arrivals.js new file mode 100644 index 000000000..c0074c61d --- /dev/null +++ b/lib/v2/zagg/new-arrivals.js @@ -0,0 +1,52 @@ +const got = require('@/utils/got'); +const { art } = require('@/utils/render'); +const path = require('path'); +const cheerio = require('cheerio'); +const host = 'https://www.zagg.com/en_us'; +module.exports = async (ctx) => { + const query = ctx.params.query; + const params = new URLSearchParams(query); + const brands = params.get('brand'); + const categories = params.get('cat'); + + const url = `${host}/new-arrivals`; + const response = await got({ + headers: { + 'X-Requested-With': 'XMLHttpRequest', + }, + method: 'post', + url, + searchParams: { + cat: categories, + brand: brands, + }, + }); + const products = response.data.products; + + const $ = cheerio.load(products); + const list = $('.item.product.product-item') + .map(function () { + const data = {}; + const details = $(this).find('.product.details-box').html(); + data.link = $(this).find('.product-item-link').eq(0).attr('href'); + data.title = $(this).find('.product-item-link').text(); + const regex = /(https.*?)\?/; + const imgUrl = $(this).find('img').eq(0).attr('data-src').match(regex)[1]; + const img = art(path.join(__dirname, 'templates/new-arrivals.art'), { + imgUrl, + }); + data.description = details + img; + return data; + }) + .get(); + ctx.state.data = { + title: 'Zagg - New Arrivals', + link: response.url, + description: 'Zagg - New Arrivals', + item: list.map((item) => ({ + title: item.title, + description: item.description, + link: item.link, + })), + }; +}; diff --git a/lib/v2/zagg/radar.js b/lib/v2/zagg/radar.js new file mode 100644 index 000000000..65e941cd8 --- /dev/null +++ b/lib/v2/zagg/radar.js @@ -0,0 +1,16 @@ +module.exports = { + 'zagg.com': { + _name: 'New Arrivals', + '.': [ + { + title: 'Zagg - New Arrivals', + docs: 'https://docs.rsshub.app/shopping.html#zagg', + source: ['/en_us/new-arrivals'], + target: (_, url) => { + const queryString = url.split('?')[1]; + return `/zagg/new-arrivals/${queryString}`; + }, + }, + ], + }, +}; diff --git a/lib/v2/zagg/router.js b/lib/v2/zagg/router.js new file mode 100644 index 000000000..fad0c8847 --- /dev/null +++ b/lib/v2/zagg/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/new-arrivals/:query?', require('./new-arrivals')); +}; diff --git a/lib/v2/zagg/templates/new-arrivals.art b/lib/v2/zagg/templates/new-arrivals.art new file mode 100644 index 000000000..43f9138e6 --- /dev/null +++ b/lib/v2/zagg/templates/new-arrivals.art @@ -0,0 +1,3 @@ +
+ +