Advertisement
Guest User

ASX.pm

a guest
Aug 22nd, 2016
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 7.08 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. #
  3. #    Copyright (C) 1998, Dj Padzensky <djpadz@padz.net>
  4. #    Copyright (C) 1998, 1999 Linas Vepstas <linas@linas.org>
  5. #    Copyright (C) 2000, Yannick LE NY <y-le-ny@ifrance.com>
  6. #    Copyright (C) 2000, Brent Neal <brentn@users.sourceforge.net>
  7. #    Copyright (C) 2001, Leigh Wedding <leigh.wedding@telstra.com>
  8. #    Copyright (C) 2000-2004, Paul Fenwick <pjf@cpan.org>
  9. #    Copyright (C) 2014, Chris Good <chris.good@@ozemail.com.au>
  10. #
  11. #    This program is free software; you can redistribute it and/or modify
  12. #    it under the terms of the GNU General Public License as published by
  13. #    the Free Software Foundation; either version 2 of the License, or
  14. #    (at your option) any later version.
  15. #
  16. #    This program is distributed in the hope that it will be useful,
  17. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. #    GNU General Public License for more details.
  20. #
  21. #    You should have received a copy of the GNU General Public License
  22. #    along with this program; if not, write to the Free Software
  23. #    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  24. #    02111-1307, USA
  25. #
  26. #
  27. # This code derived from Padzensky's work on package Finance::YahooQuote,
  28. # but extends its capabilites to encompas a greater number of data sources.
  29. #
  30. # This code was developed as part of GnuCash <http://www.gnucash.org/>
  31.  
  32. require 5.005;
  33.  
  34. use strict;
  35.  
  36. package Finance::Quote::ASX;
  37.  
  38. use HTTP::Request::Common;
  39. use LWP::UserAgent;
  40. use HTML::TableExtract;
  41. use Encode;
  42.  
  43. use vars qw/$ASX_URL @ASX_SEC_CODES/;
  44.  
  45. # VERSION
  46.  
  47. $ASX_URL = 'http://www.asx.com.au/asx/markets/priceLookup.do?by=asxCodes&asxCodes=';
  48.  
  49. # These are the ASX codes starting with X that are securities not indexes
  50. #  and thus need currency AUD returned
  51. # See http://www.asx.com.au/asx/research/listedCompanies.do
  52. @ASX_SEC_CODES = (qw/XAM XIP XRO XPD XPE XF1 XRF XST XTD XTE XTV/);
  53.  
  54. sub methods {return (australia => \&asx,asx => \&asx)}
  55.  
  56. {
  57.     my @labels = qw/name last net p_change bid offer high low volume
  58.                     price method exchange/;
  59.  
  60.     sub labels { return (australia => \@labels,
  61.                          asx       => \@labels); }
  62. }
  63.  
  64. # Australian Stock Exchange (ASX)
  65. # The ASX provides free delayed quotes through their webpage.
  66. #
  67. # Maintainer of this section is Paul Fenwick <pjf@cpan.org>
  68. # 5-May-2001 Updated by Leigh Wedding <leigh.wedding@telstra.com>
  69. # 24-Feb-2014 Updated by Chris Good <chris.good@@ozemail.com.au>
  70.  
  71. sub asx {
  72.     my $quoter = shift;
  73.     my @all_stocks = @_;
  74.     return unless @all_stocks;
  75.     my @stocks;
  76.     my %info;
  77.  
  78.     my $ua = $quoter->user_agent;
  79.  
  80.     # ASX webpage only handles up to 10 quote requests at once
  81.  
  82.     while (@stocks = splice(@all_stocks, 0, 10)) {
  83.         my $response = $ua->request(GET $ASX_URL.join("%20",@stocks));
  84.         unless ($response->is_success) {
  85.             foreach my $stock (@stocks, @all_stocks) {
  86.                 $info{$stock,"success"} = 0;
  87.                 $info{$stock,"errormsg"} = "HTTP session failed";
  88.             }
  89.             return wantarray() ? %info : \%info;
  90.         }
  91.  
  92.         my $te = HTML::TableExtract->new(
  93.             automap => 0,
  94.             headers => ["Code", "Last", '\+/-', '% Chg', "Bid", "Offer",
  95.                     "Open", "High", "Low", "Vol"]);
  96.  
  97.         $te->parse(decode('utf-8',$response->content));
  98.  
  99.         # Extract table contents.
  100.         my @rows;
  101.         unless (($te->tables > 0) && ( @rows = $te->rows)) {
  102.             foreach my $stock (@stocks, @all_stocks) {
  103.                 $info{$stock,"success"} = 0;
  104.                 $info{$stock,"errormsg"} = "Failed to parse HTML table.";
  105.             }
  106.             return wantarray() ? %info : \%info;
  107.         }
  108.  
  109.         # Pack the resulting data into our structure.
  110.         foreach my $row (@rows) {
  111.             my $stock = shift(@$row);
  112.  
  113.             # Skip any blank lines.
  114.             next unless $stock;
  115.  
  116.             # Delete spaces and '*' which sometimes appears after the code.
  117.             # Also delete high bit characters.
  118.             $stock =~ tr/* \200-\377//d;
  119.  
  120.             # Delete any whitespace characters
  121.             $stock =~ s/\s//g;
  122.  
  123.             $info{$stock,'symbol'} = $stock;
  124.  
  125.             foreach my $label (qw/last net p_change bid offer open
  126.                       high low volume/) {
  127.                 $info{$stock,$label} = shift(@$row);
  128.  
  129.                 # Again, get rid of nasty high-bit characters.
  130.                 $info{$stock,$label} =~ tr/ \200-\377//d
  131.                     unless ($label eq "name");
  132.             }
  133.  
  134.             # If that stock does not exist, it will have a empty
  135.             # string for all the fields.  The "last" price should
  136.             # always be defined (even if zero), if we see an empty
  137.             # string here then we know we've found a bogus stock.
  138.  
  139.             if ($info{$stock,'last'} eq '') {
  140.                 $info{$stock,'success'} = 0;
  141.                 $info{$stock,'errormsg'}="Stock does not exist on ASX.";
  142.                 next;
  143.             }
  144.  
  145.             # Drop commas from volume.
  146.             $info{$stock,"volume"} =~ tr/,//d;
  147.  
  148.             # The ASX returns zeros for a number of things if there
  149.             # has been no trading.  This not only looks silly, but
  150.             # can break things later.  "correct" zero'd data.
  151.  
  152.             foreach my $label (qw/open high low/) {
  153.                 if ($info{$stock,$label} == 0) {
  154.                     $info{$stock,$label} = $info{$stock,"last"};
  155.                 }
  156.             }
  157.  
  158.             # Remove trailing percentage sign from p_change
  159.             $info{$stock,"p_change"} =~ tr/%//d;
  160.  
  161.             # Australian indexes all begin with X, so don't tag them
  162.             # as having currency info.
  163.  
  164.             $info{$stock, "currency"} = "AUD" unless ($stock =~ /^X/);
  165.            
  166.             # There are some companies starting with X, so DO tag
  167.             #  them with currency AUD
  168.  
  169.             if ( grep( /^$stock$/, @ASX_SEC_CODES ) ) {
  170.                 $info{$stock, "currency"} = "AUD";
  171.             }
  172.  
  173.             $info{$stock, "method"} = "asx";
  174.             $info{$stock, "exchange"} = "Australian Stock Exchange";
  175.             $info{$stock, "price"} = $info{$stock,"last"};
  176.             $info{$stock, "success"} = 1;
  177.         }
  178.     }
  179.  
  180.     # All done.
  181.  
  182.     return %info if wantarray;
  183.     return \%info;
  184. }
  185.  
  186. 1;
  187.  
  188. =head1 NAME
  189.  
  190. Finance::Quote::ASX - Obtain quotes from the Australian Stock Exchange.
  191.  
  192. =head1 SYNOPSIS
  193.  
  194.     use Finance::Quote;
  195.  
  196.     $q = Finance::Quote->new;
  197.  
  198.     %stockinfo = $q->fetch("asx","BHP");       # Only query ASX.
  199.     %stockinfo = $q->fetch("australia","BHP"); # Failover to other sources OK.
  200.  
  201. =head1 DESCRIPTION
  202.  
  203. This module obtains information from the Australian Stock Exchange
  204. http://www.asx.com.au/.  All Australian stocks and indicies are
  205. available.  Indexes start with the letter 'X'.  For example, the
  206. All Ordinaries is "XAO".
  207.  
  208. This module is loaded by default on a Finance::Quote object.  It's
  209. also possible to load it explicity by placing "ASX" in the argument
  210. list to Finance::Quote->new().
  211.  
  212. This module provides both the "asx" and "australia" fetch methods.
  213. Please use the "australia" fetch method if you wish to have failover
  214. with other sources for Australian stocks (such as Yahoo).  Using
  215. the "asx" method will guarantee that your information only comes
  216. from the Australian Stock Exchange.
  217.  
  218. Information returned by this module is governed by the Australian
  219. Stock Exchange's terms and conditions.
  220.  
  221. =head1 LABELS RETURNED
  222.  
  223. The following labels may be returned by Finance::Quote::ASX:
  224. date, bid, ask, open, high, low, last, close, p_change, volume,
  225. net and price.
  226.  
  227. =head1 SEE ALSO
  228.  
  229. Australian Stock Exchange, http://www.asx.com.au/
  230.  
  231. Finance::Quote::Yahoo::Australia.
  232.  
  233. =cut
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement