| 1 | # $Id: easybank.pm,v 1.7 2004/05/02 11:39:52 florian Exp $ | ||||||||||
| 2 | |||||||||||
| 3 | package Finance::Bank::easybank; | ||||||||||
| 4 | |||||||||||
| 5 | 1 | require 5.005_62; | |||||||||
| 6 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use WWW::Mechanize; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use HTML::TokeParser; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use constant { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | LOGIN_URL => 'https://ebanking.easybank.at//InternetBanking/InternetBanking?d=login&svc=EASYBANK&lang=de&ui=html', | |||||||||
| 14 | 1 | }; | |||||||||
| 15 | use Class::MethodMaker | ||||||||||
| 16 | 1 | ▶ | new_hash_init => 'new', | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | get_set => [ qw/user pass _agent/ ], | ||||||||||
| 18 | boolean => [ qw/return_floats _connected/ ], | ||||||||||
| 19 | 1 | list => [ qw/accounts entries/ ]; | |||||||||
| 20 | |||||||||||
| 21 | 1 | our $VERSION = '1.05'; | |||||||||
| 22 | |||||||||||
| 23 | |||||||||||
| 24 | # login into the online banking system. | ||||||||||
| 25 | # fail if either user or password isn't defined. | ||||||||||
| 26 | # | ||||||||||
| 27 | # XXX: catch login errors. | ||||||||||
| 28 | 0 | ▶ | sub _connect { | ||||||||
|
Subroutine
_connect: not called CC=7 SCAR=40.3
| |||||||||||
| 29 | 0 | my $self = shift; | |||||||||
| 30 | 0 | my $content; | |||||||||
| 31 | |||||||||||
| 32 | 0 | ▶ | croak "Need user to connect.\n" unless $self->user; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 0 | ▶ | croak "Need password to connect.\n" unless $self->pass; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | |||||||||||
| 35 | 0 | $self->_agent(WWW::Mechanize->new); | |||||||||
| 36 | 0 | $self->_agent->agent_alias('Mac Safari'); | |||||||||
| 37 | 0 | $self->_agent->get(LOGIN_URL); | |||||||||
| 38 | 0 | $self->_agent->form_number(1); | |||||||||
| 39 | 0 | $self->_agent->field('tn', $self->user); | |||||||||
| 40 | 0 | $self->_agent->field('pin', $self->pass); | |||||||||
| 41 | 0 | $self->_agent->click('Bsenden1'); | |||||||||
| 42 | |||||||||||
| 43 | 0 | $content = $self->_agent->content; | |||||||||
| 44 | 0 | ▶ | croak "The online banking system told me, that the user was not found.\n" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | if $content =~ /Der Verfüger ist nicht vorhanden/; | ||||||||||
| 46 | 0 | ▶ | croak "The online banking system told me, that the user or the password was invalid.\n" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | if $content =~ /Das Format der Verfügernummer oder des Passworts ist ungültig/; | ||||||||||
| 48 | 0 | ▶ | croak "The online banking system told me, that the password was invalid.\n" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | if $content =~ /Ihre PIN ist falsch/; | ||||||||||
| 50 | 0 | ▶ | croak "There was a system error - please consult the hotline.\n" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | if $content =~ /Es ist ein Systemfehler aufgetreten/; | ||||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | |||||||||||
| 55 | # fetches and parses the summary page for all given accounts. | ||||||||||
| 56 | # if no accounts have been defined, fetches and parses the summary | ||||||||||
| 57 | # displayed right after the login. | ||||||||||
| 58 | # | ||||||||||
| 59 | # returns a reference to a list of summary hashes. | ||||||||||
| 60 | 0 | ▶ | sub check_balance { | ||||||||
|
Subroutine
check_balance: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 61 | 0 | my $self = shift; | |||||||||
| 62 | 0 | my @accounts; | |||||||||
| 63 | |||||||||||
| 64 | # XXX: yeah, I'm lazy, but thats the easy way for a reset. | ||||||||||
| 65 | 0 | $self->_connect; | |||||||||
| 66 | |||||||||||
| 67 | 0 | ▶ | if($self->accounts_count > 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 0 | foreach my $account ($self->accounts) { | |||||||||
| 69 | 0 | $self->_select_account($account); | |||||||||
| 70 | 0 | push @accounts, $self->_parse_summary($self->_agent->content); | |||||||||
| 71 | } | ||||||||||
| 72 | } else { | ||||||||||
| 73 | 0 | push @accounts, $self->_parse_summary($self->_agent->content); | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | # return either a list with the accounts or a hashref | ||||||||||
| 77 | # with the accountno. as key. | ||||||||||
| 78 | return wantarray | ||||||||||
| 79 | ? @accounts | ||||||||||
| 80 | 0 | ▶ | : { map { $_->{account} => $_ } @accounts }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | |||||||||||
| 84 | # fetches and parses the first entries page for all given accounts. | ||||||||||
| 85 | # if no accounts have been defined, fetches and parses the first | ||||||||||
| 86 | # entries page of the account displayed right after the login. | ||||||||||
| 87 | # | ||||||||||
| 88 | # returns a reference to a list of entry hashes. | ||||||||||
| 89 | 0 | ▶ | sub get_entries { | ||||||||
|
Subroutine
get_entries: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 90 | 0 | my $self = shift; | |||||||||
| 91 | 0 | my %accounts; | |||||||||
| 92 | my $accountno; | ||||||||||
| 93 | 0 | my $entries; | |||||||||
| 94 | |||||||||||
| 95 | # XXX: yeah, I'm lazy, but thats the easy way for a reset. | ||||||||||
| 96 | 0 | $self->_connect; | |||||||||
| 97 | |||||||||||
| 98 | # go to the entries page. | ||||||||||
| 99 | 0 | $self->_agent->form_number(2); | |||||||||
| 100 | 0 | $self->_agent->click; | |||||||||
| 101 | |||||||||||
| 102 | 0 | ▶ | if($self->entries_count > 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 0 | foreach my $account ($self->entries) { | |||||||||
| 104 | 0 | $self->_select_account($account); | |||||||||
| 105 | |||||||||||
| 106 | 0 | ($accountno, $entries) = $self->_parse_entries($self->_agent->content); | |||||||||
| 107 | 0 | $accounts{$accountno} = $entries; | |||||||||
| 108 | } | ||||||||||
| 109 | } else { | ||||||||||
| 110 | 0 | ($accountno, $entries) = $self->_parse_entries($self->_agent->content); | |||||||||
| 111 | 0 | $accounts{$accountno} = $entries; | |||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | 0 | \%accounts; | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | |||||||||||
| 118 | # selects given account ($account). | ||||||||||
| 119 | 0 | ▶ | sub _select_account { | ||||||||
|
Subroutine
_select_account: not called CC=1 SCAR=6.9
| |||||||||||
| 120 | 0 | my($self, $account) = @_; | |||||||||
| 121 | |||||||||||
| 122 | 0 | $self->_agent->form_number(1); | |||||||||
| 123 | 0 | $self->_agent->field('selected-account', $account); | |||||||||
| 124 | 0 | $self->_agent->click; | |||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | |||||||||||
| 128 | # parses given html ($content) containing the last 0 - 20 entries of an | ||||||||||
| 129 | # account and returns a hashref containing the single entries. | ||||||||||
| 130 | 0 | ▶ | sub _parse_entries { | ||||||||
|
Subroutine
_parse_entries: not called CC=9 SCAR=45.0
| |||||||||||
| 131 | 0 | my ($self, $content) = @_; | |||||||||
| 132 | 0 | my $stream = HTML::TokeParser->new(\$content); | |||||||||
| 133 | 0 | my $accountno; | |||||||||
| 134 | my @data; | ||||||||||
| 135 | |||||||||||
| 136 | 0 | $stream->get_tag('table') for 1 .. 3; | |||||||||
| 137 | 0 | $stream->get_tag('tr') for 1 .. 3; | |||||||||
| 138 | 0 | $stream->get_tag('td') for 1 .. 2; | |||||||||
| 139 | |||||||||||
| 140 | 0 | $accountno = $stream->get_trimmed_text('/td'); | |||||||||
| 141 | |||||||||||
| 142 | 0 | $stream->get_tag('table') for 1 .. 2; | |||||||||
| 143 | 0 | $stream->get_tag('tr'); | |||||||||
| 144 | |||||||||||
| 145 | # ugh... | ||||||||||
| 146 | 0 | while(1) { | |||||||||
| 147 | 0 | my $nr; | |||||||||
| 148 | my %entry; | ||||||||||
| 149 | |||||||||||
| 150 | 0 | $stream->get_tag('tr'); | |||||||||
| 151 | 0 | $stream->get_tag('td'); | |||||||||
| 152 | |||||||||||
| 153 | 0 | $nr = $stream->get_trimmed_text('/td'); | |||||||||
| 154 | # end the loop if we find the first cell in a row which isn't a | ||||||||||
| 155 | # numeric value (should be the first after the entries-table). | ||||||||||
| 156 | 0 | ▶ | last unless $nr =~ /^\d+$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 157 | 0 | $entry{nr} = $nr; | |||||||||
| 158 | |||||||||||
| 159 | 0 | for(qw/date text/) { | |||||||||
| 160 | 0 | $stream->get_tag('td'); | |||||||||
| 161 | 0 | $entry{$_} = $stream->get_trimmed_text('/td'); | |||||||||
| 162 | } | ||||||||||
| 163 | |||||||||||
| 164 | 0 | $stream->get_tag('td'); | |||||||||
| 165 | |||||||||||
| 166 | 0 | for(qw/value currency amount/) { | |||||||||
| 167 | 0 | $stream->get_tag('td'); | |||||||||
| 168 | 0 | $entry{$_} = $stream->get_trimmed_text('/td'); | |||||||||
| 169 | } | ||||||||||
| 170 | |||||||||||
| 171 | $entry{amount} = $self->_scalar2float($entry{amount}) | ||||||||||
| 172 | 0 | ▶ | if $self->return_floats; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 173 | |||||||||||
| 174 | 0 | push @data, \%entry; | |||||||||
| 175 | } | ||||||||||
| 176 | |||||||||||
| 177 | 0 | ($accountno, \@data); | |||||||||
| 178 | } | ||||||||||
| 179 | |||||||||||
| 180 | |||||||||||
| 181 | # parses given html ($content) containing the summary of an account and | ||||||||||
| 182 | # returns a hashref containing the isolated data. | ||||||||||
| 183 | 0 | ▶ | sub _parse_summary { | ||||||||
|
Subroutine
_parse_summary: not called CC=7 SCAR=40.3
| |||||||||||
| 184 | 0 | my ($self, $content) = @_; | |||||||||
| 185 | 0 | my $stream = HTML::TokeParser->new(\$content); | |||||||||
| 186 | 0 | my %data; | |||||||||
| 187 | |||||||||||
| 188 | 0 | $stream->get_tag('table') for 1 .. 2; | |||||||||
| 189 | 0 | $stream->get_tag('td'); | |||||||||
| 190 | |||||||||||
| 191 | 0 | for(qw/bc account currency name date/) { | |||||||||
| 192 | 0 | $stream->get_tag('td'); | |||||||||
| 193 | 0 | $data{$_} = $stream->get_trimmed_text('/td'); | |||||||||
| 194 | } | ||||||||||
| 195 | |||||||||||
| 196 | 0 | $stream->get_tag('table') for 1 .. 2; | |||||||||
| 197 | 0 | $stream->get_tag('b'); | |||||||||
| 198 | 0 | $data{balance} = $stream->get_trimmed_text('/b'); | |||||||||
| 199 | 0 | $stream->get_tag('b') for 1 .. 2; | |||||||||
| 200 | 0 | $data{final} = $stream->get_trimmed_text('/b'); | |||||||||
| 201 | |||||||||||
| 202 | 0 | ▶ | if($self->return_floats) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 203 | 0 | $data{$_} = $self->_scalar2float($data{$_}) for qw/balance final/; | |||||||||
| 204 | } | ||||||||||
| 205 | |||||||||||
| 206 | 0 | \%data; | |||||||||
| 207 | } | ||||||||||
| 208 | |||||||||||
| 209 | |||||||||||
| 210 | # converts given scalar ($scalar) into a float and returns it. | ||||||||||
| 211 | 0 | ▶ | sub _scalar2float { | ||||||||
|
Subroutine
_scalar2float: not called CC=1 SCAR=6.9
| |||||||||||
| 212 | 0 | my($self, $scalar) = @_; | |||||||||
| 213 | |||||||||||
| 214 | 0 | $scalar =~ s/\.//g; | |||||||||
| 215 | 0 | $scalar =~ s/,/\./g; | |||||||||
| 216 | |||||||||||
| 217 | 0 | return $scalar; | |||||||||
| 218 | } | ||||||||||
| 219 | |||||||||||
| 220 | |||||||||||
| 221 | 1 | 1; | |||||||||