Created
June 3, 2014 00:07
-
-
Save pc-rgundlapalli/39e6befe9cfe56e11442 to your computer and use it in GitHub Desktop.
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
/* E*Trade Scrape | |
run on command line: casperjs etrade_scrape.js [USERNAME] [PASSWORD] [ACCT_LAST_4_DIGITS] | |
*/ | |
var casper = require('casper').create(); | |
var account_last_4 = casper.cli.raw.args[2]; | |
casper.userAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4'); | |
casper.start('https://us.etrade.com/home', function() { | |
// ERROR CHECK: | |
// User must enter USERNAME, PASSWORD, and ACCT_LAST_4_DIGITS parameters: | |
if(casper.cli.raw.args.length < 3) | |
this.echo("ERROR! You must enter USERNAME, PASSWORD, and ACCOUNT_LAST_4_DIGITS parameters", "ERROR").exit(); | |
// ERROR CHECK: | |
// See if login page loaded correctly | |
if(this.getTitle() != "Investing, Trading and Retirement - E*TRADE Financial") | |
this.echo("ERROR! Could not load login page", "ERROR").exit(); | |
else this.echo("Loaded login page: " + this.getTitle(), "INFO"); | |
// Fill in login form and submit | |
this.fill('#log-on-form', { "USER": casper.cli.raw.args[0], "PASSWORD": casper.cli.raw.args[1] }, true); | |
}); | |
// Once logged in, go to Statements page | |
casper.thenOpen('https://edoc.etrade.com/e/t/onlinedocs/docsearch?doc_type=stmt', function() { | |
// ERROR CHECK: | |
// See if Statements page loaded correctly | |
if(this.getTitle() != "E*TRADE FINANCIAL - Accounts") | |
this.echo("ERROR! Could not load Statements page", "ERROR").exit(); | |
else this.echo("Loaded Statements page: " + this.getTitle(), "INFO"); | |
// Select correct account from dropdown | |
this.echo("Selecting account ending in " + account_last_4, "INFO"); | |
// Select the correct account from the dropdown | |
var selectedAccount = this.evaluate(function(acct) { | |
jQuery("[name='currAcct'] option").filter(function() { | |
if($(this).val().substr(-4,4) == acct) | |
return $(this).val(); | |
}).attr('selected', true).trigger("change"); | |
return jQuery("[name='currAcct']").val(); | |
}, account_last_4); | |
// Ensure that correct account was selected | |
if(selectedAccount.substr(-4,4) != account_last_4) | |
this.echo("ERROR! Could not find account ending in " + account_last_4, "ERROR").exit(); | |
else this.echo("Selected account " + selectedAccount, "INFO"); | |
}); | |
var statementStream; | |
casper.then(function() { | |
// Download first (most current) statement in the list | |
var url = 'https://edoc.etrade.com/e/t/onlinedocs/doc?docType=stmt&docId=0'; | |
statementStream = this.base64encode(url); | |
//this.download(url, 'E*Trade_acct' + account_last_4 + '.pdf'); | |
}); | |
casper.run(function() { | |
this.echo(statementStream).exit(); | |
//this.echo('Done', "GREEN_BAR").exit(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment