| 1 |
#!/usr/bin/perl -w |
| 2 |
# vi: set ts=2 sw=2 noai ic showmode showmatch: |
| 3 |
# |
| 4 |
# Copyright (C) 2024, Bruce Schuck <[email protected]> |
| 5 |
# |
| 6 |
# This program is free software; you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation; either version 2 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program; if not, write to the Free Software |
| 18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 19 |
# 02110-1301, USA |
| 20 |
|
| 21 |
# Changes: |
| 22 |
# Initial Version: 2024-08-31, Bruce Schuck |
| 23 |
|
| 24 |
package Finance::Quote::FinanceAPI; |
| 25 |
|
| 26 |
use strict; |
| 27 |
use warnings; |
| 28 |
|
| 29 |
use Encode qw(decode); |
| 30 |
use HTTP::Request::Common; |
| 31 |
use JSON qw( decode_json ); |
| 32 |
|
| 33 |
use constant DEBUG => $ENV{DEBUG}; |
| 34 |
use if DEBUG, 'Smart::Comments', '###'; |
| 35 |
|
| 36 |
our $VERSION = '1.71'; # VERSION |
| 37 |
|
| 38 |
my $FINANCEAPI_URL = 'https://yfapi.net/v6/finance/quote?region=US&lang=en&symbols='; |
| 39 |
|
| 40 |
# Change DISPLAY and method values in code below |
| 41 |
# Modify LABELS to those returned by the method |
| 42 |
|
| 43 |
our $DISPLAY = 'FinanceAPI'; |
| 44 |
our $FEATURES = {'API_KEY' => 'registered user API key'}; |
| 45 |
our @LABELS = qw/symbol name open high low last price date volume currency method/; |
| 46 |
our $METHODHASH = {subroutine => \&financeapi, |
| 47 |
display => $DISPLAY, |
| 48 |
labels => \@LABELS, |
| 49 |
features => $FEATURES}; |
| 50 |
|
| 51 |
sub methodinfo { |
| 52 |
return ( |
| 53 |
financeapi => $METHODHASH, |
| 54 |
nyse => $METHODHASH, |
| 55 |
nasdaq => $METHODHASH, |
| 56 |
usa => $METHODHASH, |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
sub labels { my %m = methodinfo(); return map {$_ => [@{$m{$_}{labels}}] } keys %m; } |
| 61 |
|
| 62 |
sub methods { |
| 63 |
my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m; |
| 64 |
} |
| 65 |
|
| 66 |
sub financeapi { |
| 67 |
|
| 68 |
my $quoter = shift; |
| 69 |
my @stocks = @_; |
| 70 |
my (%info, $url, $reply); |
| 71 |
my $ua = $quoter->user_agent(); |
| 72 |
|
| 73 |
# Set token. If not passed in as argument, get from environment. |
| 74 |
my $token = exists $quoter->{module_specific_data}->{stockdata}->{API_KEY} ? |
| 75 |
$quoter->{module_specific_data}->{stockdata}->{API_KEY} : |
| 76 |
$ENV{"FINANCEAPI_API_KEY"}; |
| 77 |
|
| 78 |
# Set headers. API key is sent as a header. |
| 79 |
my @ua_headers = ( |
| 80 |
'Accept' => 'application/json', |
| 81 |
'X-API-KEY' => $token, |
| 82 |
); |
| 83 |
|
| 84 |
foreach my $stock (@stocks) { |
| 85 |
|
| 86 |
if ( !defined $token ) { |
| 87 |
$info{ $stock, 'success' } = 0; |
| 88 |
$info{ $stock, 'errormsg' } = |
| 89 |
'FINANCEAPI_API_KEY not defined. Get an API key at https://financeapi.net/'; |
| 90 |
next; |
| 91 |
} |
| 92 |
|
| 93 |
$url = $FINANCEAPI_URL . $stock; |
| 94 |
$reply = $ua->get( $url, @ua_headers ); |
| 95 |
|
| 96 |
my $code = $reply->code; |
| 97 |
my $desc = HTTP::Status::status_message($code); |
| 98 |
my $headers = $reply->headers_as_string; |
| 99 |
my $body = $reply->content; |
| 100 |
|
| 101 |
### Body: $body |
| 102 |
|
| 103 |
my ($quote, $name, $bid, $ask, $last, $open, $high, $low, $volume, $currency, $date); |
| 104 |
my ($month, $day, $year); |
| 105 |
|
| 106 |
$info{ $stock, "symbol" } = $stock; |
| 107 |
|
| 108 |
if ( $code == 200 ) { |
| 109 |
|
| 110 |
eval {$quote = JSON::decode_json $body}; |
| 111 |
if ($@) { |
| 112 |
$info{ $stock, 'success' } = 0; |
| 113 |
$info{ $stock, 'errormsg' } = $@; |
| 114 |
next; |
| 115 |
} |
| 116 |
|
| 117 |
### [<now>] JSON quote: $quote |
| 118 |
|
| 119 |
if (!exists $quote->{'quoteResponse'}) { |
| 120 |
$info{ $stock, 'success' } = 0; |
| 121 |
$info{ $stock, 'errormsg' } = $@; |
| 122 |
next; |
| 123 |
} |
| 124 |
|
| 125 |
# JSON Result Array is sometimes empty |
| 126 |
my $json_data_count = scalar @{ $quote->{'quoteResponse'}{'result'} }; |
| 127 |
if ( $json_data_count < 1 ) { |
| 128 |
$info{ $stock, "success" } = 0; |
| 129 |
$info{ $stock, "errormsg" } = |
| 130 |
"Error retrieving quote for $stock - no listing for this name found. Please check symbol and the two letter extension (if any)"; |
| 131 |
next; |
| 132 |
} |
| 133 |
|
| 134 |
$name = $quote->{'quoteResponse'}{'result'}[0]{'longName'}; |
| 135 |
$open = $quote->{'quoteResponse'}{'result'}[0]{'regularMarketOpen'}; |
| 136 |
$high = $quote->{'quoteResponse'}{'result'}[0]{'regularMarketDayHigh'}; |
| 137 |
$low = $quote->{'quoteResponse'}{'result'}[0]{'regularMarketDayLow'}; |
| 138 |
$last = $quote->{'quoteResponse'}{'result'}[0]{'regularMarketPrice'}; |
| 139 |
$volume = $quote->{'quoteResponse'}{'result'}[0]{'regularMarketVolume'}; |
| 140 |
#$currency = $quote->{'quoteResponse'}{'result'}[0]{'financialCurrency'}; |
| 141 |
$currency = $quote->{'quoteResponse'}{'result'}[0]{'currency'}; |
| 142 |
$date = $quote->{'quoteResponse'}{'result'}[0]{'regularMarketTime'}; |
| 143 |
($month, $day, $year) = (localtime($date))[4,3,5]; |
| 144 |
$month++; |
| 145 |
$year += 1900; |
| 146 |
|
| 147 |
$info{$stock, 'name'} = $name; |
| 148 |
$info{$stock, 'open'} = $open; |
| 149 |
$info{$stock, 'high'} = $high; |
| 150 |
$info{$stock, 'low'} = $low; |
| 151 |
$info{$stock, 'last'} = $last; |
| 152 |
$info{$stock, 'price'} = $last; |
| 153 |
$info{$stock, 'volume'} = $volume; |
| 154 |
$info{$stock, 'currency'} = $currency; |
| 155 |
$info{ $stock, 'method' } = 'financeapi'; |
| 156 |
$quoter->store_date(\%info, $stock, {month => $month, day => $day, year => $year}); |
| 157 |
|
| 158 |
# Check for stocks traded in pence instead of pounds |
| 159 |
# Convert GBp or GBX to GBP (divide price by 100). |
| 160 |
# GBP.L - pence |
| 161 |
# GBPG.L - pounds |
| 162 |
|
| 163 |
if ( ($info{$stock,"currency"} eq "GBp") || |
| 164 |
($info{$stock,"currency"} eq "GBX")) { |
| 165 |
foreach my $field ( $quoter->default_currency_fields ) { |
| 166 |
next unless ( $info{ $stock, $field } ); |
| 167 |
$info{ $stock, $field } |
| 168 |
= $quoter->scale_field( $info{ $stock, $field }, 0.01 ); |
| 169 |
} |
| 170 |
$info{ $stock, "currency"} = "GBP"; |
| 171 |
} |
| 172 |
|
| 173 |
# Apply the same hack for Johannesburg Stock Exchange |
| 174 |
# (JSE) prices as they are returned in ZAc (cents) |
| 175 |
# instead of ZAR (rands). JSE symbols are suffixed |
| 176 |
# with ".JO" when querying Yahoo e.g. ANG.JO |
| 177 |
|
| 178 |
if ($info{$stock,"currency"} eq "ZAc") { |
| 179 |
foreach my $field ( $quoter->default_currency_fields ) { |
| 180 |
next unless ( $info{ $stock, $field } ); |
| 181 |
$info{ $stock, $field } |
| 182 |
= $quoter->scale_field( $info{ $stock, $field }, 0.01 ); |
| 183 |
} |
| 184 |
$info{ $stock, "currency"} = "ZAR"; |
| 185 |
} |
| 186 |
|
| 187 |
# Apply the same hack for Tel Aviv Stock Exchange |
| 188 |
# (TASE) prices as they are returned in ILA (Agorot) |
| 189 |
# instead of ILS (Shekels). TASE symbols are suffixed |
| 190 |
# with ".TA" when querying Yahoo e.g. POLI.TA |
| 191 |
|
| 192 |
if ($info{$stock,"currency"} eq "ILA") { |
| 193 |
foreach my $field ( $quoter->default_currency_fields ) { |
| 194 |
next unless ( $info{ $stock, $field } ); |
| 195 |
$info{ $stock, $field } |
| 196 |
= $quoter->scale_field( $info{ $stock, $field }, 0.01 ); |
| 197 |
} |
| 198 |
$info{ $stock, "currency"} = "ILS"; |
| 199 |
} |
| 200 |
|
| 201 |
$info{ $stock, 'success' } = 1; |
| 202 |
|
| 203 |
} else { # HTTP Request failed (code != 200) |
| 204 |
$info{ $stock, "success" } = 0; |
| 205 |
$info{ $stock, "errormsg" } = |
| 206 |
"Error retrieving quote for $stock. Attempt to fetch the URL $url resulted in HTTP response $code ($desc)"; |
| 207 |
} |
| 208 |
|
| 209 |
} |
| 210 |
|
| 211 |
return wantarray() ? %info : \%info; |
| 212 |
return \%info; |
| 213 |
|
| 214 |
} |
| 215 |
|
| 216 |
1; |
| 217 |
|
| 218 |
__END__ |
| 219 |
|
| 220 |
=head1 NAME |
| 221 |
|
| 222 |
Finance::Quote::FinanceAPI - Obtain quotes from financeapi.net. |
| 223 |
|
| 224 |
=head1 SYNOPSIS |
| 225 |
|
| 226 |
use Finance::Quote; |
| 227 |
|
| 228 |
# API Key passed during object creation |
| 229 |
$q = Finance::Quote->new('FinanceAPI', financeapi => {API_KEY => 'your-financeapi-api-key'}); |
| 230 |
|
| 231 |
# FINANCEAPI_API_KEY environment variable set |
| 232 |
$q = Finance::Quote->new; |
| 233 |
|
| 234 |
%info = $q->fetch("financeapi", "AAPL"); # Only query financeapi |
| 235 |
|
| 236 |
=head1 DESCRIPTION |
| 237 |
|
| 238 |
This module fetches information from L<https://financeapi.net/>. The API URL |
| 239 |
is https://yfapi.net/. |
| 240 |
|
| 241 |
This module is loaded by default on a Finance::Quote object. It's also possible |
| 242 |
to load it explicitly by placing "financeapi" in the argument list to |
| 243 |
Finance::Quote->new(). |
| 244 |
|
| 245 |
This module provides the "financeapi" fetch method. In addition it |
| 246 |
advertises "nyse", "usa", and "nasdaq". |
| 247 |
|
| 248 |
=head1 API_KEY |
| 249 |
|
| 250 |
L<https://financeapi.net/> requires users to register for an API Key |
| 251 |
(token). |
| 252 |
The free "Basic" API Key allows 100 queries per day and a 300 per minute |
| 253 |
rate. |
| 254 |
|
| 255 |
The API key may be set by either providing a module specific hash to |
| 256 |
Finance::Quote->new as in the above example, or by setting the environment |
| 257 |
variable FINANCEAPI_API_KEY. |
| 258 |
|
| 259 |
=head1 LABELS RETURNED |
| 260 |
|
| 261 |
The following labels are returned: |
| 262 |
|
| 263 |
=over |
| 264 |
|
| 265 |
=item name |
| 266 |
|
| 267 |
=item symbol |
| 268 |
|
| 269 |
=item open |
| 270 |
|
| 271 |
=item high |
| 272 |
|
| 273 |
=item low |
| 274 |
|
| 275 |
=item price |
| 276 |
|
| 277 |
=item date |
| 278 |
|
| 279 |
=item volume |
| 280 |
|
| 281 |
=item currency |
| 282 |
|
| 283 |
=back |
| 284 |
|
| 285 |
=cut |