| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
# This program is free software; you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License as published by |
| 5 |
# the Free Software Foundation; either version 2 of the License, or |
| 6 |
# (at your option) any later version. |
| 7 |
# |
| 8 |
# This program is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
# GNU General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU General Public License |
| 14 |
# along with this program; if not, write to the Free Software |
| 15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 16 |
# 02110-1301, USA |
| 17 |
|
| 18 |
package Finance::Quote::CurrencyRates::AlphaVantage; |
| 19 |
|
| 20 |
use strict; |
| 21 |
use warnings; |
| 22 |
|
| 23 |
use constant DEBUG => $ENV{DEBUG}; |
| 24 |
use if DEBUG, 'Smart::Comments'; |
| 25 |
|
| 26 |
use JSON; |
| 27 |
|
| 28 |
our $VERSION = '1.71'; # VERSION |
| 29 |
|
| 30 |
sub parameters { |
| 31 |
return ('API_KEY'); |
| 32 |
} |
| 33 |
|
| 34 |
sub new |
| 35 |
{ |
| 36 |
my $self = shift; |
| 37 |
my $class = ref($self) || $self; |
| 38 |
|
| 39 |
my $this = {}; |
| 40 |
bless $this, $class; |
| 41 |
|
| 42 |
my $args = shift; |
| 43 |
|
| 44 |
### AlphaVantage->new args : $args |
| 45 |
|
| 46 |
# AlphaVantage is the ONLY module permitted to use an environment variable |
| 47 |
# for API key (for backwards compatibility). New modules MUST use the |
| 48 |
# API_KEY from args. |
| 49 |
|
| 50 |
$this->{API_KEY} = $ENV{'ALPHAVANTAGE_API_KEY'}; |
| 51 |
$this->{API_KEY} = $args->{API_KEY} if (ref $args eq 'HASH') and (exists $args->{API_KEY}); |
| 52 |
|
| 53 |
return $this; |
| 54 |
} |
| 55 |
|
| 56 |
sub multipliers |
| 57 |
{ |
| 58 |
my ($this, $ua, $from, $to) = @_; |
| 59 |
|
| 60 |
my $url = 'https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE'; |
| 61 |
my $try_cnt = 0; |
| 62 |
my $json_data; |
| 63 |
my $rate; |
| 64 |
do { |
| 65 |
$try_cnt += 1; |
| 66 |
# AlphaVantage is limiting free/basic api use to one query per second. |
| 67 |
# sleep 2 ensures a query for currency exchange is not immediately |
| 68 |
# following a price query. |
| 69 |
sleep 2; |
| 70 |
my $reply = $ua->get($url |
| 71 |
. '&from_currency=' . ${from} |
| 72 |
. '&to_currency=' . ${to} |
| 73 |
. '&apikey=' . $this->{API_KEY}); |
| 74 |
|
| 75 |
return unless ($reply->code == 200); |
| 76 |
|
| 77 |
my $body = $reply->content; |
| 78 |
|
| 79 |
$json_data = JSON::decode_json $body; |
| 80 |
if ( !$json_data || $json_data->{'Error Message'} ) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
### JSON: $json_data |
| 85 |
|
| 86 |
sleep (20) if (($try_cnt < 5) && ($json_data->{'Note'})); |
| 87 |
} while (($try_cnt < 5) && ($json_data->{'Note'})); |
| 88 |
|
| 89 |
if( !$json_data->{'Realtime Currency Exchange Rate'} ) { |
| 90 |
### No data in JSON |
| 91 |
$rate = 0.0; |
| 92 |
} else { |
| 93 |
$rate = |
| 94 |
$json_data->{'Realtime Currency Exchange Rate'}->{'5. Exchange Rate'}; |
| 95 |
} |
| 96 |
|
| 97 |
### Rate from JSON: $rate |
| 98 |
|
| 99 |
return unless $rate + 0; |
| 100 |
|
| 101 |
# For small rates, request the inverse |
| 102 |
if ($rate < 0.001) { |
| 103 |
### Rate is too small, requesting inverse : $rate |
| 104 |
my ($a, $b) = $this->multipliers($ua, $to, $from); |
| 105 |
return ($b, $a); |
| 106 |
} |
| 107 |
|
| 108 |
return (1.0, $rate); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
1; |
| 113 |
|
| 114 |
=head1 NAME |
| 115 |
|
| 116 |
Finance::Quote::CurrencyRates::AlphaVantage - Obtain currency rates from |
| 117 |
https://www.alphavantage.co |
| 118 |
|
| 119 |
=head1 SYNOPSIS |
| 120 |
|
| 121 |
use Finance::Quote; |
| 122 |
|
| 123 |
$q = Finance::Quote->new(currency_rates => {order => ['AlphaVantage'], |
| 124 |
alphavantage => {API_KEY => ...}}); |
| 125 |
|
| 126 |
$value = $q->currency('18.99 EUR', 'USD'); |
| 127 |
|
| 128 |
=head1 DESCRIPTION |
| 129 |
|
| 130 |
This module fetches currency rates from https://www.alphavantage.co and |
| 131 |
provides data to Finance::Quote to convert the first argument to the equivalent |
| 132 |
value in the currency indicated by the second argument. |
| 133 |
|
| 134 |
This module is the default currency conversion module for a Finance::Quote |
| 135 |
object. |
| 136 |
|
| 137 |
=head1 API_KEY |
| 138 |
|
| 139 |
https://www.alphavantage.co requires users to register and obtain an API key, |
| 140 |
which is also called a token. |
| 141 |
|
| 142 |
The API key may be set by either providing a alphavantage hash inside the |
| 143 |
currency_rates hash to Finance::Quote->new as in the above example, or by |
| 144 |
setting the environment variable ALPHAVANTAGE_API_KEY. |
| 145 |
|
| 146 |
=head1 Terms & Conditions |
| 147 |
|
| 148 |
Use of https://www.alphavantage.co is governed by any terms & conditions of |
| 149 |
that site. |
| 150 |
|
| 151 |
Finance::Quote is released under the GNU General Public License, version 2, |
| 152 |
which explicitly carries a "No Warranty" clause. |
| 153 |
|
| 154 |
=cut |