OFX hack for using the historical forex rates to fix csv files with a conversion rate
#!/usr/bin/perl | |
=begin comment | |
Go here: https://www.ofx.com/en-us/forex-news/historical-exchange-rates/ | |
open developer tools in Chrome | |
paste in console; | |
var jqry = document.createElement('script'); | |
jqry.src = "https://code.jquery.com/jquery-3.3.1.min.js"; | |
document.getElementsByTagName('head')[0].appendChild(jqry); | |
jQuery.noConflict(); | |
filter whatever currency you want/need and run ; | |
$(".historical-rates--table--results .ng-scope td").text() | |
copy the text to a file, aka | |
GBPHKD.txt | |
and run this script over it | |
=cut | |
$months = { "January"=>1, "February"=>2, "March"=>3, "April"=>4, "May"=>5, "June"=>6, "July"=>7, "August"=>8, "September"=>9, | |
"October"=>10, "November"=>11, "December"=>12 }; | |
$convrate = {}; | |
while(<>) { | |
chomp; | |
while(/([A-Za-z]+?)\ (\d+?)\,\ \d\d(\d\d)([0-9\.]*)/igsm) { | |
$fdate = sprintf("%02d",$2)."/".sprintf("%02d",$months->{$1})."/".sprintf("%02d",$3); | |
$convrate->{$fdate} = $4; | |
} | |
} | |
open(F, "<MT940_A_20191205_092821.csv"); | |
$skipfirst = 0; | |
while(<F>) { | |
chomp; | |
if (!$skipfirst) { # skip header | |
print $_."convrate\n"; | |
$skipfirst = 1; | |
next; | |
} | |
/.*?\;.*?\;(.*?)\;.*/; | |
if (!$convrate->{$1}) { | |
print "Cannot find rate for ".$1."\n"; | |
exit; | |
} | |
print $_.$convrate->{$1}."\n"; | |
} | |
close F; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment