Quote/ASX.pm

filter: Statementstmt 32.1% 35 / 109 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 71.4% 10 / 14 Podpod 0.0% 0 / 7 totaltotal 24.1% 45 / 186 CC 23 SCAR 37.0
CC
23
Cov
23%
asx
72.0
get_asx_data
56.0
process_asx_data
56.0
CRAP 40.3
« Quote/CurrencyRates/OpenExchange.pm ↑ Summary Quote.pm »
1 #!/usr/bin/perl -w
2 # vi: set ts=2 sw=2 noai ic showmode showmatch:
3 #
4 # Copyright (C) 1998, Dj Padzensky <djpadz@padz.net>
5 # Copyright (C) 1998, 1999 Linas Vepstas <linas@linas.org>
6 # Copyright (C) 2000, Yannick LE NY <y-le-ny@ifrance.com>
7 # Copyright (C) 2000, Brent Neal <brentn@users.sourceforge.net>
8 # Copyright (C) 2001, Leigh Wedding <leigh.wedding@telstra.com>
9 # Copyright (C) 2000-2004, Paul Fenwick <pjf@cpan.org>
10 # Copyright (C) 2014, Chris Good <chris.good@@ozemail.com.au>
11 #
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 # 02110-1301, USA
26 #
27 #
28 # This code derived from Padzensky's work on package Finance::YahooQuote,
29 # but extends its capabilites to encompas a greater number of data sources.
30 #
31 # This code was developed as part of GnuCash <http://www.gnucash.org/>
32  
33 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 package Finance::Quote::ASX;
37  
38 5 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 5 ▶use JSON qw/decode_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 5 ▶use constant DEBUG => $ENV{DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
42 5 ▶use if DEBUG, 'Smart::Comments';
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 5 ▶use vars qw/$ASX_URL/;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 5 our $VERSION = '1.72_02'; # TRIAL VERSION
47  
48 5 our $DISPLAY = 'ASX - Australian Securities Exchange';
49 5 our @LABELS = qw/symbol ask bid p_change net type name last price volume currency method/;
50 5 our $METHODHASH = {subroutine => \&asx,
51 display => $DISPLAY,
52 labels => \@LABELS};
53  
54 10 ▶sub methodinfo {
Subroutine methodinfo: called CC=1 SCAR=0.0
Pod undocumented
55 return (
56 10 asx => $METHODHASH,
57 australia => $METHODHASH,
58 );
59 }
60  
61 5 ▶sub labels {
Subroutine labels: called CC=1 SCAR=0.0
Pod undocumented
62 5 my %m = methodinfo(); return map {$_ => [@{$m{$_}{labels}}] } keys %m;
63 }
64  
65 5 ▶sub methods {
Subroutine methods: called CC=1 SCAR=0.0
Pod undocumented
66 5 my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m;
67 }
68  
69 5 $ASX_URL = 'https://asx.api.markitdigital.com/asx-research/1.0/companies/';
70  
71 # Australian Stock Exchange (ASX)
72 # The ASX provides free delayed quotes through their webpage:
73 # https://www2.asx.com.au/markets/company/NAB
74 #
75 # Maintainer of this section is Paul Fenwick <pjf@cpan.org>
76 # 5-May-2001 Updated by Leigh Wedding <leigh.wedding@telstra.com>
77 # 24-Feb-2014 Updated by Chris Good <chris.good@@ozemail.com.au>
78 # 12-Oct-2020 Updated by Jeremy Volkening
79 #
80 # Jan-2021 Updated by Geoff <cleanoutmyshed@gmail.com>
81 # October 2020 the ASX revamped their website with dynamic content for quotes
82 # which prevented the previous HTML screen scraping from working, but exposed
83 # a number of JSON data sources, two of which are used here.
84 # The primary source returns data elements for almost all securities, but
85 # does not return prices for certain security types (some bonds and exchange
86 # traded products, options, and warrants), and returns an error for indices.
87 # The alternate source returns less data elements, but provides usable quote
88 # data for all the known exceptions, including indices.
89 # This version will always call the primary source, and call the alternate if
90 # a price is not returned by the primary.
91 #
92 # 2024-10-21 Updated by Bruce Schuck <bschuck@asgard-systems.com>
93 # The primary URL (https://www.asx.com.au/asx/1/share/) stopped retrieving
94 # data. The website is utilizing the Imperva Incapsula, an anti-webscraping
95 # service. A caveat of using the alternate URL is that only a few
96 # data points are returned and no trade date.
97 #
98 # Smart::Comments implemented to conform with the Hackers Guide:
99 # https://github.com/finance-quote/finance-quote/blob/master/Documentation/Hackers-Guide
100 #
101  
102 # Main function to fetch quotes from the Australian Securities Exchange (ASX)
103  
104 0 ▶sub asx {
Subroutine asx: not called CC=8 SCAR=42.8
Pod undocumented
105  
106 0 my $quoter = shift;
107 0 ▶ my @symbols = @_
Branch: 1 branch
BranchTrueFalseTotal
my(@symbols) = or return 0 0 0
108 or return;
109  
110 0 my($error, %info, $status, $ua, $url);
111  
112 0 $ua = $quoter->user_agent;
113  
114 0 for my $symbol (@symbols) {
115 ### ASX.pm Processing symbol: $symbol
116 0 $info{ $symbol, 'symbol' } = $symbol;
117 0 $info{ $symbol, 'method' } = 'asx';
118 0 $info{ $symbol, 'currency' } = 'AUD';
119  
120 0 $symbol =~ s/\s+$//;
121 0 ▶ if ($symbol !~ m/^[A-Za-z0-9]{1,6}$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($symbol =~ /^[A-Za-z0-9]{1,6}$/) 0 0 0
122 0 $info{ $symbol, 'success' } = 0;
123 0 $info{ $symbol, 'errormsg' } = 'Invalid symbol. ASX symbols must be alpha numeric maximum length 6 characters.';
124 ### ASX.pm: $info{ $symbol, 'errormsg' }
125 0 next;
126 }
127  
128 0 ($status, $error) = asx_fetch($symbol, $ua, \%info);
129  
130 0 ▶ if ($status != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status != 1) 0 0 0
131 0 $info{ $symbol, 'success' } = 0;
132 0 $info{ $symbol, 'errormsg' } = "$error";
133 ### ASX.pm Unsuccessful call to ASX URL - symbol cannot be processed: $symbol
134 0 next;
135 }
136  
137 ### ASX.pm We have valid data, apply various clean ups and add remaining data for symbol: $symbol
138  
139 # Remove trailing percentage sign from p_change
140 0 $info{ $symbol, 'p_change' } =~ s/\%$//;
141  
142 0 $info{ $symbol, 'price' } = $info{ $symbol, 'last' };
143  
144 0 ▶ if ((exists $info{ $symbol, 'date' }) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $info{join $;, $symbol, "date"} and $info{join $;, $symbol, "date"} =~ /([0-9]{4}-[0-9]{2}-[0-9]{2})T/) 0 0 0
Condition0%0/3
exists $info{join $;, $symbol, "date"} and $info{join $;, $symbol, "date"} =~ /([0-9]{4}-[0-9]{2}-[0-9]{2})T/
!ll&&!rl&&r
000
MC/DC0%0/2
exists $info{join $;, $symbol, "date"} and $info{join $;, $symbol, "date"} =~ /([0-9]{4}-[0-9]{2}-[0-9]{2})T/
exists $info{join $;, $symbol, "date"} $info{join $;, $symbol, "date"} =~ /([0-9]{4}-[0-9]{2}-[0-9]{2})T/
Truth table0%0/3
A and B
A= exists $info{join $;, $symbol, "date"}B= $info{join $;, $symbol, "date"} =~ /([0-9]{4}-[0-9]{2}-[0-9]{2})T/
ABresult
0X0
100
111
145 ($info{ $symbol, 'date' } =~ m/([0-9]{4}-[0-9]{2}-[0-9]{2})T/)) {
146 0 $quoter->store_date(\%info, $symbol, {isodate => $1});
147 ### ASX.pm Converted Last Trade Date to ISO format: "$info{ $symbol, 'date' } --> $1"
148 }
149  
150 # Technically indices don't have a currency, but it is not possible to distinguish them
151 0 $info{ $symbol, 'currency' } = 'AUD';
152  
153 0 $info{ $symbol, 'success' } = 1;
154 0 $info{ $symbol, 'errormsg' } = '';
155  
156 }
157  
158 ### ASX.pm Returning data for all symbols to Finance-Quote and exiting <file>[<line>]
159 0 ▶ return %info if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
160 0 return \%info;
161  
162 } # end main asx method
163  
164 # Internal function to handle ASX data source
165  
166 0 ▶sub asx_fetch {
Subroutine asx_fetch: not called CC=4 SCAR=30.0
Pod undocumented
167  
168 0 my ($symbol, $ua, $info) = @_;
169  
170 0 my($data, $error, %label_map, $status, $url);
171  
172 0 $url = $ASX_URL . $symbol . '/header';
173 0 ($status, $error, $data) = get_asx_data($url, $ua);
174 0 ▶ return $status, $error unless $status == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $status == 1 0 0 0
175  
176 0 ▶ if (exists $data->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"error"}) 0 0 0
177 0 $status = 0;
178 0 $error = "Error returned by ASX server '$url'. Code: " . $data->{error}{code} . ' Message: ' . $data->{error}{message};
179 ### ASX.pm Error: $error
180 0 return $status, $error;
181 }
182  
183 0 ▶ if (! exists $data->{data}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $data->{"data"}) 0 0 0
184 0 $status = 0;
185 0 $error = "Cannot parse content from ASX server '$url'. Expected a top level JSON element named data.";
186 ### ASX.pm Error: $error
187 0 return $status, $error;
188 }
189  
190 # Map the Finance::Quote labels (left) to the corresponding ASX labels (right)
191 %label_map = (
192 0 'name' => 'displayName',
193 'ask' => 'priceAsk',
194 'bid' => 'priceBid',
195 'net' => 'priceChange',
196 'p_change' => 'priceChangePercent',
197 'last' => 'priceLast',
198 'type' => 'securityType',
199 'volume' => 'volume',
200 );
201  
202 0 process_asx_data($symbol, $data->{data}, \%label_map, $info);
203  
204 0 return 1, '';
205  
206 } # end asx_fetch
207  
208 # Internal function to fetch, validate, and decode data from an ASX URL using LWP User Agent
209 # Handle any errors
210  
211 0 ▶sub get_asx_data {
Subroutine get_asx_data: not called CC=7 SCAR=40.3
Pod undocumented
212  
213 0 my ($url, $ua) = @_;
214  
215 0 my($data, $error, $json, $response, $status);
216  
217 ### ASX.pm Retrieving data from ASX URL: $url
218  
219 # HTTP code 400 is returned if the symbol is not found, along with
220 # a JSON structure:
221 # {"error":{"code":400,"message":"Bad Request: Symbol not found","errors":[{"message":"Bad Request: Symbol not found"}]}}
222  
223 0 $response = $ua->get($url);
224 0 ▶ if (! $response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
225 0 $status = 0;
226 0 ▶ if ( $response->code == 400 && $response->header('content-type') =~ m|application/json|i && $response->content =~ m|Symbol not found|i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code == 400 and $response->header('content-type') =~ m[application/json]i and $response->content =~ /Symbol not found/i) { } 0 0 0
Condition0%0/6
$response->code == 400 and $response->header("content-type") =~ m[application/json]i and $response->content =~ /Symbol not found/i
!ll&&!rl&&r
000
$response->code == 400 and $response->header("content-type") =~ m[application/json]i
!ll&&!rl&&r
000
MC/DC0%0/3
$response->code == 400 and $response->header("content-type") =~ m[application/json]i and $response->content =~ /Symbol not found/i
$response->code == 400 $response->header("content-type") =~ m[application/json]i $response->content =~ /Symbol not found/i
Truth table0%0/4
A and B and C
A= $response->code == 400B= $response->header("content-type") =~ m[application/json]iC= $response->content =~ /Symbol not found/i
ABCresult
0XX0
10X0
1100
1111
227 0 $error = "Symbol not found!";
228 } else {
229 0 $error = "Unable to fetch data from the ASX server '$url'. Status: " . $response->status_line;
230 }
231 ### ASX.pm Error: $error
232 0 return $status, $error, undef;
233 }
234  
235 0 ▶ if ($response->header('content-type') !~ m|application/json|i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->header("content-type") =~ m[application/json]i) 0 0 0
236 0 $status = 0;
237 0 $error = "Invalid content-type from ASX server '$url'. Expected: application/json, received: " . $response->header('content-type');
238 ### ASX.pm Error: $error
239 0 return $status, $error, undef;
240 }
241  
242 0 $json = $response->content;
243  
244 # The JSON module will croak on errors, so use eval to trap this.
245 0 $data = eval{ decode_json($json) };
246 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
247 0 $status = 0;
248 0 $error = "Failed to parse JSON data from ASX server '$url'. Error: '$@'.";
249 ### ASX.pm Error: $error
250 0 return $status, $error, undef;
251 }
252  
253 # Return valid, decoded data
254 0 $status = 1;
255 0 return $status, $error, $data;
256  
257 } # end get_asx_data
258  
259 # Internal function to push the ASX data elements into the Finance::Quote structure (%info)
260  
261 0 ▶sub process_asx_data {
Subroutine process_asx_data: not called CC=7 SCAR=40.3
Pod undocumented
262  
263 0 my ($symbol, $data, $label_map, $info) = @_;
264  
265 0 foreach my $label (sort(keys %{$label_map})) {
266 0 ▶ if ((exists $data->{$label_map->{$label}}) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$data{$label_map->{$label}} and defined $$data{$label_map->{$label}}) { } 0 0 0
Condition0%0/3
exists $$data{$label_map->{$label}} and defined $$data{$label_map->{$label}}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $$data{$label_map->{$label}} and defined $$data{$label_map->{$label}}
exists $$data{$label_map->{$label}} defined $$data{$label_map->{$label}}
Truth table0%0/3
A and B
A= exists $$data{$label_map->{$label}}B= defined $$data{$label_map->{$label}}
ABresult
0X0
100
111
267 (defined $data->{$label_map->{$label}})) {
268 # Concatenate Primary and Alternate Names
269 0 ▶ if (($label eq 'name') &&
Branch: 1 branch
BranchTrueFalseTotal
if ($label eq 'name' and exists $$info{join $;, $symbol, $label} and uc $$info{join $;, $symbol, $label} ne uc $$data{$label_map->{$label}}) { } 0 0 0
Condition0%0/6
$label eq "name" and exists $$info{join $;, $symbol, $label} and uc $$info{join $;, $symbol, $label} ne uc $$data{$label_map->{$label}}
!ll&&!rl&&r
000
$label eq "name" and exists $$info{join $;, $symbol, $label}
!ll&&!rl&&r
000
MC/DC0%0/3
$label eq "name" and exists $$info{join $;, $symbol, $label} and uc $$info{join $;, $symbol, $label} ne uc $$data{$label_map->{$label}}
$label eq "name" exists $$info{join $;, $symbol, $label} uc $$info{join $;, $symbol, $label} ne uc $$data{$label_map->{$label}}
Truth table0%0/4
A and B and C
A= $label eq "name"B= exists $$info{join $;, $symbol, $label}C= uc $$info{join $;, $symbol, $label} ne uc $$data{$label_map->{$label}}
ABCresult
0XX0
10X0
1100
1111
270 (exists $info->{$symbol, $label}) &&
271 (uc($info->{$symbol, $label}) ne uc($data->{$label_map->{$label}}))) {
272 0 $info->{$symbol, $label} = $data->{$label_map->{$label}} . ' ' . $info->{$symbol, $label};
273 }
274 # Overwrite all other labels
275 else {
276 0 $info->{$symbol, $label} = $data->{$label_map->{$label}};
277 }
278 ### ASX.pm Mapped ASX data element to Finance-Quote: sprintf("%-22s%-15s%-s", $label_map->{$label}, $label, $data->{$label_map->{$label}})
279 }
280 else {
281 0 $info->{$symbol,$label} = '';
282 }
283 }
284  
285 0 return;
286  
287 } # end process_asx_data
288  
289 5 1;
290 __END__
« Quote/CurrencyRates/OpenExchange.pm ↑ Summary Quote.pm »