| 1 |
#!/usr/bin/perl -w |
| 2 |
# vi: set ts=2 sw=2 noai ic showmode showmatch: |
| 3 |
# |
| 4 |
# Copyright (C) 2023, 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 |
|
| 22 |
package Finance::Quote::StockData; |
| 23 |
|
| 24 |
use strict; |
| 25 |
use warnings; |
| 26 |
|
| 27 |
use Encode qw(decode); |
| 28 |
use HTTP::Request::Common; |
| 29 |
use JSON qw(decode_json); |
| 30 |
|
| 31 |
use constant DEBUG => $ENV{DEBUG}; |
| 32 |
use if DEBUG, 'Smart::Comments', '###'; |
| 33 |
|
| 34 |
our $VERSION = '1.71'; # VERSION |
| 35 |
|
| 36 |
my $STOCKDATA_URL = 'https://api.stockdata.org/v1/data/quote?symbols='; |
| 37 |
# Gets appended with '$stock&api_token=$token' |
| 38 |
|
| 39 |
# my $user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'; |
| 40 |
my $user_agent = 'Finance-Quote OpenSource Stock Quote Tool'; |
| 41 |
|
| 42 |
our $DISPLAY = 'StockData'; |
| 43 |
our $FEATURES = {'API_KEY' => 'registered user API key'}; |
| 44 |
our @LABELS = qw/symbol name open high low last date volume currency method/; |
| 45 |
our $METHODHASH = {subroutine => \&stockdata, |
| 46 |
display => $DISPLAY, |
| 47 |
labels => \@LABELS, |
| 48 |
features => $FEATURES}; |
| 49 |
|
| 50 |
sub methodinfo { |
| 51 |
return ( |
| 52 |
stockdata => $METHODHASH, |
| 53 |
nyse => $METHODHASH, |
| 54 |
nasdaq => $METHODHASH, |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
sub labels { my %m = methodinfo(); return map {$_ => [@{$m{$_}{labels}}] } keys %m; } |
| 59 |
|
| 60 |
sub methods { |
| 61 |
my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m; |
| 62 |
} |
| 63 |
|
| 64 |
#sub methods { |
| 65 |
# return (stockdata => \&stockdata, |
| 66 |
# nyse => \&stockdata, |
| 67 |
# nasdaq => \&stockdata); |
| 68 |
#} |
| 69 |
|
| 70 |
# our @labels = qw/symbol name open high low last date volume currency method/; |
| 71 |
|
| 72 |
#sub labels { |
| 73 |
# return (stockdata => \@labels, |
| 74 |
# nyse => \@labels, |
| 75 |
# nasdaq => \@labels); |
| 76 |
#} |
| 77 |
|
| 78 |
sub stockdata { |
| 79 |
|
| 80 |
my $quoter = shift; |
| 81 |
my @stocks = @_; |
| 82 |
my (%info, $url, $reply); |
| 83 |
my $ua = $quoter->user_agent(); |
| 84 |
$ua->agent($user_agent); |
| 85 |
|
| 86 |
my $token = exists $quoter->{module_specific_data}->{stockdata}->{API_KEY} ? |
| 87 |
$quoter->{module_specific_data}->{stockdata}->{API_KEY} : |
| 88 |
$ENV{"STOCKDATA_API_KEY"}; |
| 89 |
|
| 90 |
foreach my $stock (@stocks) { |
| 91 |
|
| 92 |
if ( !defined $token ) { |
| 93 |
$info{ $stock, 'success' } = 0; |
| 94 |
$info{ $stock, 'errormsg' } = |
| 95 |
'StockData API_KEY not defined. Get an API key at https://stockdata.org'; |
| 96 |
next; |
| 97 |
} |
| 98 |
|
| 99 |
$url = $STOCKDATA_URL . $stock . '&api_token=' . $token; |
| 100 |
$reply = $ua->request( GET $url); |
| 101 |
|
| 102 |
my $code = $reply->code; |
| 103 |
my $desc = HTTP::Status::status_message($code); |
| 104 |
my $headers = $reply->headers_as_string; |
| 105 |
my $body = decode('UTF-8', $reply->content); |
| 106 |
|
| 107 |
### Body: $body |
| 108 |
|
| 109 |
my ($name, $last, $open, $high, $low, $date, $isodate, $volume, $currency, $quote); |
| 110 |
|
| 111 |
$info{ $stock, "symbol" } = $stock; |
| 112 |
|
| 113 |
if ( $code == 200 ) { |
| 114 |
|
| 115 |
eval {$quote = JSON::decode_json $body}; |
| 116 |
if ($@) { |
| 117 |
$info{ $stock, 'success' } = 0; |
| 118 |
$info{ $stock, 'errormsg' } = $@; |
| 119 |
next; |
| 120 |
} |
| 121 |
|
| 122 |
### [<now>] JSON quote: $quote |
| 123 |
|
| 124 |
if (!exists $quote->{'meta'} || $quote->{'meta'}{'returned'} != 1) { |
| 125 |
$info{ $stock, 'success' } = 0; |
| 126 |
$info{ $stock, 'errormsg' } = $@; |
| 127 |
next; |
| 128 |
} |
| 129 |
|
| 130 |
$name = $quote->{'data'}[0]{'name'}; |
| 131 |
$last = $quote->{'data'}[0]{'price'}; |
| 132 |
$open = $quote->{'data'}[0]{'day_open'}; |
| 133 |
$low = $quote->{'data'}[0]{'day_low'}; |
| 134 |
$high = $quote->{'data'}[0]{'day_high'}; |
| 135 |
$volume = $quote->{'data'}[0]{'volume'}; |
| 136 |
$currency = $quote->{'data'}[0]{'currency'}; |
| 137 |
$date = $quote->{'data'}[0]{'last_trade_time'}; |
| 138 |
($isodate) = $date =~ m|^([\d\-]+)T|; |
| 139 |
|
| 140 |
### [<now>] isodate: $isodate |
| 141 |
|
| 142 |
$info{ $stock, 'name' } = $name; |
| 143 |
$info{ $stock, 'last' } = $last; |
| 144 |
$info{ $stock, 'open' } = $open; |
| 145 |
$info{ $stock, 'low' } = $low; |
| 146 |
$info{ $stock, 'high' } = $high; |
| 147 |
$info{ $stock, 'volume' } = $volume; |
| 148 |
$info{ $stock, 'currency' } = $currency; |
| 149 |
$info{ $stock, 'method' } = 'stockdata'; |
| 150 |
$quoter->store_date(\%info, $stock, {isodate => $isodate}); |
| 151 |
$info{ $stock, 'success' } = 1; |
| 152 |
|
| 153 |
} else { # HTTP Request failed (code != 200) |
| 154 |
$info{ $stock, "success" } = 0; |
| 155 |
$info{ $stock, "errormsg" } = |
| 156 |
"Error retrieving quote for $stock. Attempt to fetch the URL $url resulted in HTTP response $code ($desc)"; |
| 157 |
} |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
return wantarray() ? %info : \%info; |
| 162 |
return \%info; |
| 163 |
|
| 164 |
} |
| 165 |
|
| 166 |
1; |
| 167 |
|
| 168 |
__END__ |
| 169 |
|
| 170 |
=head1 NAME |
| 171 |
|
| 172 |
Finance::Quote::StockData - Obtain quotes from StockData.org |
| 173 |
|
| 174 |
=head1 SYNOPSIS |
| 175 |
|
| 176 |
use Finance::Quote; |
| 177 |
|
| 178 |
$q = Finance::Quote->new('StockData', |
| 179 |
stockdata => {API_KEY => 'your-stockdata-api-token'}); |
| 180 |
|
| 181 |
%info = $q->fetch('stockdata', 'AAPL'); # Only query foobar |
| 182 |
|
| 183 |
%info = $q->fetch('nyse', 'IBM'); # Failover to other sources OK. |
| 184 |
|
| 185 |
=head1 DESCRIPTION |
| 186 |
|
| 187 |
This module fetches information from L<https://www.stockdata.org/>. |
| 188 |
|
| 189 |
This module is loaded by default on a Finance::Quote object. It's also possible |
| 190 |
to load it explicitly by placing "stockdata" in the argument list to |
| 191 |
Finance::Quote->new(). |
| 192 |
|
| 193 |
This module provides "stockdata", "nyse", "nasdaq" |
| 194 |
fetch methods. Currently stock quote data is only |
| 195 |
available for securities traded on the US markets. |
| 196 |
|
| 197 |
Information obtained by this module may be covered by New York |
| 198 |
Exchange terms and conditions. |
| 199 |
|
| 200 |
=head1 API_KEY |
| 201 |
|
| 202 |
L<https://www.stockdata.org/> requires users to register and obtain |
| 203 |
an API key, which is also called a token. The token is a sequence |
| 204 |
of random characters. |
| 205 |
|
| 206 |
The API key may be set by either providing a module specific hash to |
| 207 |
Finance::Quote->new as in the above example, or by setting the environment |
| 208 |
variable STOCKDATA_API_KEY. |
| 209 |
|
| 210 |
=head1 LABELS RETURNED |
| 211 |
|
| 212 |
The following labels are returned: |
| 213 |
|
| 214 |
=over |
| 215 |
|
| 216 |
=item name |
| 217 |
|
| 218 |
=item symbol |
| 219 |
|
| 220 |
=item open |
| 221 |
|
| 222 |
=item high |
| 223 |
|
| 224 |
=item low |
| 225 |
|
| 226 |
=item last |
| 227 |
|
| 228 |
=item volume |
| 229 |
|
| 230 |
=item date |
| 231 |
|
| 232 |
=item currency |
| 233 |
|
| 234 |
=back |
| 235 |
|
| 236 |
=cut |