| 1 |
#!/usr/bin/perl -w |
| 2 |
# vi: set ts=2 sw=2 noai ic showmode showmatch: |
| 3 |
# |
| 4 |
# Copyright (C) 1998, Dj Padzensky <[email protected]> |
| 5 |
# Copyright (C) 1998, 1999 Linas Vepstas <[email protected]> |
| 6 |
# Copyright (C) 2000, Yannick LE NY <[email protected]> |
| 7 |
# Copyright (C) 2000, Brent Neal <[email protected]> |
| 8 |
# Copyright (C) 2001, Leigh Wedding <[email protected]> |
| 9 |
# Copyright (C) 2000-2004, Paul Fenwick <[email protected]> |
| 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 |
use strict; |
| 34 |
use warnings; |
| 35 |
|
| 36 |
package Finance::Quote::ASX; |
| 37 |
|
| 38 |
use LWP::UserAgent; |
| 39 |
use JSON qw/decode_json/; |
| 40 |
|
| 41 |
use constant DEBUG => $ENV{DEBUG}; |
| 42 |
use if DEBUG, 'Smart::Comments'; |
| 43 |
|
| 44 |
use vars qw/$ASX_URL/; |
| 45 |
|
| 46 |
our $VERSION = '1.71'; # VERSION |
| 47 |
|
| 48 |
our $DISPLAY = 'ASX - Australian Securities Exchange'; |
| 49 |
our @LABELS = qw/symbol ask bid p_change net type name last price volume currency method/; |
| 50 |
our $METHODHASH = {subroutine => \&asx, |
| 51 |
display => $DISPLAY, |
| 52 |
labels => \@LABELS}; |
| 53 |
|
| 54 |
sub methodinfo { |
| 55 |
return ( |
| 56 |
asx => $METHODHASH, |
| 57 |
australia => $METHODHASH, |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
sub labels { |
| 62 |
my %m = methodinfo(); return map {$_ => [@{$m{$_}{labels}}] } keys %m; |
| 63 |
} |
| 64 |
|
| 65 |
sub methods { |
| 66 |
my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m; |
| 67 |
} |
| 68 |
|
| 69 |
$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 <[email protected]> |
| 76 |
# 5-May-2001 Updated by Leigh Wedding <[email protected]> |
| 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 <[email protected]> |
| 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 <[email protected]> |
| 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 |
sub asx { |
| 105 |
|
| 106 |
my $quoter = shift; |
| 107 |
my @symbols = @_ |
| 108 |
or return; |
| 109 |
|
| 110 |
my($error, %info, $status, $ua, $url); |
| 111 |
|
| 112 |
$ua = $quoter->user_agent; |
| 113 |
|
| 114 |
for my $symbol (@symbols) { |
| 115 |
### ASX.pm Processing symbol: $symbol |
| 116 |
$info{ $symbol, 'symbol' } = $symbol; |
| 117 |
$info{ $symbol, 'method' } = 'asx'; |
| 118 |
$info{ $symbol, 'currency' } = 'AUD'; |
| 119 |
|
| 120 |
$symbol =~ s/\s+$//; |
| 121 |
if ($symbol !~ m/^[A-Za-z0-9]{1,6}$/) { |
| 122 |
$info{ $symbol, 'success' } = 0; |
| 123 |
$info{ $symbol, 'errormsg' } = 'Invalid symbol. ASX symbols must be alpha numeric maximum length 6 characters.'; |
| 124 |
### ASX.pm: $info{ $symbol, 'errormsg' } |
| 125 |
next; |
| 126 |
} |
| 127 |
|
| 128 |
($status, $error) = asx_fetch($symbol, $ua, \%info); |
| 129 |
|
| 130 |
if ($status != 1) { |
| 131 |
$info{ $symbol, 'success' } = 0; |
| 132 |
$info{ $symbol, 'errormsg' } = "$error"; |
| 133 |
### ASX.pm Unsuccessful call to ASX URL - symbol cannot be processed: $symbol |
| 134 |
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 |
$info{ $symbol, 'p_change' } =~ s/\%$//; |
| 141 |
|
| 142 |
$info{ $symbol, 'price' } = $info{ $symbol, 'last' }; |
| 143 |
|
| 144 |
if ((exists $info{ $symbol, 'date' }) && |
| 145 |
($info{ $symbol, 'date' } =~ m/([0-9]{4}-[0-9]{2}-[0-9]{2})T/)) { |
| 146 |
$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 |
$info{ $symbol, 'currency' } = 'AUD'; |
| 152 |
|
| 153 |
$info{ $symbol, 'success' } = 1; |
| 154 |
$info{ $symbol, 'errormsg' } = ''; |
| 155 |
|
| 156 |
} |
| 157 |
|
| 158 |
### ASX.pm Returning data for all symbols to Finance-Quote and exiting <file>[<line>] |
| 159 |
return %info if wantarray; |
| 160 |
return \%info; |
| 161 |
|
| 162 |
} # end main asx method |
| 163 |
|
| 164 |
# Internal function to handle ASX data source |
| 165 |
|
| 166 |
sub asx_fetch { |
| 167 |
|
| 168 |
my ($symbol, $ua, $info) = @_; |
| 169 |
|
| 170 |
my($data, $error, %label_map, $status, $url); |
| 171 |
|
| 172 |
$url = $ASX_URL . $symbol . '/header'; |
| 173 |
($status, $error, $data) = get_asx_data($url, $ua); |
| 174 |
return $status, $error unless $status == 1; |
| 175 |
|
| 176 |
if (exists $data->{error}) { |
| 177 |
$status = 0; |
| 178 |
$error = "Error returned by ASX server '$url'. Code: " . $data->{error}{code} . ' Message: ' . $data->{error}{message}; |
| 179 |
### ASX.pm Error: $error |
| 180 |
return $status, $error; |
| 181 |
} |
| 182 |
|
| 183 |
if (! exists $data->{data}) { |
| 184 |
$status = 0; |
| 185 |
$error = "Cannot parse content from ASX server '$url'. Expected a top level JSON element named data."; |
| 186 |
### ASX.pm Error: $error |
| 187 |
return $status, $error; |
| 188 |
} |
| 189 |
|
| 190 |
# Map the Finance::Quote labels (left) to the corresponding ASX labels (right) |
| 191 |
%label_map = ( |
| 192 |
'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 |
process_asx_data($symbol, $data->{data}, \%label_map, $info); |
| 203 |
|
| 204 |
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 |
sub get_asx_data { |
| 212 |
|
| 213 |
my ($url, $ua) = @_; |
| 214 |
|
| 215 |
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 |
$response = $ua->get($url); |
| 224 |
if (! $response->is_success) { |
| 225 |
$status = 0; |
| 226 |
if ( $response->code == 400 && $response->header('content-type') =~ m|application/json|i && $response->content =~ m|Symbol not found|i ) { |
| 227 |
$error = "Symbol not found!"; |
| 228 |
} else { |
| 229 |
$error = "Unable to fetch data from the ASX server '$url'. Status: " . $response->status_line; |
| 230 |
} |
| 231 |
### ASX.pm Error: $error |
| 232 |
return $status, $error, undef; |
| 233 |
} |
| 234 |
|
| 235 |
if ($response->header('content-type') !~ m|application/json|i) { |
| 236 |
$status = 0; |
| 237 |
$error = "Invalid content-type from ASX server '$url'. Expected: application/json, received: " . $response->header('content-type'); |
| 238 |
### ASX.pm Error: $error |
| 239 |
return $status, $error, undef; |
| 240 |
} |
| 241 |
|
| 242 |
$json = $response->content; |
| 243 |
|
| 244 |
# The JSON module will croak on errors, so use eval to trap this. |
| 245 |
$data = eval{ decode_json($json) }; |
| 246 |
if ($@) { |
| 247 |
$status = 0; |
| 248 |
$error = "Failed to parse JSON data from ASX server '$url'. Error: '$@'."; |
| 249 |
### ASX.pm Error: $error |
| 250 |
return $status, $error, undef; |
| 251 |
} |
| 252 |
|
| 253 |
# Return valid, decoded data |
| 254 |
$status = 1; |
| 255 |
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 |
sub process_asx_data { |
| 262 |
|
| 263 |
my ($symbol, $data, $label_map, $info) = @_; |
| 264 |
|
| 265 |
foreach my $label (sort(keys %{$label_map})) { |
| 266 |
if ((exists $data->{$label_map->{$label}}) && |
| 267 |
(defined $data->{$label_map->{$label}})) { |
| 268 |
# Concatenate Primary and Alternate Names |
| 269 |
if (($label eq 'name') && |
| 270 |
(exists $info->{$symbol, $label}) && |
| 271 |
(uc($info->{$symbol, $label}) ne uc($data->{$label_map->{$label}}))) { |
| 272 |
$info->{$symbol, $label} = $data->{$label_map->{$label}} . ' ' . $info->{$symbol, $label}; |
| 273 |
} |
| 274 |
# Overwrite all other labels |
| 275 |
else { |
| 276 |
$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 |
$info->{$symbol,$label} = ''; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
return; |
| 286 |
|
| 287 |
} # end process_asx_data |
| 288 |
|
| 289 |
1; |
| 290 |
__END__ |
| 291 |
|
| 292 |
=head1 NAME |
| 293 |
|
| 294 |
Finance::Quote::ASX - Obtain quotes from the Australian Stock Exchange. |
| 295 |
|
| 296 |
=head1 SYNOPSIS |
| 297 |
|
| 298 |
use Finance::Quote; |
| 299 |
$q = Finance::Quote->new; |
| 300 |
%stockinfo = $q->fetch("asx","BHP"); # Only query ASX. |
| 301 |
%stockinfo = $q->fetch("australia","BHP"); # Failover to other sources OK. |
| 302 |
|
| 303 |
=head1 DESCRIPTION |
| 304 |
|
| 305 |
This module obtains information from the Australian Stock Exchange |
| 306 |
http://www.asx.com.au/. Data for all Australian listed securities and indices |
| 307 |
is available. Indexes start with the letter 'X'. For example, the |
| 308 |
All Ordinaries is "XAO". But some securities also start with the letter 'X'. |
| 309 |
|
| 310 |
This module is loaded by default on a Finance::Quote object. It's |
| 311 |
also possible to load it explicitly by placing "ASX" in the argument |
| 312 |
list to Finance::Quote->new(). |
| 313 |
|
| 314 |
This module provides both the "asx" and "australia" fetch methods. |
| 315 |
Please use the "australia" fetch method if you wish to have failover |
| 316 |
with other sources for Australian stocks (such as Yahoo). Using |
| 317 |
the "asx" method will guarantee that your information only comes |
| 318 |
from the Australian Stock Exchange. |
| 319 |
|
| 320 |
Information returned by this module is governed by the Australian |
| 321 |
Stock Exchange's terms and conditions. |
| 322 |
|
| 323 |
=head1 LABELS RETURNED |
| 324 |
|
| 325 |
The following labels may be returned by Finance::Quote::ASX: |
| 326 |
bid, offer, open, high, low, last, net, p_change, volume, |
| 327 |
and price. |
| 328 |
|
| 329 |
=head1 SEE ALSO |
| 330 |
|
| 331 |
Australian Stock Exchange, http://www.asx.com.au/ |
| 332 |
|
| 333 |
Finance::Quote::Yahoo::Australia. |
| 334 |
=cut |