A roaring went through the tech-savvy finance community when Yahoo suddenly shut down its Finance API on Nov 1, 2017. Dozens of libraries and scripts that had parsed HTTP output from this API for years ceased to work. No longer could libraries such as Finance::Quote query prices of stocks, funds and currencies and hand these over to other applications such as GnuCash. By using Finance::Quote—I even wrote an extension module for it—I was affected as well, and finally I stumbled upon a solution: Version 1.41 recently introduced an Alpha Vantage module, and querying this source instead of Yahoo is straightforward.
Unfortunately, using Debian or Ubuntu one has to install this version from source, but it's not that hard to do. And for using Alpha Vantage, you have to register for an API key, but that's also no issue. One of the things that still don't work is currency conversion, as that module is still tied to Yahoo's interface. Alpha Vantage does provide currency conversion through its API, though. Looking at AlphaVantage.pm I recreated a simple Perl logic to perform currency conversion. Note that I haven't packed it into a real Finance::Quote module, it's just a simple Perl script that Works For Me™. For simplicity, I don't do any error handling. It relies on an environment variable that contains the API key. Here's how I did it:
#!/usr/bin/perl -wuse lib
'/home/user/lib/perl';#not needed#use Finance::Quote;#debug#use Data::Dumper;use JSON
qw( decode_json
);use HTTP
::Request::Common;my $pair = $ARGV[0]; # e.g. "XAU:EUR" for gold price in Euro@curr = split( /:/, $pair );my $API_KEY = $ENV{'ALPHAVANTAGE_API_KEY'};$url = "https://www.alphavantage.co/query" . "?function=CURRENCY_EXCHANGE_RATE" . "&from_currency=" . $curr[0] . "&to_currency=" . $curr[1] . "&apikey=" . $API_KEY;$ua = LWP
::UserAgent->new;$reply = $ua->request(GET
$url);#debug#my $code = $reply->code;#debug#my $desc = HTTP::Status::status_message($code);my $body = $reply->content;my $json_data = JSON
::decode_json $body;#debug#print Dumper(\$json_data);my %fx = %{$json_data->{'Realtime Currency Exchange Rate'}};my $last_refresh = $fx{'6. Last Refreshed'};$last_refresh = substr($last_refresh, 0, 10); # remove timemy $isodate = substr($last_refresh, 0, 10);my $rate = $fx{'5. Exchange Rate'};print $isodate . ' ' . $rate . "\n";