Created
October 4, 2019 23:06
-
-
Save bentorkington/cb7cf5f11a44b4362d31a2716e32f147 to your computer and use it in GitHub Desktop.
Get historical BNZ Kiwisaver fund data as CSV
This file contains hidden or 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
#!/usr/bin/perl | |
use LWP::UserAgent; | |
use XML::LibXML; | |
use HTTP::Request; | |
use DateTime; | |
use Date::Parse; | |
use DateTime::Format::Strptime; | |
my $startDate = DateTime->new( | |
day => 1, | |
month => 1, | |
year => 2018, | |
); | |
my $stopDate = DateTime->new( | |
day => 27, | |
month => 2, | |
year => 2018, | |
); | |
my $dateFormat = DateTime::Format::Strptime->new( pattern => '%d %B %Y' ); | |
my $ua= LWP::UserAgent->new; | |
my $url = "https://www.bnz.co.nz/XMLFeed/portal/kiwisaver/xml?forDate="; | |
while ( $startDate->add(days => 1) < $stopDate) { | |
my $req = HTTP::Request->new( GET => $url . $startDate->ymd('-')); | |
my $response = $ua->request($req); | |
if ($response->code == 200) { | |
my $content = $response->decoded_content(); | |
my $dom = XML::LibXML->load_xml(string => $content); | |
my $dateString = $dom->findvalue('/rss/standard/indicativedate'); | |
my $date = $dateFormat->parse_datetime($dateString); | |
if ($date == $startDate) { | |
print ($date->ymd . ", "); | |
foreach my $fund ($dom->findnodes('/rss/standard/category/investment')) { | |
my $fundName = $fund->findvalue('./description'); | |
my ($fundPrice) = $fund->findvalue('./price') =~ /^\$ (.*)$/ ; | |
print "$fundPrice, "; | |
} | |
print "\n"; | |
} else { | |
#print $date->ymd . " was an holiday\n"; | |
} | |
} | |
else { | |
die "Got HTTP response " . $response->code . "\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment