1
0

fix: Errors
All checks were successful
Remote Deploy / deploy (push) Successful in 2m20s

This commit is contained in:
2025-09-02 07:58:32 +02:00
parent bfa8359978
commit f55a996360
2 changed files with 152 additions and 121 deletions

1
.gitignore vendored
View File

@@ -2,3 +2,4 @@ node_modules
volume/browser
db
downloads
errors

View File

@@ -5,7 +5,6 @@ import parseThisShit from './parse.js';
const EMAIL = process.env.EMAIL;
const PASSWORD = process.env.PASSWORD;
//const SHAREPOINT_URL = 'https://onedrive.live.com/personal/7d8c4d9baeeebde3/_layouts/15/doc2.aspx?resid=2bddf9b7-8613-4ae3-a684-0be6d73d90bf&cid=7d8c4d9baeeebde3&ct=1748937302474&wdOrigin=OFFICECOM-WEB.START.UPLOAD&wdPreviousSessionSrc=HarmonyWeb&wdPreviousSession=ce7df0ab-aade-4df2-9e2e-492e99049666';
const SHAREPOINT_URL = 'https://spsejecnacz.sharepoint.com/:x:/s/nastenka/ESy19K245Y9BouR5ksciMvgBu3Pn_9EaT0fpP8R6MrkEmg';
const VOLUME_PATH = path.resolve('./volume/browser');
@@ -18,13 +17,46 @@ async function clearDownloadsFolder() {
}
}
async function handleError(page, err) {
try {
const errorsDir = path.resolve('./errors');
if (!fs.existsSync(errorsDir)) fs.mkdirSync(errorsDir);
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const filePath = path.join(errorsDir, `error-${timestamp}.png`);
await page.screenshot({ path: filePath, fullPage: true });
console.error(`❌ Error occurred. Screenshot saved: ${filePath}`);
// Keep only last 10 screenshots
const files = fs.readdirSync(errorsDir)
.map(f => ({
name: f,
time: fs.statSync(path.join(errorsDir, f)).mtime.getTime()
}))
.sort((a, b) => b.time - a.time);
if (files.length > 10) {
const oldFiles = files.slice(10);
for (const f of oldFiles) {
fs.unlinkSync(path.join(errorsDir, f.name));
}
}
} catch (screenshotErr) {
console.error('Failed to take screenshot:', screenshotErr);
}
console.error(err);
}
(async () => {
const browser = await puppeteer.launch({
let browser, page;
try {
browser = await puppeteer.launch({
headless: 'new',
userDataDir: VOLUME_PATH,
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
const [page] = await browser.pages();
[page] = await browser.pages();
const downloadPath = path.resolve('./downloads');
if (!fs.existsSync(downloadPath)) fs.mkdirSync(downloadPath);
@@ -63,7 +95,7 @@ async function clearDownloadsFolder() {
break;
}
}
} catch (err) {
} catch {
console.log('"Sign in with password" button not found, continuing...');
}
@@ -78,20 +110,16 @@ async function clearDownloadsFolder() {
console.log('No stay signed in prompt.');
}
// wait for navigation after login
await page.waitForNavigation({ waitUntil: 'networkidle2' });
}
// Wait for iframe containing file options
const frameHandle = await page.waitForSelector('iframe');
const frame = await frameHandle.contentFrame();
await frame.waitForSelector('button[title="File"]', { timeout: 60000 });
await frame.click('button[title="File"]');
await new Promise(r => setTimeout(r, 500));
// Click "Create a Copy"
try {
await frame.waitForSelector('div[role="menuitem"][name="Create a Copy"]', { visible: true });
} catch {
@@ -99,17 +127,13 @@ async function clearDownloadsFolder() {
await new Promise(r => setTimeout(r, 500));
}
await frame.click('div[role="menuitem"][name="Create a Copy"]');
await new Promise(r => setTimeout(r, 500));
// Click "Download a Copy"
await frame.waitForSelector('div[role="menuitem"][name="Download a Copy"]', { visible: true });
await frame.click('div[role="menuitem"][name="Download a Copy"]');
// Wait some seconds for download to start
await new Promise(r => setTimeout(r, 10000));
// Helper: wait for file to appear in download folder
function waitForFile(filename, timeout = 30000) {
return new Promise((resolve, reject) => {
const start = Date.now();
@@ -125,7 +149,6 @@ async function clearDownloadsFolder() {
});
}
// Helper: get newest .xlsx file in downloads folder
function getNewestFile(dir) {
const files = fs.readdirSync(dir)
.map(f => ({
@@ -136,7 +159,6 @@ async function clearDownloadsFolder() {
return files.length ? path.join(dir, files[0].name) : null;
}
// Wait for the downloaded file
const downloadedFilePath = getNewestFile(downloadPath);
if (!downloadedFilePath) {
throw new Error('No XLSX file found in download folder');
@@ -145,8 +167,16 @@ async function clearDownloadsFolder() {
await waitForFile(downloadedFilePath);
await parseThisShit(downloadedFilePath);
await clearDownloadsFolder();
await browser.close();
} catch (err) {
if (page) {
await handleError(page, err);
} else {
console.error('Fatal error before page init:', err);
}
if (browser) await browser.close();
process.exit(1);
}
})();