]> git.draconx.ca Git - scripts.git/commitdiff
Add a tool to import Bank of Canada exchange rates to Ledger.
authorNick Bowler <nbowler@draconx.ca>
Sun, 24 Apr 2022 17:38:26 +0000 (13:38 -0400)
committerNick Bowler <nbowler@draconx.ca>
Sun, 24 Apr 2022 17:38:26 +0000 (13:38 -0400)
bocexchange-to-ledger.pl [new file with mode: 0755]

diff --git a/bocexchange-to-ledger.pl b/bocexchange-to-ledger.pl
new file mode 100755 (executable)
index 0000000..c0afaf5
--- /dev/null
@@ -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"
+               }
+       }
+
+}