Created
December 8, 2015 07:05
-
-
Save billpull/c8278fdf367455f4eb91 to your computer and use it in GitHub Desktop.
nightmarescript.js
This file contains hidden or 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
#!/usr/bin/env node | |
var fs = require('fs'); | |
var path = require('path'); | |
var log = require('verbalize'); | |
var yaml = require('js-yaml'); | |
var Nightmare = require('nightmare'); | |
var Promise = require('q').Promise; | |
var argv = require('minimist')(process.argv.slice(2)); | |
// Verbalize `runner` | |
log.runner = 'hype-bot'; | |
var config = yaml.safeLoad(fs.readFileSync('./bin/config.yml', 'utf8')); | |
var stores = yaml.safeLoad(fs.readFileSync('./bin/stores.yml', 'utf8')); | |
var product = argv._[0] || argv.p || argv.product; | |
var size = argv._[1] || argv.s || argv.size; | |
var storeArg = argv._[2] || argv.st || argv.store || 'supreme'; | |
var store = stores[storeArg]; | |
if (!product) { | |
log.error('Please provide a product name with `-p`'); | |
} | |
/** | |
* Application | |
*/ | |
var productHref = ''; | |
var productSizeOption = null; | |
var nightmare = Nightmare({show: true}); | |
function fillOutBillingDetails() { | |
Promise.resolve(nightmare | |
.evaluate(function (config) { | |
$("#credit_card_type").val(config.cardType) | |
$("#cnb").val(config.carNumber) | |
$("#vval").val(config.cvv) | |
$("#credit_card_month").val(config.expMonth) | |
$("#credit_card_year").val(config.expYear) | |
$("#order_terms").siblings('.iCheck-helper').click(); | |
$("input[value='process payment']").click(); | |
}, config) | |
).then(function (isSuccess) { | |
log.info(product + ' Purchased!!!'); | |
}); | |
} | |
function fillOutUserDetails() { | |
Promise.resolve(nightmare | |
.evaluate(function (config) { | |
$("#order_billing_name").val(config.name) | |
$("#order_email").val(config.email) | |
$("#order_tel").val(config.tel) | |
$("#bo").val(config.address) | |
$("#order_billing_zip").val(config.zip) | |
$("#order_billing_city").val(config.city) | |
$("#order_billing_state").val(config.state) | |
$("#order_billing_country").val(config.country) | |
}, config) | |
).then(function () { | |
fillOutBillingDetails(); | |
}); | |
} | |
function checkoutProduct(sizeValue) { | |
Promise.resolve(nightmare | |
.select(store.sizeSelector, sizeValue) | |
.click(store.addToCartSelector) | |
.click(store.goToCartSelector) | |
).then(function () { | |
fillOutUserDetails() | |
}); | |
} | |
function selectProductSize() { | |
Promise.resolve(nightmare | |
.evaluate(function (size, sizeSelector) { | |
return $(sizeSelector + ' option') | |
.filter(function () { return $(this).html() == size; }) | |
.val(); | |
}, size, store.sizeSelector, function (sizeValue) { | |
return sizeValue | |
}) | |
).then(function (sizeValue) { | |
if (sizeValue) { | |
log.info('Size ' + size + ' is available'); | |
checkoutProduct(sizeValue); | |
} else { | |
log.error('Size ' + size + ' is not available'); | |
} | |
}); | |
} | |
function loadProductPage(productUrl) { | |
var fullProductUrl = store.url + productUrl; | |
log.info('Loading: ' + fullProductUrl); | |
Promise.resolve(nightmare | |
.goto(fullProductUrl) | |
.evaluate(function (soldOutSelector) { | |
return $(soldOutSelector).length > 0; | |
}, store.soldOutSelector, function (isSoldOut) { | |
return isSoldOut; | |
}) | |
).then(function (isSoldOut) { | |
if (isSoldOut) { | |
log.error('Product ' + product + ' is sold out'); | |
} else { | |
selectProductSize(); | |
} | |
}); | |
} | |
function loadAllProductsPage() { | |
var productImgSelector = 'img[alt="' + product + '"]'; | |
log.info('Loading: ' + store.productsUrl); | |
Promise.resolve(nightmare | |
.refresh() | |
.exists(productImgSelector) | |
.evaluate(function (productImgSelector) { | |
var productImages = $(productImgSelector); | |
if (productImages && productImages.length) { | |
var productImg = productImages[0]; | |
return $(productImg).parent('a').attr('href'); | |
} | |
return ''; | |
}, productImgSelector, function (url) { | |
return url; | |
}) | |
).then(function (productUrl) { | |
log.info('Product ' + product + ' Exists'); | |
loadProductPage(productUrl); | |
}); | |
} | |
Promise.resolve(nightmare | |
.goto(store.productsUrl) | |
.evaluate(function () { | |
var cookies = document.cookie.split(';'); | |
for (var i = 0; i < cookies.length; i++) { | |
var cookie = cookies[i]; | |
var eqPos = cookie.indexOf("="); | |
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie; | |
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT"; | |
} | |
return document.cookie; | |
}) | |
).then(function () { | |
loadAllProductsPage(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment