Quote/FinanceAPI.pm

filter: Statementstmt 31.5% 36 / 114 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.9% 10 / 11 Podpod 0.0% 0 / 4 totaltotal 28.7% 46 / 160 CC 19 SCAR 55.2
CC
19
Cov
25%
financeapi
380.0
BEGIN
1.0
BEGIN
1.0
CRAP 249.3
« Quote/NSEIndia.pm ↑ Summary Quote/BSEIndia.pm »
1 #!/usr/bin/perl -w
2 # vi: set ts=2 sw=2 noai ic showmode showmatch:
3 #
4 # Copyright (C) 2024, 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 # Changes:
22 # Initial Version: 2024-08-31, Bruce Schuck
23  
24 package Finance::Quote::FinanceAPI;
25  
26 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 5 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 5 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 5 ▶use JSON qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 5 ▶use constant DEBUG => $ENV{DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
34 5 ▶use if DEBUG, 'Smart::Comments', '###';
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 5 our $VERSION = '1.72_02'; # TRIAL VERSION
37  
38 5 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 5 our $DISPLAY = 'FinanceAPI';
44 5 our $FEATURES = {'API_KEY' => 'registered user API key'};
45 5 our @LABELS = qw/symbol name open high low last price date volume currency method/;
46 5 our $METHODHASH = {subroutine => \&financeapi,
47 display => $DISPLAY,
48 labels => \@LABELS,
49 features => $FEATURES};
50  
51 10 ▶sub methodinfo {
Subroutine methodinfo: called CC=1 SCAR=0.0
Pod undocumented
52 return (
53 10 financeapi => $METHODHASH,
54 nyse => $METHODHASH,
55 nasdaq => $METHODHASH,
56 usa => $METHODHASH,
57 );
58 }
59  
60 5 ▶sub labels { my %m = methodinfo(); return map {$_ => [@{$m{$_}{labels}}] } keys %m; }
Subroutine labels: called CC=1 SCAR=0.0
Pod undocumented
61  
62 5 ▶sub methods {
Subroutine methods: called CC=1 SCAR=0.0
Pod undocumented
63 5 my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m;
64 }
65  
66 0 ▶sub financeapi {
Subroutine financeapi: not called CC=19 SCAR=59.4
Pod undocumented
67  
68 0 my $quoter = shift;
69 0 my @stocks = @_;
70 0 my (%info, $url, $reply);
71 0 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 0 ▶ $ENV{"FINANCEAPI_API_KEY"};
Branch: 1 branch
BranchTrueFalseTotal
exists $quoter->{'module_specific_data'}{'stockdata'}{'API_KEY'} ? : 0 0 0
77  
78 # Set headers. API key is sent as a header.
79 0 my @ua_headers = (
80 'Accept' => 'application/json',
81 'X-API-KEY' => $token,
82 );
83  
84 0 foreach my $stock (@stocks) {
85  
86 0 ▶ if ( !defined $token ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $token) 0 0 0
87 0 $info{ $stock, 'success' } = 0;
88 0 $info{ $stock, 'errormsg' } =
89 'FINANCEAPI_API_KEY not defined. Get an API key at https://financeapi.net/';
90 0 next;
91 }
92  
93 0 $url = $FINANCEAPI_URL . $stock;
94 0 $reply = $ua->get( $url, @ua_headers );
95  
96 0 my $code = $reply->code;
97 0 my $desc = HTTP::Status::status_message($code);
98 0 my $headers = $reply->headers_as_string;
99 0 my $body = $reply->content;
100  
101 ### Body: $body
102  
103 0 my ($quote, $name, $bid, $ask, $last, $open, $high, $low, $volume, $currency, $date);
104 0 my ($month, $day, $year);
105  
106 0 $info{ $stock, "symbol" } = $stock;
107  
108 0 ▶ if ( $code == 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 200) { } 0 0 0
109  
110 0 eval {$quote = JSON::decode_json $body};
111 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
112 0 $info{ $stock, 'success' } = 0;
113 0 $info{ $stock, 'errormsg' } = $@;
114 0 next;
115 }
116  
117 ### [<now>] JSON quote: $quote
118  
119 0 ▶ if (!exists $quote->{'quoteResponse'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $quote->{"quoteResponse"}) 0 0 0
120 0 $info{ $stock, 'success' } = 0;
121 0 $info{ $stock, 'errormsg' } = $@;
122 0 next;
123 }
124  
125 # JSON Result Array is sometimes empty
126 0 my $json_data_count = scalar @{ $quote->{'quoteResponse'}{'result'} };
127 0 ▶ if ( $json_data_count < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json_data_count < 1) 0 0 0
128 0 $info{ $stock, "success" } = 0;
129 0 $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 0 next;
132 }
133  
134 0 $name = $quote->{'quoteResponse'}{'result'}[0]{'longName'};
135 0 $open = $quote->{'quoteResponse'}{'result'}[0]{'regularMarketOpen'};
136 0 $high = $quote->{'quoteResponse'}{'result'}[0]{'regularMarketDayHigh'};
137 0 $low = $quote->{'quoteResponse'}{'result'}[0]{'regularMarketDayLow'};
138 0 $last = $quote->{'quoteResponse'}{'result'}[0]{'regularMarketPrice'};
139 0 $volume = $quote->{'quoteResponse'}{'result'}[0]{'regularMarketVolume'};
140 #$currency = $quote->{'quoteResponse'}{'result'}[0]{'financialCurrency'};
141 0 $currency = $quote->{'quoteResponse'}{'result'}[0]{'currency'};
142 0 $date = $quote->{'quoteResponse'}{'result'}[0]{'regularMarketTime'};
143 0 ($month, $day, $year) = (localtime($date))[4,3,5];
144 0 $month++;
145 0 $year += 1900;
146  
147 0 $info{$stock, 'name'} = $name;
148 0 $info{$stock, 'open'} = $open;
149 0 $info{$stock, 'high'} = $high;
150 0 $info{$stock, 'low'} = $low;
151 0 $info{$stock, 'last'} = $last;
152 0 $info{$stock, 'price'} = $last;
153 0 $info{$stock, 'volume'} = $volume;
154 0 $info{$stock, 'currency'} = $currency;
155 0 $info{ $stock, 'method' } = 'financeapi';
156 0 $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 0 ▶ if ( ($info{$stock,"currency"} eq "GBp") ||
Branch: 1 branch
BranchTrueFalseTotal
if ($info{join $;, $stock, "currency"} eq "GBp" or $info{join $;, $stock, "currency"} eq "GBX") 0 0 0
Condition0%0/3
$info{join $;, $stock, "currency"} eq "GBp" or $info{join $;, $stock, "currency"} eq "GBX"
l!l&&r!l&&!r
000
MC/DC0%0/2
$info{join $;, $stock, "currency"} eq "GBp" or $info{join $;, $stock, "currency"} eq "GBX"
$info{join $;, $stock, "currency"} eq "GBp" $info{join $;, $stock, "currency"} eq "GBX"
Truth table0%0/3
A or B
A= $info{join $;, $stock, "currency"} eq "GBp"B= $info{join $;, $stock, "currency"} eq "GBX"
ABresult
1X1
011
000
164 ($info{$stock,"currency"} eq "GBX")) {
165 0 foreach my $field ( $quoter->default_currency_fields ) {
166 0 ▶ next unless ( $info{ $stock, $field } );
Branch: 1 branch
BranchTrueFalseTotal
unless $info{join $;, $stock, $field} 0 0 0
167 $info{ $stock, $field }
168 0 = $quoter->scale_field( $info{ $stock, $field }, 0.01 );
169 }
170 0 $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 0 ▶ if ($info{$stock,"currency"} eq "ZAc") {
Branch: 1 branch
BranchTrueFalseTotal
if ($info{join $;, $stock, "currency"} eq "ZAc") 0 0 0
179 0 foreach my $field ( $quoter->default_currency_fields ) {
180 0 ▶ next unless ( $info{ $stock, $field } );
Branch: 1 branch
BranchTrueFalseTotal
unless $info{join $;, $stock, $field} 0 0 0
181 $info{ $stock, $field }
182 0 = $quoter->scale_field( $info{ $stock, $field }, 0.01 );
183 }
184 0 $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 0 ▶ if ($info{$stock,"currency"} eq "ILA") {
Branch: 1 branch
BranchTrueFalseTotal
if ($info{join $;, $stock, "currency"} eq "ILA") 0 0 0
193 0 foreach my $field ( $quoter->default_currency_fields ) {
194 0 ▶ next unless ( $info{ $stock, $field } );
Branch: 1 branch
BranchTrueFalseTotal
unless $info{join $;, $stock, $field} 0 0 0
195 $info{ $stock, $field }
196 0 = $quoter->scale_field( $info{ $stock, $field }, 0.01 );
197 }
198 0 $info{ $stock, "currency"} = "ILS";
199 }
200  
201 0 $info{ $stock, 'success' } = 1;
202  
203 } else { # HTTP Request failed (code != 200)
204 0 $info{ $stock, "success" } = 0;
205 0 $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 0 ▶ return wantarray() ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
212 0 return \%info;
213  
214 }
215  
216 5 1;
217  
218 __END__
« Quote/NSEIndia.pm ↑ Summary Quote/BSEIndia.pm »