-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jared Chapiewsky
committed
Dec 3, 2018
1 parent
0dac991
commit 4da9f4a
Showing
9 changed files
with
993 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# This file is for unifying the coding style for different editors and IDEs | ||
# editorconfig.org | ||
|
||
root = true | ||
|
||
[*] | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[**.js] | ||
indent_style = tab | ||
indent_size = 4 | ||
|
||
[**.json] | ||
indent_style = space | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
AMAZON_USERNAME=fake@example.com | ||
AMAZON_PASSWORD=123456 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"env": { | ||
"browser": false, | ||
"commonjs": true, | ||
"es6": true | ||
}, | ||
"extends": "eslint:recommended", | ||
"parserOptions": { | ||
"ecmaVersion": 2017 | ||
}, | ||
"rules": { | ||
"indent": [ | ||
"error", | ||
"tab" | ||
], | ||
"linebreak-style": [ | ||
"error", | ||
"unix" | ||
], | ||
"quotes": [ | ||
"error", | ||
"single" | ||
], | ||
"semi": [ | ||
"error", | ||
"always" | ||
], | ||
"no-console" : 0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.idea/ | ||
node_modules/ | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,18 @@ | ||
# giveaway-grabber | ||
# giveaway-grabber 💰 | ||
|
||
WIP | ||
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. | ||
|
||
Wanted a reason to experiment with puppeteer, so here goes. | ||
|
||
## Installing/Getting started | ||
|
||
Copy .env.example to .env, adding your own Amazon username and password. | ||
|
||
```javascript | ||
$ NPM install | ||
$ NPM start | ||
``` | ||
|
||
Check the console to see how things are going. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,90 @@ | ||
const puppeteer = require('puppeteer'); | ||
|
||
(async () => { | ||
const browser = await puppeteer.launch(); | ||
const page = await browser.newPage(); | ||
await page.goto('https://example.com'); | ||
await page.screenshot({path: 'example.png'}); | ||
|
||
await browser.close(); | ||
})(); | ||
/*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; | ||
} | ||
|
||
(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'); | ||
|
||
//loop through each giveaway item | ||
const giveawayKeys = new Array(24); | ||
await asyncForEach(giveawayKeys, async (val, index) => { | ||
let i = index + 1; | ||
try{ | ||
await page.waitForSelector('#giveaway-grid > #giveaway-item-' + i, { timeout: 5000 }); | ||
} catch(error) { | ||
console.log('giveaway ' + i + ' did not exist?'); | ||
return; | ||
} | ||
|
||
//only do "no entry requirement" giveaways (for now) | ||
const noEntryRequired = await page.$x('//*[@id="giveaway-item-' + i +'"]/a/div[2]/div[2]/span[contains(text(), "No entry requirement")]'); | ||
if (noEntryRequired.length > 0) { | ||
const giveawayItemPromise = page.waitForNavigation(); | ||
await page.click('#giveaway-grid > #giveaway-item-' + i + ' > .a-link-normal > .a-section > .a-spacing-base'); | ||
await giveawayItemPromise; | ||
|
||
//check if already entered | ||
let alreadyEntered = false; | ||
try{ | ||
await page.waitForSelector('.qa-giveaway-result-text', { timeout: 1000 }); | ||
alreadyEntered = true; | ||
} catch(error) { | ||
//nothing to do here... | ||
console.log('giveaway ' + i + ' is ready!'); | ||
} | ||
if (alreadyEntered) { | ||
console.log('giveaway ' + i + ' already entered.'); | ||
await navigateBackToGiveaways(page); | ||
return; | ||
} | ||
|
||
//try to win! | ||
await page.waitForSelector('#box_click_target'); | ||
await page.click('#box_click_target'); | ||
|
||
await navigateBackToGiveaways(page); | ||
} else { | ||
console.log('giveaway ' + i + ' requires entry.'); | ||
} | ||
}); | ||
|
||
//TODO: go to next page... | ||
|
||
await browser.close(); | ||
})(); |
Oops, something went wrong.