| 1 |
#!/usr/bin/perl -w |
| 2 |
# vi: set ts=2 sw=2 ic noai showmode showmatch: |
| 3 |
|
| 4 |
# This program is free software; you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation; either version 2 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program; if not, write to the Free Software |
| 16 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 17 |
# 02110-1301, USA |
| 18 |
|
| 19 |
# Copyright (C) 2024, Bruce Schuck <[email protected]> |
| 20 |
|
| 21 |
# Changes: |
| 22 |
# 2024-09-22 - Initial version. Base code opied from |
| 23 |
# CurrencyRates/YahooJSON.pm |
| 24 |
|
| 25 |
package Finance::Quote::CurrencyRates::FinanceAPI; |
| 26 |
|
| 27 |
use strict; |
| 28 |
use warnings; |
| 29 |
|
| 30 |
use constant DEBUG => $ENV{DEBUG}; |
| 31 |
use if DEBUG, 'Smart::Comments'; |
| 32 |
|
| 33 |
use JSON; |
| 34 |
|
| 35 |
our $VERSION = '1.71'; # VERSION |
| 36 |
|
| 37 |
my $YIND_URL_HEAD = 'https://yfapi.net/v8/finance/chart/'; |
| 38 |
my $YIND_URL_TAIL = '?metrics=high&interval=1d&range=1d'; |
| 39 |
|
| 40 |
sub new |
| 41 |
{ |
| 42 |
my $self = shift; |
| 43 |
my $class = ref($self) || $self; |
| 44 |
|
| 45 |
my $this = {}; |
| 46 |
bless $this, $class; |
| 47 |
|
| 48 |
my $args = shift; |
| 49 |
|
| 50 |
### FinanceAPI->new args : $args |
| 51 |
|
| 52 |
# FinanceAPI is permitted to use an environment variable for API key |
| 53 |
# (for backwards compatibility). |
| 54 |
# New modules should use the API_KEY from args. |
| 55 |
|
| 56 |
$this->{API_KEY} = $ENV{'FINANCEAPI_API_KEY'}; |
| 57 |
$this->{API_KEY} = $args->{API_KEY} if (ref $args eq 'HASH') and (exists $args->{API_KEY}); |
| 58 |
|
| 59 |
return $this; |
| 60 |
} |
| 61 |
|
| 62 |
sub multipliers |
| 63 |
{ |
| 64 |
my ($this, $ua, $from, $to) = @_; |
| 65 |
|
| 66 |
my $json_data; |
| 67 |
my $rate; |
| 68 |
|
| 69 |
# Set headers. API key is sent as a header. |
| 70 |
my @ua_headers = ( |
| 71 |
'Accept' => 'application/json', |
| 72 |
'X-API-KEY' => $this->{API_KEY}, |
| 73 |
); |
| 74 |
|
| 75 |
my $reply = $ua->get($YIND_URL_HEAD |
| 76 |
. ${from} |
| 77 |
. ${to} |
| 78 |
. '%3DX' |
| 79 |
. $YIND_URL_TAIL, @ua_headers); |
| 80 |
|
| 81 |
### HTTP Status: $reply->code |
| 82 |
return unless ($reply->code == 200); |
| 83 |
|
| 84 |
my $body = $reply->content; |
| 85 |
|
| 86 |
$json_data = JSON::decode_json $body; |
| 87 |
|
| 88 |
### JSON: $json_data |
| 89 |
|
| 90 |
if ( !$json_data || !$json_data->{'chart'}->{'result'}->[0]->{'meta'}->{'regularMarketPrice'} ) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
$rate = |
| 95 |
$json_data->{'chart'}->{'result'}->[0]->{'meta'}->{'regularMarketPrice'}; |
| 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::FinanceAPI - Obtain currency rates from |
| 117 |
https://yfapi.net/v8/finance/chart/. |
| 118 |
|
| 119 |
=head1 SYNOPSIS |
| 120 |
|
| 121 |
use Finance::Quote; |
| 122 |
|
| 123 |
$q = Finance::Quote->new(currency_rates => |
| 124 |
{order => ['FinanceAPI'], |
| 125 |
financeapi => {API_KEY => ...} |
| 126 |
}); |
| 127 |
|
| 128 |
$value = $q->currency('18.99 EUR', 'USD'); |
| 129 |
|
| 130 |
=head1 DESCRIPTION |
| 131 |
|
| 132 |
This module fetches currency rates from |
| 133 |
https://yfapi.net/v8/finance/chart/ provides data to Finance::Quote |
| 134 |
to convert the first argument to the equivalent value in the currency |
| 135 |
indicated by the second argument. |
| 136 |
|
| 137 |
This module is not the default currency conversion module for a Finance::Quote |
| 138 |
object. |
| 139 |
|
| 140 |
=head1 API_KEY |
| 141 |
|
| 142 |
https://financeapi.net requires users to register and obtain an API key. |
| 143 |
|
| 144 |
The API key can be set by setting the Environment variable |
| 145 |
"FINANCEAPI_API_KEY" or providing a 'financeapi' hash inside the |
| 146 |
'currency_rates' hash to Finance::Quote->new as in the above example. |
| 147 |
|
| 148 |
=head1 Terms & Conditions |
| 149 |
|
| 150 |
Use of https://yfapi.net/v8/finance/chart/ is |
| 151 |
governed by any terms & conditions of that site. |
| 152 |
|
| 153 |
Finance::Quote is released under the GNU General Public License, version 2, |
| 154 |
which explicitly carries a "No Warranty" clause. |
| 155 |
|
| 156 |
=cut |