#!/usr/bin/perl
# workaround for dzil
BEGIN {
$download_mandiri::VERSION = '0.18';
}
use 5.010;
use strict;
use Cwd;
use Data::Rmap qw(:all);
use YAML;
# we don't use required=>1 because App::Options still works in BEGIN phase and
# thus breaks perl -c.
option => {
profile => { type => 'string', default => 'default' },
account => { type => 'string', default => "" },
username => { type => 'string' }, # req
password => { type => 'string' }, # req
data_dir => { type => 'string', # req
default => File::HomeDir->my_home . "/mandiri" },
log_dir => { type => 'string', # req
default => File::HomeDir->my_home . "/mandiri/logs" },
days => { type => 'string', required => 0, default => 31 },
},
);
use vars qw($Log_Level);
BEGIN { $Log_Level = "debug" }
use Log::Any::App '$log',
-category_level => {Dumps=>'off'},
-file => {
path => "$App::options{log_dir}/main.log",
},
-dir => {
path => "$App::options{log_dir}/dumps",
level => 'off',
# currently we always dump, Log::Any::App > 0.23 will support specifying
# sub { ... } to refer to general level
category_level => {Dumps => 'trace'},
};
for (qw/username password data_dir log_dir/) {
defined($App::options{$_}) or die "Missing required argument: $_\n";
}
mkpath($App::options{data_dir}) unless (-d $App::options{data_dir});
die "Can't create data_dir `$App::options{data_dir}'" unless (-d $App::options{data_dir});
my $ibank = Finance::Bank::ID::Mandiri->new(
username => $App::options{username},
password => $App::options{password},
logger => $log,
logger_dump => Log::Any->get_logger(category => "Dumps"),
verify_https => 1,
);
eval {
my $bal = $ibank->check_balance;
$log->debug("Balance: ".$bal);
my $stmt = $ibank->get_statement(
account => $App::options{account},
days => $App::options{days},
);
my $filename = sprintf "%s/mandiri.statement.%s.%s.to.%s.yaml",
$App::options{data_dir},
$stmt->{account},
$stmt->{start_date}->ymd,
$stmt->{end_date}->ymd;
$log->info("Writing statements to YAML file `$filename' ...");
# insert balance to statement (since mandiri statement doesn't contain any
# balance information)
$stmt->{_balance} = $bal;
# stringify DateTime objects so dump becomes simpler and shorter, the
# $_[0]{seen} = {} is a trick to allow multiple places which mention the same
# object to be converted (defeat circular checking)
rmap_ref { $_[0]{seen} = {}; $_ = $_->ymd if UNIVERSAL::isa($_, "DateTime") } $stmt;
write_file($filename, Dump($stmt));
};
if ($@) {
$log->error("die: $@");
}
# no matter what, try to logout so we're not locked out for 10 minutes, annoying
eval { $ibank->logout; };
$log->info("End session");
=pod
=head1 NAME
download_mandiri
=head1 VERSION
version 0.18
=head1 SYNOPSIS
First, put your username and password in ~/.app/download-mandiri.conf, e.g.:
[ALL]
username = ABCDEF0123
password = 123456
Then:
$ download-mandiri
Get your statements in data dir (defaults to ~/mandiri/). See logs in your log
dir (defaults to ~/mandiri/logs).
=head1 DESCRIPTION
This is a command-line script which you can run from cron or whatever to
conveniently download Bank Mandiri statements. By default, it downloads 31-day's
worth of statements to ~/mandiri/. To change this, use --days and --data_dir.
=head1 SEE ALSO
L<Finance::Bank::ID::Mandiri>
=head1 AUTHOR
Steven Haryanto <stevenharyanto@gmail.com>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2011 by Steven Haryanto.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
__END__