Created
August 31, 2011 07:59
-
-
Save martincik/1183042 to your computer and use it in GitHub Desktop.
MBank.cz - Transactions harvester
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
require 'nokogiri' | |
require 'open-uri' | |
require 'mechanize' | |
require 'logger' | |
class MBankHarvester | |
URL = "https://cz.mbank.eu/" | |
def initialize(login, password, logfile = nil) | |
@login = login | |
@password = password | |
@logfile ||= File.join(File.dirname(__FILE__), '..', '..', 'log', 'mbank_cz_harvester.log') | |
end | |
def parse_data | |
start_agent | |
login | |
transactions = get_transactions | |
save_transactions(transactions.body) if transactions | |
logout | |
end | |
private | |
def start_agent | |
@agent = Mechanize.new { |a| a.log = Logger.new(@logfile) } | |
@agent.user_agent_alias = "Mac Safari" | |
end | |
def login | |
page = @agent.get(URL + 'logon.aspx') | |
login_form = page.forms.first | |
login_form.fields_with(:name => 'customer').first.value = @login | |
login_form.fields_with(:name => 'password').first.value = @password | |
@agent.submit(login_form) | |
end | |
def logout | |
page = @agent.get(URL + 'logout.aspx') | |
end | |
def get_transactions | |
page = @agent.get(URL + 'accounts_list.aspx') | |
page = click_on_link(page, "mKonto - normalni ucet 670100-2203892976/6210") | |
page = click_on_link(page, "Historie transakc\303\255") | |
form = page.forms_with(:name => 'MainForm').first | |
form.action = 'printout_oper_list.aspx' | |
today = Date.today | |
form.radiobuttons_with(:name => 'rangepanel_group', :value => 'daterange_radio').first.check | |
form.fields_with(:name => 'daterange_from_day').first.value = 1 | |
form.fields_with(:name => 'daterange_from_month').first.value = today.month | |
form.fields_with(:name => 'daterange_from_year').first.value = today.year | |
form.fields_with(:name => 'daterange_to_day').first.value = Date.civil(today.year, today.month, -1).day | |
form.fields_with(:name => 'daterange_to_month').first.value = today.month | |
form.fields_with(:name => 'daterange_to_year').first.value = today.year | |
form.fields_with(:name => 'lastdays_days').first.value = 1 | |
form.fields_with(:name => 'lastdays_period').first.value = 'M' | |
form.fields_with(:name => 'accoperlist_typefilter_group').first.value = 'ALL' | |
form.checkbox_with(:name => 'export_oper_history_check').checked = true | |
select_list = form.fields_with(:name => 'export_oper_history_format').first | |
select_list.value = 'CSV' | |
page = form.click_button | |
end | |
def save_transactions(transactions) | |
transaction_file = File.new("transakce.csv", "w") | |
transaction_file.write(transactions) | |
transaction_file.close | |
end | |
def click_on_link(page, name) | |
l = page.link_with(:text => name) | |
action, params = parse_ajax_from_link(l) | |
form = page.forms.first | |
form.fields_with(:name => '__PARAMETERS').first.value = params | |
form.action = action | |
@agent.submit(form) | |
end | |
def parse_ajax_from_link(ajax_string) | |
values = ajax_string.attributes['onclick'].sub('doSubmit(','').sub(/\)\;(.*)$/,'').gsub('\'','').split(',') | |
return values[0], values[3] | |
end | |
end | |
mbank_cz_harvester = MBankHarvester.new('login', 'password') | |
mbank_cz_harvester.parse_data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment