Skip to content

Commit

Permalink
Merge pull request #4 from jpchip/paginate
Browse files Browse the repository at this point in the history
add pagination
  • Loading branch information
jpchip authored Dec 4, 2018
2 parents 4da9f4a + 9fd5b51 commit 69a86c6
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 49 deletions.
5 changes: 3 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
AMAZON_USERNAME=[email protected]
AMAZON_PASSWORD=123456
AMAZON_USERNAME=[email protected]
AMAZON_PASSWORD=123456
HEADLESS=1
28 changes: 17 additions & 11 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.0.1 - 2018-12-03
### Added
- This CHANGELOG file
- Basic working script
- README file
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.1.0 - 2018-12-04
### Added
- add page iteration
- tells you if you didn't win
- ability to run headless or not

## 0.0.1 - 2018-12-03
### Added
- This CHANGELOG file
- Basic working script
- README file
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This script will loop through all your Amazon giveaways and try to submit entries for them.

**WIP** - "no entry requirement" giveaways are implemented for first page. Need to include page iteration next.
**WIP** - Only "no entry requirement" giveaways are implemented.

Wanted a reason to experiment with puppeteer, so here goes.

Expand All @@ -11,8 +11,10 @@ Wanted a reason to experiment with puppeteer, so here goes.
Copy .env.example to .env, adding your own Amazon username and password.

```javascript
$ NPM install
$ NPM start
$ npm install
$ npm start
```

Check the console to see how things are going.

To run in browser, change `HEADLESS=0` in .env file to `HEADLESS=1`.
94 changes: 62 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,25 @@
#! /usr/bin/env node

/*global process, console */
require('dotenv').config();
const puppeteer = require('puppeteer');


async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}

async function navigateBackToGiveaways(page) {
const navigationPromise = page.waitForNavigation();
await page.waitForSelector('#giveaway-default-return-link-button');
await page.click('#giveaway-default-return-link-button');
return await navigationPromise;
//const navigationPromise = page.waitForNavigation();
//await page.waitForSelector('#giveaway-default-return-link-button');
//await page.click('#giveaway-default-return-link-button');
return await page.goBack();
}

(async () => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();

//sign in
await page.goto('https://www.amazon.com/ap/signin?_encoding=UTF8&ignoreAuthState=1&openid.assoc_handle=usflex&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fgiveaway%2Fhome%2Fref%3Dnav_custrec_signin&switch_account=');
const username = process.env.AMAZON_USERNAME;
const password = process.env.AMAZON_PASSWORD;

await page.waitForSelector('#ap_email');
await page.click('#ap_email');
await page.type('#ap_email', username);

await page.waitForSelector('#ap_password');
await page.click('#ap_password');
await page.type('#ap_password', password);

const signInPromise = page.waitForNavigation();
await page.waitForSelector('#signInSubmit');
await page.click('#signInSubmit');
await signInPromise;

//go to giveaways
await page.goto('https://www.amazon.com/ga/giveaways');

async function enterGiveaways(page, pageNumber) {
//loop through each giveaway item
console.log('Page ' + pageNumber + ' Start:');
const giveawayKeys = new Array(24);
await asyncForEach(giveawayKeys, async (val, index) => {
let i = index + 1;
Expand Down Expand Up @@ -75,16 +53,68 @@ async function navigateBackToGiveaways(page) {
}

//try to win!
console.log('waiting for box...');
await page.waitForSelector('#box_click_target');
await page.click('#box_click_target');
await page.click('#box_click_target', {delay: 2000});
const resultTextEl = await page.waitForSelector('.qa-giveaway-result-text');
const resultText = await page.evaluate(resultTextEl => resultTextEl.textContent, resultTextEl);
console.log(resultText);

await navigateBackToGiveaways(page);
} else {
console.log('giveaway ' + i + ' requires entry.');
}
});

//TODO: go to next page...
//go to next page, if we can
try {
await page.waitForSelector('.a-section > #giveawayListingPagination > .a-pagination > .a-last > a');
} catch(e) {
console.log('No more pages! Goodbye!');
return;
}

const navigationPromise = page.waitForNavigation();
await page.click('.a-section > #giveawayListingPagination > .a-pagination > .a-last > a');
await navigationPromise;

console.log('Next Page!');
await enterGiveaways(page, pageNumber + 1);
}

async function signIn(page, username, password) {
await page.goto('https://www.amazon.com/ap/signin?_encoding=UTF8&ignoreAuthState=1&openid.assoc_handle=usflex&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fgiveaway%2Fhome%2Fref%3Dnav_custrec_signin&switch_account=');

await page.waitForSelector('#ap_email');
await page.click('#ap_email');
await page.type('#ap_email', username);

await page.waitForSelector('#ap_password');
await page.click('#ap_password');
await page.type('#ap_password', password);

const signInPromise = page.waitForNavigation();
await page.waitForSelector('#signInSubmit');
await page.click('#signInSubmit');
await signInPromise;
}

//start index code
(async () => {
const headless = (process.env.HEADLESS === 1);
const browser = await puppeteer.launch({headless: headless});
const page = await browser.newPage();

//sign in
const username = process.env.AMAZON_USERNAME;
const password = process.env.AMAZON_PASSWORD;
await signIn(page, username, password);

//go to giveaways
await page.goto('https://www.amazon.com/ga/giveaways?pageId=50');

//enter giveaways
await enterGiveaways(page, 1);

await browser.close();
})();
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "giveaway-grabber",
"version": "0.0.1",
"version": "0.1.0",
"description": "Automates entries for Amazon Giveaways",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 69a86c6

Please sign in to comment.