From 7d0fb98baa5a52cd0660fa1fa0e525e48fe83ce8 Mon Sep 17 00:00:00 2001 From: Edward Peterson Date: Wed, 16 Nov 2022 18:14:03 -0500 Subject: [PATCH] Added ebay processor --- index.js | 2 - processors/ebay.processor.js | 111 +++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 processors/ebay.processor.js diff --git a/index.js b/index.js index 82f7bbc..2ffe0f6 100644 --- a/index.js +++ b/index.js @@ -32,8 +32,6 @@ app.post('/convertGalleryToHar', async (req, res) => { console.log('Processor found'); try { const payloads = await run(url, processor); - console.log(payloads); - res.status(200).json({ log: { entries: payloads diff --git a/processors/ebay.processor.js b/processors/ebay.processor.js new file mode 100644 index 0000000..29ab417 --- /dev/null +++ b/processors/ebay.processor.js @@ -0,0 +1,111 @@ +const _ = require('lodash'); +const path = require('path'); +module.exports = { + baseUrl: 'ebay.com', + execute: async function (page) { + const thumbnailGallerySelector = '.ux-thumb-image-carousel'; + const thumbnailGalleryItemSelector = '.ux-image-filmstrip-carousel-item > img:nth-child(1)'; + const sellerSelector = '.ux-seller-section__item--seller > a:nth-child(1) > span:nth-child(1)' + const descriptionSelectr = '#desc_ifr'; + //page is considered loaded + //get the seller to see if we can tell what layout its using + + const sellerElement = await page.$(sellerSelector); + const seller = await page.evaluate(el => el.textContent, sellerElement); + console.log(`User is '${seller}'`); + if (seller === 'classicautomall') { + await page.waitForSelector(descriptionSelectr); + + return await classicautomall(page); + } else if (seller === 'gateway-classic-cars') { + await page.waitForSelector(descriptionSelectr); + + return await gatewayclassiccars(page); + } else { + var sources = []; + const hasThumbnailGallery = !!(await page.$(thumbnailGallerySelector)); + // const hasViewAllButton = !!(await page.$('.thumbnail-list-wrapper > a.cgg-btn:nth-child(6)')) + if (hasThumbnailGallery) { + + const carouselItems = await page.$$(thumbnailGalleryItemSelector); + // console.log(carouselItems) + const sourcesFromThumbnailGallery = await Promise.all(carouselItems.map(async carouselItem => { + const src = await page.evaluate(el => el.getAttribute('src'), carouselItem); + // console.log(src); + return { url: convertThumbnailUrlToFullSize(src) }; + })); + sources = sourcesFromThumbnailGallery; + } + const descriptionIframe = await page.$('#desc_ifr'); + const descriptionUrl = await page.evaluate(el => el.getAttribute('src'), descriptionIframe) + await page.goto(descriptionUrl); + //check the description if it has viewall button + const hasViewAllButton = !!(await page.$('.thumbnail-list-wrapper > a.cgg-btn:nth-child(6)')) + console.log('hasViewALl', hasViewAllButton) + if (hasViewAllButton) { + const openFullGallerySelector = '.thumbnail-list-wrapper > a.cgg-btn:nth-child(6)'; + const nextUrl = await page.evaluate(el => el.getAttribute('href'), await page.$(openFullGallerySelector)); + await page.goto(nextUrl); + await page.waitForSelector('.photo'); + const images = await page.$$('.photo'); + sources = await Promise.all(images.map(async carouselItem => { + const src = await page.evaluate(el => el.getAttribute('src'), carouselItem); + // console.log(src); + return { url: src }; + })); + return sources; + } + const hasSectionedImages = !!(await page.$('main.content')); + console.log(hasSectionedImages); + if(hasSectionedImages) { + const imageSelector = 'main.content > div > div > div > a > img'; + const images = await page.$$(imageSelector); + console.log(images.length) + sources = await Promise.all(images.map(async carouselItem => { + const src = await page.evaluate(el => el.getAttribute('src'), carouselItem); + // console.log(src); + return { url: src }; + })); + return sources; + } + + return sources; + } + } +} +function convertThumbnailUrlToFullSize(url) { + const baseName = url.substring(url.lastIndexOf('/') + 1) + const basePath = url.replace(baseName, ''); + return `${basePath}s-l1600.jpg`; +} +async function classicautomall(page) { + const descriptionIframe = await page.$('#desc_ifr'); + const descriptionUrl = await page.evaluate(el => el.getAttribute('src'), descriptionIframe) + await page.goto(descriptionUrl); + // const iframe = descriptionIframe.contentFrame(); + // await .waitForSelector('section.image-gallery') + const items = await page.$$('.lg-gallery > img:nth-child(1)'); + + const sources = await Promise.all(items.map(async carouselItem => { + const src = await page.evaluate(el => el.getAttribute('src'), carouselItem); + // console.log(src); + return { url: src }; + })); + return sources; +} +async function gatewayclassiccars(page) { + const descriptionIframe = await page.$('#desc_ifr'); + const descriptionUrl = await page.evaluate(el => el.getAttribute('src'), descriptionIframe) + await page.goto(descriptionUrl); + const openFullGallerySelector = '.thumbnail-list-wrapper > a.cgg-btn:nth-child(6)'; + const nextUrl = await page.evaluate(el => el.getAttribute('href'), await page.$(openFullGallerySelector)); + await page.goto(nextUrl); + await page.waitForSelector('.photo'); + const images = await page.$$('.photo'); + const sources = await Promise.all(images.map(async carouselItem => { + const src = await page.evaluate(el => el.getAttribute('src'), carouselItem); + // console.log(src); + return { url: src }; + })); + return sources; +} \ No newline at end of file