| 1 | #!/usr/bin/perl -w | ||||||||||||||||||||||||||||
| 2 | # vi: set ts=2 sw=2 noai ic showmode showmatch: | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # Copyright (C) 2023, Bruce Schuck <bschuck@asgard-systems.com> | ||||||||||||||||||||||||||||
| 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 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 5 | ▶ | use Encode qw(decode); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 5 | ▶ | use HTTP::Request::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 5 | ▶ | use JSON qw(decode_json); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 5 | ▶ | use constant DEBUG => $ENV{DEBUG}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 5 | ▶ | use if DEBUG, 'Smart::Comments', '###'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 5 | our $VERSION = '1.72_01'; # TRIAL VERSION | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 5 | 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 | 5 | my $user_agent = 'Finance-Quote OpenSource Stock Quote Tool'; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 5 | our $DISPLAY = 'StockData'; | |||||||||||||||||||||||||||
| 43 | 5 | our $FEATURES = {'API_KEY' => 'registered user API key'}; | |||||||||||||||||||||||||||
| 44 | 5 | our @LABELS = qw/symbol name open high low last date volume currency method/; | |||||||||||||||||||||||||||
| 45 | 5 | our $METHODHASH = {subroutine => \&stockdata, | |||||||||||||||||||||||||||
| 46 | display => $DISPLAY, | ||||||||||||||||||||||||||||
| 47 | labels => \@LABELS, | ||||||||||||||||||||||||||||
| 48 | features => $FEATURES}; | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 10 | ▶ | sub methodinfo { | ||||||||||||||||||||||||||
|
Subroutine
methodinfo: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 51 | return ( | ||||||||||||||||||||||||||||
| 52 | 10 | stockdata => $METHODHASH, | |||||||||||||||||||||||||||
| 53 | nyse => $METHODHASH, | ||||||||||||||||||||||||||||
| 54 | nasdaq => $METHODHASH, | ||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 5 | ▶ | sub labels { my %m = methodinfo(); return map {$_ => [@{$m{$_}{labels}}] } keys %m; } | ||||||||||||||||||||||||||
|
Subroutine
labels: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 5 | ▶ | sub methods { | ||||||||||||||||||||||||||
|
Subroutine
methods: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 61 | 5 | 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 | 0 | ▶ | sub stockdata { | ||||||||||||||||||||||||||
|
Subroutine
stockdata: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | my $quoter = shift; | |||||||||||||||||||||||||||
| 81 | 0 | my @stocks = @_; | |||||||||||||||||||||||||||
| 82 | 0 | my (%info, $url, $reply); | |||||||||||||||||||||||||||
| 83 | 0 | my $ua = $quoter->user_agent(); | |||||||||||||||||||||||||||
| 84 | 0 | $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 | 0 | ▶ | $ENV{"STOCKDATA_API_KEY"}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | foreach my $stock (@stocks) { | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | if ( !defined $token ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | $info{ $stock, 'success' } = 0; | |||||||||||||||||||||||||||
| 94 | 0 | $info{ $stock, 'errormsg' } = | |||||||||||||||||||||||||||
| 95 | 'StockData API_KEY not defined. Get an API key at https://stockdata.org'; | ||||||||||||||||||||||||||||
| 96 | 0 | next; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | $url = $STOCKDATA_URL . $stock . '&api_token=' . $token; | |||||||||||||||||||||||||||
| 100 | 0 | $reply = $ua->request( GET $url); | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | my $code = $reply->code; | |||||||||||||||||||||||||||
| 103 | 0 | my $desc = HTTP::Status::status_message($code); | |||||||||||||||||||||||||||
| 104 | 0 | my $headers = $reply->headers_as_string; | |||||||||||||||||||||||||||
| 105 | 0 | my $body = decode('UTF-8', $reply->content); | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | ### Body: $body | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | my ($name, $last, $open, $high, $low, $date, $isodate, $volume, $currency, $quote); | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | $info{ $stock, "symbol" } = $stock; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | if ( $code == 200 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | eval {$quote = JSON::decode_json $body}; | |||||||||||||||||||||||||||
| 116 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | $info{ $stock, 'success' } = 0; | |||||||||||||||||||||||||||
| 118 | 0 | $info{ $stock, 'errormsg' } = $@; | |||||||||||||||||||||||||||
| 119 | 0 | next; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | ### [<now>] JSON quote: $quote | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | if (!exists $quote->{'meta'} || $quote->{'meta'}{'returned'} != 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not exists $quote->{"meta"} or $quote->{"meta"}{"returned"} != 1
MC/DC0%0/2
not exists $quote->{"meta"} or $quote->{"meta"}{"returned"} != 1
not exists $quote->{"meta"}
$quote->{"meta"}{"returned"} != 1
Truth table0%0/3
not(A) or B
A= not exists $quote->{"meta"}B= $quote->{"meta"}{"returned"} != 1
| |||||||||||||||||||||||||||||
| 125 | 0 | $info{ $stock, 'success' } = 0; | |||||||||||||||||||||||||||
| 126 | 0 | $info{ $stock, 'errormsg' } = $@; | |||||||||||||||||||||||||||
| 127 | 0 | next; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | $name = $quote->{'data'}[0]{'name'}; | |||||||||||||||||||||||||||
| 131 | 0 | $last = $quote->{'data'}[0]{'price'}; | |||||||||||||||||||||||||||
| 132 | 0 | $open = $quote->{'data'}[0]{'day_open'}; | |||||||||||||||||||||||||||
| 133 | 0 | $low = $quote->{'data'}[0]{'day_low'}; | |||||||||||||||||||||||||||
| 134 | 0 | $high = $quote->{'data'}[0]{'day_high'}; | |||||||||||||||||||||||||||
| 135 | 0 | $volume = $quote->{'data'}[0]{'volume'}; | |||||||||||||||||||||||||||
| 136 | 0 | $currency = $quote->{'data'}[0]{'currency'}; | |||||||||||||||||||||||||||
| 137 | 0 | $date = $quote->{'data'}[0]{'last_trade_time'}; | |||||||||||||||||||||||||||
| 138 | 0 | ($isodate) = $date =~ m|^([\d\-]+)T|; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | ### [<now>] isodate: $isodate | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | $info{ $stock, 'name' } = $name; | |||||||||||||||||||||||||||
| 143 | 0 | $info{ $stock, 'last' } = $last; | |||||||||||||||||||||||||||
| 144 | 0 | $info{ $stock, 'open' } = $open; | |||||||||||||||||||||||||||
| 145 | 0 | $info{ $stock, 'low' } = $low; | |||||||||||||||||||||||||||
| 146 | 0 | $info{ $stock, 'high' } = $high; | |||||||||||||||||||||||||||
| 147 | 0 | $info{ $stock, 'volume' } = $volume; | |||||||||||||||||||||||||||
| 148 | 0 | $info{ $stock, 'currency' } = $currency; | |||||||||||||||||||||||||||
| 149 | 0 | $info{ $stock, 'method' } = 'stockdata'; | |||||||||||||||||||||||||||
| 150 | 0 | $quoter->store_date(\%info, $stock, {isodate => $isodate}); | |||||||||||||||||||||||||||
| 151 | 0 | $info{ $stock, 'success' } = 1; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | } else { # HTTP Request failed (code != 200) | ||||||||||||||||||||||||||||
| 154 | 0 | $info{ $stock, "success" } = 0; | |||||||||||||||||||||||||||
| 155 | 0 | $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 | 0 | ▶ | return wantarray() ? %info : \%info; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 0 | return \%info; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 5 | 1; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | __END__ | ||||||||||||||||||||||||||||