Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ if (process.argv[2] === '--headless=false') {
headless = false
}

let ignoreErrors = false;

// accept --ignore-errors argument to control error handling behavior
if (process.argv.includes('--ignore-errors')) {
ignoreErrors = true;
}

const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))

const getProgress = async () => {
Expand Down Expand Up @@ -131,18 +138,27 @@ const getMonthAndYear = async (metadata, page) => {
Note: I have tried both left arrow press and clicking directly the left side of arrow using playwright click method.
However, both of them are not working. So, I have injected the click method in the html.
*/
await page.evaluate(() => document.getElementsByClassName('SxgK2b OQEhnd')[0].click())

// we wait until new photo is loaded
await page.waitForURL((url) => {
return url.host === 'photos.google.com' && url.href !== currentUrl
},
{
timeout: timeoutValue,
})

await downloadPhoto(page)
await saveProgress(page)
try {
await page.evaluate(() => document.getElementsByClassName('SxgK2b OQEhnd')[0].click())

// we wait until new photo is loaded
await page.waitForURL((url) => {
return url.host === 'photos.google.com' && url.href !== currentUrl
},
{
timeout: timeoutValue,
})

await downloadPhoto(page)
await saveProgress(page)
} catch (error) {
console.error('An error occurred:', error);
await saveProgress(page);
if (!ignoreErrors) {
console.log('Aborting due to error. Use --ignore-errors to continue on errors.');
process.exit(1);
}
}
Comment on lines 131 to 164
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Add consecutive error limit to prevent infinite loops.

When ignoreErrors is enabled and errors persist (e.g., DOM structure changed, network issues), the script will loop indefinitely, repeatedly encountering the same error. This wastes resources and provides no clear indication when manual intervention is needed.

Add a consecutive error counter to abort after a threshold is reached:

 chromium.use(stealth())
 
 const timeoutValue = 300000
 const userDataDir = './session'
 const downloadPath = './download'
 
 let headless = true
 
 // accept --headless=false argument to run in headful mode
 if (process.argv[2] === '--headless=false') {
   headless = false
 }
 
 let ignoreErrors = false;
 
 // accept --ignore-errors argument to control error handling behavior
 if (process.argv.includes('--ignore-errors')) {
   ignoreErrors = true;
 }
+
+const MAX_CONSECUTIVE_ERRORS = 3;
+let consecutiveErrors = 0;

Then update the catch block:

     } catch (error) {
       console.error('An error occurred:', error);
       await saveProgress(page);
+      consecutiveErrors++;
+      
+      if (consecutiveErrors >= MAX_CONSECUTIVE_ERRORS) {
+        console.error(`Aborting after ${MAX_CONSECUTIVE_ERRORS} consecutive errors.`);
+        process.exit(1);
+      }
+      
       if (!ignoreErrors) {
         console.log('Aborting due to error. Use --ignore-errors to continue on errors.');
         process.exit(1);
       }
+      console.log(`Continuing despite error (${consecutiveErrors}/${MAX_CONSECUTIVE_ERRORS} consecutive errors)...`);
     }
   }

And reset the counter on success (after line 153):

       await downloadPhoto(page)
       await saveProgress(page)
+      consecutiveErrors = 0;  // Reset on success
     } catch (error) {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
try {
await page.evaluate(() => document.getElementsByClassName('SxgK2b OQEhnd')[0].click())
// we wait until new photo is loaded
await page.waitForURL((url) => {
return url.host === 'photos.google.com' && url.href !== currentUrl
},
{
timeout: timeoutValue,
})
await downloadPhoto(page)
await saveProgress(page)
} catch (error) {
console.error('An error occurred:', error);
await saveProgress(page);
if (!ignoreErrors) {
console.log('Aborting due to error. Use --ignore-errors to continue on errors.');
process.exit(1);
}
}
chromium.use(stealth())
const timeoutValue = 300000
const userDataDir = './session'
const downloadPath = './download'
let headless = true
// accept --headless=false argument to run in headful mode
if (process.argv[2] === '--headless=false') {
headless = false
}
let ignoreErrors = false
// accept --ignore-errors argument to control error handling behavior
if (process.argv.includes('--ignore-errors')) {
ignoreErrors = true
}
const MAX_CONSECUTIVE_ERRORS = 3
let consecutiveErrors = 0
// … inside your main loop …
try {
await page.evaluate(() => document.getElementsByClassName('SxgK2b OQEhnd')[0].click())
// we wait until new photo is loaded
await page.waitForURL(
(url) => url.host === 'photos.google.com' && url.href !== currentUrl,
{ timeout: timeoutValue }
)
await downloadPhoto(page)
await saveProgress(page)
consecutiveErrors = 0 // Reset on success
} catch (error) {
console.error('An error occurred:', error)
await saveProgress(page)
consecutiveErrors++
if (consecutiveErrors >= MAX_CONSECUTIVE_ERRORS) {
console.error(`Aborting after ${MAX_CONSECUTIVE_ERRORS} consecutive errors.`)
process.exit(1)
}
if (!ignoreErrors) {
console.log('Aborting due to error. Use --ignore-errors to continue on errors.')
process.exit(1)
}
console.log(
`Continuing despite error (${consecutiveErrors}/${MAX_CONSECUTIVE_ERRORS} consecutive errors)...`
)
}
🤖 Prompt for AI Agents
In index.js around lines 141 to 161, add a consecutive error counter and
threshold to avoid infinite loops when ignoreErrors is true: declare a let
consecutiveErrorCount = 0 and const MAX_CONSECUTIVE_ERRORS = <reasonable number>
in the surrounding scope, increment consecutiveErrorCount inside the catch block
before deciding whether to continue, and if consecutiveErrorCount >=
MAX_CONSECUTIVE_ERRORS log a clear fatal message and call process.exit(1)
regardless of ignoreErrors; after successful operations (immediately after line
153 where the success path continues) reset consecutiveErrorCount = 0 so
transient failures don't trigger the abort.

}
await browser.close()
await exiftool.end()
Expand Down