From 5a4be921fbd67f87a4dadd9d31088a0dfc10f51b Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Sun, 24 Apr 2022 13:38:26 -0400 Subject: [PATCH] Add a tool to import Bank of Canada exchange rates to Ledger. --- bocexchange-to-ledger.pl | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100755 bocexchange-to-ledger.pl diff --git a/bocexchange-to-ledger.pl b/bocexchange-to-ledger.pl new file mode 100755 index 0000000..c0afaf5 --- /dev/null +++ b/bocexchange-to-ledger.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl +# +# Copyright © 2022 Nick Bowler +# +# Simple script to help convert the CSV dumps of daily exchange rates from +# the Bank of Canada into ledger price databases. +# +# License WTFPL2: Do What The Fuck You Want To Public License, version 2. +# This is free software: you are free to do what the fuck you want to. +# There is NO WARRANTY, to the extent permitted by law. + +use strict; +use Text::CSV; + +my ($file) = (@ARGV, "/dev/stdin"); +my $csv = Text::CSV->new({ binary => 1 }); + +open my $fh, "<", $file or die "$file: $!"; + +while (<$fh>) { + last if /OBSERVATIONS/; +} +$csv->header($fh); + +while (my $row = $csv->getline_hr($fh)) { + next unless $row->{date} =~ /....-..-../; + + foreach (sort keys %$row) { + my $fx = uc $_; + if ($fx =~ /^FX([[:upper:]]{3})CAD$/) { + print "P $row->{date} $1 $row->{$_} CAD\n" + } + } + +} -- 2.43.2