Sat, 23 Jun 07

Automatically download egg credit card transactions

I’ve followed in my previous footsteps and automated the download of my egg credit card transactions and statements. Using this in combination with my ofx convertor makes it easier for me to upload my financial data to wesabe.

As always, the code is in google code and pasted below for your viewing pleasure…

require 'egg-credentials'

def execute_curl(cmd)
  `#{cmd}`
end

COOKIE_LOCATION = "/tmp/egg.cookie"
STATEMENT_DATE = '18 January 2007'

RECENT_TRANSACTIONS_FILE = '/Users/chrisroos/Desktop/egg-recent-transactions.html'
STATEMENT_FILE = '/Users/chrisroos/Desktop/egg-statement.html'

# Logging in requires a cookie that is set when we first visit the login page
curl_cmd = %[curl -s -c"#{COOKIE_LOCATION}" "https://new.egg.com/security/customer/logon?URI=https://new.egg.com/customer/youraccounts"]
execute_curl curl_cmd

# POST our login credentials
curl_cmd = %[curl -s -b"#{COOKIE_LOCATION}" -c"#{COOKIE_LOCATION}" -L "https://logon.egg.com/LoginWebServer/services/CustomerLogin" -d"firstName=#{FIRST_NAME}" -d"lastName=#{LAST_NAME}" -d"dobDay=#{DOB_DAY}" -d"dobMonth=#{DOB_MONTH}" -d"dobYear=#{DOB_YEAR}" -d"postcode=#{POSTCODE}" -d"mmn=#{MOTHERS_MAIDEN_NAME}" -d"password=#{PASSWORD}" -d"HiddenURI=https%3A%2F%2Fnew.egg.com%2Fcustomer%2Fyouraccounts"]
execute_curl curl_cmd

# List and save recent transactions
curl_cmd = %[curl -s -o"#{RECENT_TRANSACTIONS_FILE}" -b"#{COOKIE_LOCATION}" "https://your.egg.com/customer/eggcard/recenttransactions.aspx"]
execute_curl curl_cmd

# There is no sensible way to get your statements.  I'd hope for something like /statements/18-jun-2007.html but no such luck
# Instead, there is a zero indexed array of statement dates.  Selecting one of these statement dates allows us to construct
# a uri using the index of the date in the list.  So, if 18 June 2007 is the first date in the list (i.e. index 0) then we can view that
# statement at https://your.egg.com/customer/eggcard/statements.aspx?index=0
# Interestingly, this is how it works from the recent transactions page, from any actual statement page, it appears that this is
# too hard (it's not) and instead the developers have resorted to using some magical .net/javascript voodoo to obtain a different
# statement.
require 'rubygems'
require 'hpricot'

recent_transactions = File.open(RECENT_TRANSACTIONS_FILE) { |f| f.read }
doc = Hpricot(recent_transactions)
statement_dates = []
(doc/'select#ctl01_content_recenttransactions_lstPreviousStatements/option').each { |e| statement_dates << e.inner_text }
statement_index = statement_dates.index(STATEMENT_DATE)

curl_cmd = %[curl -s -o"#{STATEMENT_FILE}" -b"#{COOKIE_LOCATION}" "https://your.egg.com/customer/eggcard/statements.aspx?index=#{statement_index}"]
execute_curl curl_cmd