Created
September 13, 2020 10:38
-
-
Save tluyben/70f57ff7fded7dab2ee0417d4b4a395e to your computer and use it in GitHub Desktop.
OFX hack for using the historical forex rates to fix csv files with a conversion rate
This file contains 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 | |
=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