Skip to content

Commit

Permalink
Merge pull request #51 from jpchip/catch-navigation-errors
Browse files Browse the repository at this point in the history
Catch navigation errors
  • Loading branch information
jpchip authored Apr 11, 2019
2 parents 10a2854 + 425d440 commit ee01e69
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions src/giveaways.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,10 @@ async function navigateToGiveaway(page, giveawayNumber) {
}

/**
* Checks for the result of the giveaway entry and log appropriately
* Checks for the result of the giveaway entry and log appropriately.
* Returns true if result found, false if not.
* @param {Puppeteer.Page} page
* @returns {Promise<void>}
* @returns {Promise<boolean>}
*/
async function handleGiveawayResult(page) {
try {
Expand Down Expand Up @@ -201,29 +202,41 @@ async function handleGiveawayResult(page) {
await sgMail.send(msg);
}
}
return true;
} catch (error) {
console.log('could not get result, oh well. Moving on!');
return false;
}
}

/**
* Attempts to enter a no entry requirement type giveaway
* Attempts to enter a no entry requirement type giveaway.
* Will try again if it fails once.
* @param {Puppeteer.Page} page
* @param {boolean} [repeatAttempt]
* @returns {Promise<void>}
*/
async function enterNoEntryRequirementGiveaway(page) {
async function enterNoEntryRequirementGiveaway(page, repeatAttempt) {
console.log('waiting for box...');
await checkForSwitchAccount(page);
await checkForPassword(page);
await checkForCaptcha(page);
try {
await page.waitForSelector('.tapToSeeText', { visible: true });
await page.waitFor(
() => document.querySelector('.tapToSeeText').style.opacity === '1'
);
await page.waitForSelector('#box_click_target');
await page.click('#box_click_target', { delay: 2000 });
} catch (error) {
console.log('could not find box?');
}

await handleGiveawayResult(page);
const resultFound = await handleGiveawayResult(page);
if (!resultFound && !repeatAttempt) {
console.log('lets try that again.');
await enterNoEntryRequirementGiveaway(page, true);
}
}

/**
Expand Down Expand Up @@ -270,6 +283,7 @@ async function enterVideoGiveaway(page) {
async function enterGiveaways(page, pageNumber) {
//loop through each giveaway item
console.log('Page ' + pageNumber + ' Start:');
let giveawayExists = true;
const giveawayKeys = new Array(24);
await asyncForEach(giveawayKeys, async (val, index) => {
let i = index + 1;
Expand All @@ -280,6 +294,15 @@ async function enterGiveaways(page, pageNumber) {
);
} catch (error) {
console.log('giveaway ' + i + ' did not exist?');
giveawayExists = false;
}

if (!giveawayExists) {
// it's weird that it couldn't find a giveaway, let's make sure we
// aren't on some other page...
await checkForSwitchAccount(page);
await checkForPassword(page);
await checkForCaptcha(page);
return;
}

Expand All @@ -299,7 +322,12 @@ async function enterGiveaways(page, pageNumber) {
);

if (noEntryRequired.length > 0 || videoRequired.length > 0) {
await navigateToGiveaway(page, i);
try {
await navigateToGiveaway(page, i);
} catch (error) {
console.log('could not navigate to giveaway ' + i + '. Next!');
return;
}

//check if ended
let ended = await hasGiveawayEnded(page);
Expand Down Expand Up @@ -328,6 +356,7 @@ async function enterGiveaways(page, pageNumber) {

//go back
await page.goBack();
await checkForSwitchAccount(page);
await checkForPassword(page, pageNumber);
} else {
console.log('giveaway ' + i + ' cannot be entered.');
Expand Down

0 comments on commit ee01e69

Please sign in to comment.