Created
November 1, 2012 03:19
-
-
Save waterlou/3991423 to your computer and use it in GitHub Desktop.
iPad Mini Hong Kong lucky draw automation
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
### | |
Usage: phantomjs ipadreserve.coffee "First Name" "Last Name" "email address" "ID Card number" "product code" "store code" | |
product code: | |
"MD528ZP/A" : 16G Black Wifi | |
"MD531ZP/A" : 16G White Wifi | |
"MD529ZP/A" : 32G Black Wifi | |
"MD532ZP/A" : 32G White Wifi | |
"MD530ZP/A" : 64G Black Wifi | |
"MD533ZP/A" : 64G White Wifi | |
store code: | |
"R428" : IFC | |
"R485" : Festival Walk | |
### | |
page = require('webpage').create() | |
system = require('system') | |
phantom.settings = | |
userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/534.55.3 (KHTML, like Gecko) Version/5.1.3 Safari/534.53.10" | |
page.state = 0 | |
url = "https://reserve.apple.com/HK/en_HK/reserve/iPad" | |
page.onAlert = (msg) -> | |
console.log "alert> #{msg}" | |
page.onConfirm = (msg) -> | |
console.log "confirm> #{msg}" | |
return true | |
page.onPrompt = (msg) -> | |
console.log "prompt> #{msg}" | |
page.onConsoleMessage = (msg) -> | |
console.log "console> #{msg}" | |
page.onError = (msg, trace) -> | |
console.log(msg) | |
for item in trace | |
console.log "#{item.file} : #{item.line}" | |
page.onResourceRequested = (res) -> | |
console.log "requesting> #{JSON.stringify(res)}" if false | |
page.onResourceReceived = (res) -> | |
console.log "requested> #{JSON.stringify(res)}" if false | |
page.onUrlChanged = (res) -> | |
console.log "urlChanged> #{JSON.stringify(res)}" if true | |
++page.state | |
firstname = system.args[1] | |
lastname = system.args[2] | |
email = system.args[3] | |
govid = system.args[4] | |
model = system.args[5] | |
storecode = system.args[6] | |
retValue = 0 # by default return 0 | |
console.log "data: #{firstname}, #{lastname}, #{email}, #{govid}, #{model}, #{storecode}" | |
page.onLoadFinished = (status) -> | |
console.log "status: #{status}" | |
if status != 'success' | |
console.log "Connection failed." | |
phantom.exit() | |
return | |
# no need to reload jquery for apple site | |
#page.injectJs 'jquery-1.8.2.min.js' | |
if page.state==1 | |
# select store and product | |
page.evaluate (model, storecode) -> | |
jQuery("#store").val(storecode).change() | |
console.log "store selected" | |
filterstring = '[value="' + model + '"]' | |
jQuery('input[name = product]').filter(filterstring).attr('checked', true).click() | |
console.log "product selected" | |
, model, storecode | |
# enter all other info and wait for url change | |
page.evaluate (firstname, lastname, email, govid)-> | |
jQuery("#quantity").val("2").change() | |
jQuery('#firstname').val(firstname) | |
jQuery('#lastname').val(lastname) | |
jQuery('#email').val(email) | |
jQuery('#govid').val(govid) | |
jQuery('#submitButton a').click() | |
, firstname, lastname, email, govid | |
if page.state==2 | |
page.render('apple.png') # save last screen | |
console.log "done" | |
phantom.exit(retValue) | |
page.open url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment