Thursday, December 14. 2017Working around the Yahoo Finance API shutdown disasterA 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 -w
use 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 time my $isodate = substr($last_refresh, 0, 10); my $rate = $fx{'5. Exchange Rate'}; print $isodate . ' ' . $rate . "\n";
(Page 1 of 1, totaling 1 entries)
|
AboutCalendar
ArchivesCategoriesShow tagged entriesandroid antenna anti-spam apache astronomy austria automobile bash bluetooth bug career cloud collecting comic cooking cw debian dreams education electronics event fail fashion finance flickr fun gentoo geography german gnu-linux gnucash google google earth graphics guitar hardware history image processing internet kernel kids language lhc lifestyle linkroll literature ltd machine learning making mallorca mathematics matlab microsoft migration movies munich music nautilus numismatics octave pdf perl philately philosophy phone photo gear photography physics podcast politics postfix private programming public transport rant religion review samsung science security shtf social web software statistics storage sustainability symbian tablet time lapse transceiver tv usenet venice video virtualization wordplay work www yahoo youtube
Syndicate This BlogFollow meBookmarks
Powered by |