diff options
author | Yaohan Chen <yaohan.chen@gmail.com> | 2014-04-01 01:21:59 -0400 |
---|---|---|
committer | Yaohan Chen <yaohan.chen@gmail.com> | 2014-04-01 01:31:53 -0400 |
commit | 23fcf1acc62e0f32f479c12c1916b2b1dd922199 (patch) | |
tree | 96ddaf09bb3cb901a9d9a0300bf2c14a37586de9 /util.coffee | |
parent | bb224302e1720bf7119c5b3a2e3c394ce989ecc7 (diff) | |
download | hib-dlagent-23fcf1acc62e0f32f479c12c1916b2b1dd922199.tar.gz hib-dlagent-23fcf1acc62e0f32f479c12c1916b2b1dd922199.zip |
Support the new Humble Bundle page
Use PhantomJS to handle the dynamically generated page and captchas.
Add options to specify the location of PhantomJS scripts and
configuration file.
Update README on requirements, usage, and debugging information.
Update CHANGELOG.
Diffstat (limited to 'util.coffee')
-rw-r--r-- | util.coffee | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/util.coffee b/util.coffee new file mode 100644 index 0000000..169cc35 --- /dev/null +++ b/util.coffee @@ -0,0 +1,71 @@ +system = require('system') + +# prints a log message if the LOG environment variable is set +if system.env.LOG? + exports.log = log = system.stderr.writeLine +else + exports.log = log = -> + +# prints a message to stderr, reads a line of input, and returns the input +exports.ask = ask = (message) -> + system.stderr.write message + system.stdin.readLine() + +# a page that directs its console messages to exportss.log +exports.page = page = require('webpage').create() +page.onConsoleMessage = log + +child_process = require('child_process') + +# displays a screenshot of the page, and returns the 'display' process object +exports.display_screenshot = display_screenshot = -> + # FIXME use mktemp, or write to display process directly + screenshot = '/tmp/hib-dlagent-phantomjs.png' + page.render screenshot + child_process.spawn 'display', [screenshot] + +# handles login/captcha boxes, and calls the passed action() when logged in +exports.handle_login_captcha = handle_login_captcha = (action, username, password) -> + need_to_submit = false + + # complete a login form if there is one + if page.evaluate(-> document.querySelector 'input[name="username"]') + log 'Entering login information...' + page.evaluate (username, password) -> + username_box = document.querySelector 'input[name="username"]' + password_box = document.querySelector 'input[name="password"]' + if username_box + username_box.value = username + if password_box + password_box.value = password + , username, password + need_to_submit = true + + # handle a captcha box if there is one + if page.evaluate(-> document.querySelector '#recaptcha_response_field') + log 'Humble Bundle wants you to solve a captcha. Displaying screenshot...' + display_process = display_screenshot() + input = ask 'Enter the captcha solution, or press Enter to get a new challenge: ' + display_process.kill() + + page.evaluate (input)-> + if input is '' + Recaptcha.reload() + else + captcha_box = document.querySelector '#recaptcha_response_field' + captcha_box.value = input + , input + need_to_submit = true + + if need_to_submit + # Entered information, submit and check for captcha/login again after load finishes + log 'Submitting login information and/or captcha response...' + page.onLoadFinished = -> handle_login_captcha action, username, password + page.evaluate -> + form = document.querySelector('form') + if form + form.submit() + else + log 'Logged in...' + action() + |