|
| 1 | +const got = require('@/utils/got'); |
| 2 | +const { art } = require('@/utils/render'); |
| 3 | +const path = require('path'); |
| 4 | +const cheerio = require('cheerio'); |
| 5 | +const host = 'https://www.zagg.com/en_us'; |
| 6 | +module.exports = async (ctx) => { |
| 7 | + const query = ctx.params.query; |
| 8 | + const params = new URLSearchParams(query); |
| 9 | + const brands = params.get('brand'); |
| 10 | + const categories = params.get('cat'); |
| 11 | + |
| 12 | + const url = `${host}/new-arrivals`; |
| 13 | + const response = await got({ |
| 14 | + headers: { |
| 15 | + 'X-Requested-With': 'XMLHttpRequest', |
| 16 | + }, |
| 17 | + method: 'post', |
| 18 | + url, |
| 19 | + searchParams: { |
| 20 | + cat: categories, |
| 21 | + brand: brands, |
| 22 | + }, |
| 23 | + }); |
| 24 | + const products = response.data.products; |
| 25 | + |
| 26 | + const $ = cheerio.load(products); |
| 27 | + const list = $('.item.product.product-item') |
| 28 | + .map(function () { |
| 29 | + const data = {}; |
| 30 | + const details = $(this).find('.product.details-box').html(); |
| 31 | + data.link = $(this).find('.product-item-link').eq(0).attr('href'); |
| 32 | + data.title = $(this).find('.product-item-link').text(); |
| 33 | + const regex = /(https.*?)\?/; |
| 34 | + const imgUrl = $(this).find('img').eq(0).attr('data-src').match(regex)[1]; |
| 35 | + const img = art(path.join(__dirname, 'templates/new-arrivals.art'), { |
| 36 | + imgUrl, |
| 37 | + }); |
| 38 | + data.description = details + img; |
| 39 | + return data; |
| 40 | + }) |
| 41 | + .get(); |
| 42 | + ctx.state.data = { |
| 43 | + title: 'Zagg - New Arrivals', |
| 44 | + link: response.url, |
| 45 | + description: 'Zagg - New Arrivals', |
| 46 | + item: list.map((item) => ({ |
| 47 | + title: item.title, |
| 48 | + description: item.description, |
| 49 | + link: item.link, |
| 50 | + })), |
| 51 | + }; |
| 52 | +}; |
0 commit comments