Added screenshots #3

Merged
saif.haq merged 1 commits from psw-crawler into main 2026-01-23 07:12:11 +00:00
1 changed files with 46 additions and 4 deletions

View File

@ -3,6 +3,7 @@ const luxon = require("luxon");
const fs = require("fs"); const fs = require("fs");
const path = require("path"); const path = require("path");
const dotenv = require("dotenv").config({ path: __dirname + "/.env" }); const dotenv = require("dotenv").config({ path: __dirname + "/.env" });
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
const utils = require("./utils"); const utils = require("./utils");
@ -14,6 +15,14 @@ const utils = require("./utils");
)} >=========` )} >=========`
); );
const syncDate = luxon.DateTime.now().toFormat("yyyy-MM-dd");
const screenshotDirectory = path.join(__dirname, `screenshots/psw/${syncDate}`);
if (!fs.existsSync(screenshotDirectory)) {
fs.mkdirSync(screenshotDirectory, { recursive: true });
}
/** /**
* loading config data * loading config data
@ -47,9 +56,19 @@ const utils = require("./utils");
config[environment].psw_url, config[environment].psw_url,
{ {
waitUntil: "domcontentloaded", waitUntil: "domcontentloaded",
timeout: 0 timeout: 7000
} });
);
// wait for 3 sec
console.log("Waiting 3 sec and taking screenshot.");
await new Promise((resolve) => setTimeout(resolve, 3 * 1000));
// take screenshot
let screenshotPath = path.join(
screenshotDirectory,
`${luxon.DateTime.now().toMillis()}.png`
);
await page.screenshot({ path: screenshotPath, fullPage: true });
await page.waitForSelector("tbody tr.ItemStyle"); await page.waitForSelector("tbody tr.ItemStyle");
@ -78,6 +97,18 @@ const utils = require("./utils");
}); });
}); });
// wait for 3 sec
console.log("Waiting 3 sec and taking screenshot..");
await new Promise((resolve) => setTimeout(resolve, 3 * 1000));
// take screenshot
screenshotPath = path.join(
screenshotDirectory,
`${luxon.DateTime.now().toMillis()}.png`
);
await page.screenshot({ path: screenshotPath, fullPage: true });
const jsonDir = path.join(config[environment].psw_fi_invoices_path, "data/unprocessed"); const jsonDir = path.join(config[environment].psw_fi_invoices_path, "data/unprocessed");
fs.mkdirSync(jsonDir, { recursive: true }); fs.mkdirSync(jsonDir, { recursive: true });
@ -86,8 +117,19 @@ const outputFile = path.join(jsonDir, `${luxon.DateTime.now().toMillis()}.json`)
fs.writeFileSync(outputFile, JSON.stringify(data, null, 2)); fs.writeFileSync(outputFile, JSON.stringify(data, null, 2));
console.log(`Saved JSON to ${outputFile}`); console.log(`Saved JSON to ${outputFile}`);
console.log("Waiting 5 sec and taking screenshot before closing browser...");
// wait for 5 sec
await new Promise((resolve) => setTimeout(resolve, 5 * 1000));
// take screenshot
screenshotPath = path.join(
screenshotDirectory,
`${luxon.DateTime.now().toMillis()}.png`
);
await page.screenshot({ path: screenshotPath, fullPage: true });
await page.close(); await page.close();
await browser.close(); await browser.close();
})(); })();