diff --git a/auction.js b/auction.js index 75df9a7..f0728c4 100644 --- a/auction.js +++ b/auction.js @@ -38,7 +38,16 @@ export async function collectAuctions() { const filterToggle = page.locator('#saved-search-261859'); await filterToggle.waitFor({ state: 'attached' }); - filterToggle.evaluate((node) => node.click()); + + await filterToggle.evaluate((node) => node.click()); + + const totalResultsText = await page + .locator('.result-number', { + hasNotText: '0 Results Total Match Your 1 Saved Search', + }) + .innerText(); + + const total = parseInt(totalResultsText.split(' ')[0]); const auctionIds = []; let shouldCollect = true; @@ -47,24 +56,31 @@ export async function collectAuctions() { const responsePromise = page.waitForResponse( 'https://easy-pass.acvauctions.com/bff/filters/auctions/buying/ended' ); - const response = await responsePromise; + + await page.evaluate(async () => { + window.scrollTo(0, document.body.scrollHeight); + }); + + const response = await responsePromise.catch(() => null); + + if (!response) { + continue; + } if (response.status() === 200) { const { data: { results }, } = await response.json(); + console.log(results.length); - if (results.length !== 0) { - auctionIds.push(...results.map((result) => result.id)); - await page.evaluate(async () => { - window.scrollTo(0, document.body.scrollHeight); - }); - } else { - shouldCollect = false; - } + auctionIds.push(...results.map((result) => result.id)); } else { shouldCollect = false; } + + if (auctionIds.length === total) { + shouldCollect = false; + } } await context.close(); diff --git a/index-local.js b/index-local.js index cae11c0..3fcf84e 100644 --- a/index-local.js +++ b/index-local.js @@ -5,26 +5,22 @@ import { proQuoteCar } from './proquote.js'; dotenv.config(); (async () => { - try { - // while (true) { - // await proQuoteCar({ - // title: '2010 GMC Acadia SLE', - // condition: [], - // price: 1250, - // city: 'Peoria, IL', - // vin: '1GKLVLED9AJ101761', - // odometer: { type: 'miles', value: 146743 }, - // year: 2010, - // make: 'GMC', - // model: 'Acadia', - // color: 'Silver', - // auctionId: 7304031, - // auctionDate: '6/2/2023', - // proQuote: { avg: 1296.71 }, - // }); - // } - await init(); - } catch (e) { - console.log(e); - } + // while (true) { + // await proQuoteCar({ + // title: '2010 GMC Acadia SLE', + // condition: [], + // price: 1250, + // city: 'Peoria, IL', + // vin: '1GKLVLED9AJ101761', + // odometer: { type: 'miles', value: 146743 }, + // year: 2010, + // make: 'GMC', + // model: 'Acadia', + // color: 'Silver', + // auctionId: 7304031, + // auctionDate: '6/2/2023', + // proQuote: { avg: 1296.71 }, + // }); + // } + await init(); })(); diff --git a/index.js b/index.js index 76f2ea1..ef38853 100644 --- a/index.js +++ b/index.js @@ -5,12 +5,6 @@ import { init } from './scrapper.js'; dotenv.config(); functions.http('initScrapping', async (_, res) => { - try { - await init(); - res.send('done'); - } catch (e) { - console.log('::: main catch :::'); - console.log(e); - res.status(400).send(e); - } + await init(); + res.send('done'); });