From 40e0c7e8393bfba796131883424d68a59d488c35 Mon Sep 17 00:00:00 2001 From: RohitR311 Date: Tue, 3 Dec 2024 22:21:26 +0530 Subject: [PATCH] feat: add scrapeSchema data accumulation logic --- maxun-core/src/interpret.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/maxun-core/src/interpret.ts b/maxun-core/src/interpret.ts index 114605e2..a7a5de47 100644 --- a/maxun-core/src/interpret.ts +++ b/maxun-core/src/interpret.ts @@ -329,15 +329,23 @@ export default class Interpreter extends EventEmitter { scrapeSchema: async (schema: Record) => { await this.ensureScriptsLoaded(page); - // Scrape data using the schema const scrapeResult = await page.evaluate((schemaObj) => window.scrapeSchema(schemaObj), schema); - // Log result and accumulate it - console.log("Scrape result:", scrapeResult); - this.cumulativeResults.push(...(Array.isArray(scrapeResult) ? scrapeResult : [scrapeResult])); + const newResults = Array.isArray(scrapeResult) ? scrapeResult : [scrapeResult]; + newResults.forEach((result) => { + Object.entries(result).forEach(([key, value]) => { + const keyExists = this.cumulativeResults.some( + (item) => key in item && item[key] !== undefined + ); + + if (!keyExists) { + this.cumulativeResults.push({ [key]: value }); + } + }); + }); const mergedResult: Record[] = [ - Object.fromEntries( + Object.fromEntries( Object.entries( this.cumulativeResults.reduce((acc, curr) => { Object.entries(curr).forEach(([key, value]) => {