Allow for unlimited retries, add configuration to set a start and end time for checking, don't fail if a network error prevents a page from loading, and update the readme

This commit is contained in:
Kevin MacMartin 2021-01-03 00:35:29 -05:00
parent 34f2e4f9ca
commit 2b5900c6a7
3 changed files with 56 additions and 30 deletions

View File

@ -3,8 +3,10 @@
"password":"", "password":"",
"cv2":"", "cv2":"",
"item_page":"", "item_page":"",
"refresh_time":"60", "refresh_time":"55",
"refresh_tries": "60", "refresh_tries": "60",
"start_time": "7:00",
"end_time": "20:00",
"price_limit":"925", "price_limit":"925",
"auto_submit":"true" "auto_submit":"true"
} }

View File

@ -26,16 +26,30 @@ async function run() {
}); });
const page = await browser.newPage(), const page = await browser.newPage(),
maxVariance = 8; maxVariance = 8,
startTime = Number(config.start_time.replace(/:.*/, "")) + Number(config.start_time.replace(/^[^:]*:/, "")) / 60,
endTime = Number(config.end_time.replace(/:.*/, "")) + Number(config.end_time.replace(/^[^:]*:/, "")) / 60;
let success = true, let success = true,
randomVariance = 0, randomVariance = 0,
currentTry = 1; currentTry = 1,
date,
timeNow;
async function navigateToPage(url) {
try {
await page.goto(url, { waitUntil: "load" });
} catch (err) {
log(`Failed to navigate to ${url}, retrying in 30 seconds...`);
await page.waitForTimeout(30000);
await navigateToPage(url);
}
}
log("Started bot"); log("Started bot");
while (true) { while (true) {
await page.goto("https://secure.newegg.ca/NewMyAccount/AccountLogin.aspx?nextpage=https%3a%2f%2fwww.newegg.ca%2f", { waitUntil: "load" }); await navigateToPage("https://secure.newegg.ca/NewMyAccount/AccountLogin.aspx?nextpage=https%3a%2f%2fwww.newegg.ca%2f");
if (page.url().includes("signin")) { if (page.url().includes("signin")) {
try { try {
@ -78,35 +92,43 @@ async function run() {
await page.waitForTimeout(3500); await page.waitForTimeout(3500);
while (true) { while (true) {
await page.goto(`https://www.newegg.ca/${config.item_page}`, { waitUntil: "load" }); date = new Date();
timeNow = date.getHours() + date.getMinutes() / 60;
if (await clickButtonWithText(page, "Add to cart")) { if (timeNow >= startTime && timeNow <= endTime) {
log("Adding the product the to cart"); await navigateToPage(`https://www.newegg.ca/${config.item_page}`);
await page.waitForSelector(".modal-content", { timeout: 5000 });
await page.waitForTimeout(1000);
if (await clickButtonWithText(page, "No, thanks")) { if (await clickButtonWithText(page, "Add to cart")) {
log("Rejected product coverage option"); log("Adding the product the to cart");
} await page.waitForSelector(".modal-content", { timeout: 5000 });
await page.waitForTimeout(1000);
try { if (await clickButtonWithText(page, "No, thanks")) {
await page.waitForSelector(".message-title", { timeout: 5000 }); log("Rejected product coverage option");
log("Successfully added the item to the cart!"); }
} catch (error) {
log("Fail: Added to cart but no success window appeared"); try {
await page.waitForSelector(".message-title", { timeout: 5000 });
log("Successfully added the item to the cart!");
} catch (error) {
log("Fail: Added to cart but no success window appeared");
success = false;
}
break;
} else if (config.refresh_tries !== 0 && currentTry > config.refresh_tries) {
log(`Fail: Maximum number of refresh tries reached (${config.refresh_tries})`);
success = false; success = false;
break;
} else {
randomVariance = Math.floor(Math.random() * maxVariance);
log(`(${currentTry}) Product currently unavailable, retrying in ${Number(config.refresh_time) + randomVariance} seconds...`);
currentTry++;
await page.waitForTimeout((Number(config.refresh_time) + randomVariance) * 1000);
} }
break;
} else if (currentTry > config.refresh_tries) {
log(`Fail: Maximum number of refresh tries reached (${config.refresh_tries})`);
success = false;
break;
} else { } else {
randomVariance = Math.floor(Math.random() * maxVariance); log(`${date.getHours()}:${date.getMinutes()} is outside the active time period between ${config.start_time} and ${config.end_time}`);
log(`(${currentTry}) Product currently unavailable, retrying in ${Number(config.refresh_time) + randomVariance} seconds...`); await page.waitForTimeout(60000);
currentTry++;
await page.waitForTimeout((Number(config.refresh_time) + randomVariance) * 1000);
} }
} }
} }
@ -114,7 +136,7 @@ async function run() {
if (success) { if (success) {
while (true) { while (true) {
log("Navigating to secure checkout"); log("Navigating to secure checkout");
await page.goto("https://secure.newegg.ca/shop/cart", { waitUntil: "load" }); await navigateToPage("https://secure.newegg.ca/shop/cart");
const totalPriceElement = await page.$(".summary-content-total span strong"), const totalPriceElement = await page.$(".summary-content-total span strong"),
totalPrice = await page.evaluate(totalPriceElement => totalPriceElement.textContent, totalPriceElement); totalPrice = await page.evaluate(totalPriceElement => totalPriceElement.textContent, totalPriceElement);

View File

@ -15,10 +15,12 @@ PUPPETEER_PRODUCT=firefox npm install puppeteer
* `cv2`: code on the back of your credit card * `cv2`: code on the back of your credit card
* `item_page`: the part of the URL (starting with a slash) for the product after `https://www.newegg.ca` (eg: `/amd-ryzen-9-5900x/p/N82E16819113664`) * `item_page`: the part of the URL (starting with a slash) for the product after `https://www.newegg.ca` (eg: `/amd-ryzen-9-5900x/p/N82E16819113664`)
* `refresh_time`: amount of time between page refreshes (with a random variance) * `refresh_time`: amount of time between page refreshes (with a random variance)
* `refresh_tries`: the number of times the script will check before quitting (to avoid getting banned) * `refresh_tries`: the number of times the script will check before quitting (set to 0 for unlimited)
* `start_time`: a 24 hour time in the format `7:00` that determines when the script will start checking for the day
* `end_time`: a 24 hour time in the format `20:00` that determines when the script will stop checking for the day
* `auto_submit`: set this to false if you want to do a test run (everything up to the actual purchase) * `auto_submit`: set this to false if you want to do a test run (everything up to the actual purchase)
* `price_limit`: set this to a number you don't want your cart total to exceed before checking out * `price_limit`: set this to a number you don't want your cart total to exceed before checking out
## Credits ## Credits
Inspired by https://github.com/Ataraksia/NeweggBot Inspired by and some code borrowed from: https://github.com/Ataraksia/NeweggBot