Finance::Bank::HDFC and Template::Extract

April 17th, 2008 by Rohan

I released Finance::Bank::HDFC version 0.14 recently. The latest release has the all important get_mini_statement() method implemented. Yes! Check your mini account statement (last 20 transactions) from the command line!

guitar.png

The Template::Extract module from CPAN was extremely helpful here. The account statements were available as JavaScript code in the source HTML file.


	dattxn[l_count] = '10 Apr 2008';
	txndesc[l_count] = "Description blah blah";
	refchqnbr[l_count] = '000003009168';
	datvalue[l_count] = '11 Apr 2008';
	amttxn[l_count] = '0.01';
	balaftertxn[l_count] = '999999.99';
	coddrcr[l_count] = 'D';
	l_count ++;

	dattxn[l_count] = '01 Apr 2008';
	txndesc[l_count] = "A millionaire?";
	refchqnbr[l_count] = '037103902179';
	datvalue[l_count] = '01 Apr 2008';
	amttxn[l_count] = '1000000.00';
	balaftertxn[l_count] = '1000000.00';
	coddrcr[l_count] = 'C';
	l_count ++;

Looks just like a template right? Correct. Enter Template::Extract.

Template::Extract - Use TT2 syntax to extract data from documents

This is the template code I used


# template for extracting mini statements
Readonly my $TEMPLATE_MINI_STATEMENT => <<'EOF';
[% FOREACH record %]
    [% ... %]dattxn[l_count] = '[% date_transaction %]';
    [% ... %]txndesc[l_count] = "[% description %]";
    [% ... %]refchqnbr[l_count] = '[% ref_chq_num %]';
    [% ... %]datvalue[l_count] = '[% date_value %]';
    [% ... %]amttxn[l_count] = '[% amount %]';
    [% ... %]balaftertxn[l_count] = '[% balance %]';
    [% ... %]coddrcr[l_count] = '[% type %]';
[% END %]
EOF

After using the extract() method:

my $template = Template::Extract->new;
my $ref = $template->extract($TEMPLATE_MINI_STATEMENT, $response->content);

This is the resulting arrayref. Use it the way you want.


$ref = [
          {
            'amount' => '0.01',
            'balance' => '999999.99',
            'ref_chq_num' => '000003009168',
            'date_transaction' => '10 Apr 2008',
            'type' => 'D',
            'date_value' => '11 Apr 2008',
            'description' => 'Description blah blah'
          },
          {
            'amount' => '1000000.00',
            'balance' => '1000000.00',
            'ref_chq_num' => '037103902179',
            'date_transaction' => '01 Apr 2008',
            'type' => 'C',
            'date_value' => '01 Apr 2008',
            'description' => 'A millionaire?'
          }
        ];

For example:


foreach my $stmt_ref (@$ref) {
      print  "Amount: " . $stmt_ref->{amount} . "\n";
      print  "Balance: " . $stmt_ref->{balance} . "\n";
}

[Post to Twitter] Tweet This Post  [Post to Facebook] Facebook 

4 Responses to “Finance::Bank::HDFC and Template::Extract”

  1. AJay Says:

    Wow! Just what I was looking for! Thanks.
    Do you plan to implement buy and sell also? If not, I can try.
    Just let me know how to build the request content. Do you do it by seeing the url when doing the transaction or by snooping on the packet sent out or do you have some documentation? Forgive my innocence but this is really not my domain :)
    Thanks
    A

  2. AJay Says:

    My bad :(
    I thought this was for HDFC securities and hence the comment on the buy and sell
    My query still remains on how I can do it for say, HDFC securities.
    Thanks
    Ajay

  3. Rohan Says:

    Hi Ajay,
    This only works for netbanking and not HDFC securities. Securities is an entirely different website and will require new code.
    And no, there is no snooping of any kind going on :) Its just about understanding how HTTP servers and browsers send and receive requests.

  4. Mukesh Says:

    Hi ROhan,

    THis HDFC bank parser is very good. I liked it.
    By the way, I have a question in general about such parser.
    If I have website, where in we have a javascript dynamically generating the login form, then how will we parse such a webpage?
    Thanks,
    MR.

Leave a Reply




Tweet This Post links powered by Tweet This v1.4.1, a WordPress plugin for Twitter.