File Coverage

blib/lib/Finance/Quote/NZX.pm
Criterion Covered Total %
statement 23 53 43.4
branch 0 10 0.0
condition n/a
subroutine 9 11 81.8
pod 0 3 0.0
total 32 77 41.5


line stmt bran cond sub pod time code
1             #!/usr/bin/perl -w
2              
3             # This program is free software; you can redistribute it and/or modify
4             # it under the terms of the GNU General Public License as published by
5             # the Free Software Foundation; either version 2 of the License, or
6             # (at your option) any later version.
7             #
8             # This program is distributed in the hope that it will be useful,
9             # but WITHOUT ANY WARRANTY; without even the implied warranty of
10             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11             # GNU General Public License for more details.
12             #
13             # You should have received a copy of the GNU General Public License
14             # along with this program; if not, write to the Free Software
15             # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16             # 02110-1301, USA
17              
18             package Finance::Quote::NZX;
19              
20 5     5   2632 use strict;
  5         18  
  5         158  
21 5     5   25 use warnings;
  5         11  
  5         186  
22              
23 5     5   28 use constant DEBUG => $ENV{DEBUG};
  5         10  
  5         402  
24 5     5   38 use if DEBUG, 'Smart::Comments';
  5         12  
  5         34  
25              
26 5     5   242 use LWP::UserAgent;
  5         11  
  5         41  
27 5     5   116 use Web::Scraper;
  5         10  
  5         35  
28 5     5   395 use String::Util qw(trim);
  5         10  
  5         3327  
29              
30             our $VERSION = '1.58'; # VERSION
31              
32             our @labels = qw/last isin name currency date isodate/;
33              
34             sub labels {
35 5     5 0 14 return ( nzx => \@labels );
36             }
37              
38             sub methods {
39 5     5 0 22 return ( nzx => \&nzx );
40             }
41              
42             sub nzx {
43 0     0 0   my $quoter = shift;
44 0           my @symbols = @_;
45 0           my $ua = $quoter->user_agent();
46 0           my %info;
47              
48 0           foreach my $symbol (@_) {
49 0           eval {
50 0           my $url = "https://www.nzx.com/instruments/$symbol";
51 0           my $reply = $ua->get($url);
52              
53             my $widget = scraper {
54 0     0     process '/html/body/section/div[2]/div/section[1]/div/div[1]/h1', 'last' => ['TEXT', sub{trim($_)}];
  0            
55 0           process '/html/body/section/div[2]/div/section[1]/div/div[2]/table/tbody/tr[5]/td[2]', 'isin' => ['TEXT', sub{trim($_)}];
  0            
56 0           process '/html/body/section/div[2]/div/section[1]/div/div[2]/table/tbody/tr[1]/td[2]', 'name' => ['TEXT', sub{trim($_)}];
  0            
57 0           process '/html/body/section/div[2]/div/div[2]/span', 'when' => ['TEXT', sub{trim($_)}];
  0            
58 0           };
59              
60 0           my $result = $widget->scrape($reply);
61             ### RESULT : $result
62              
63 0 0         die "Failed to find $symbol" unless exists $result->{last};
64            
65            
66 0           $info{$symbol, 'success'} = 1;
67 0           $info{$symbol, 'currency'} = 'NZD';
68 0 0         $info{$symbol, 'last'} = $1 if $result->{last} =~ /([0-9.]+)/;
69 0           $info{$symbol, 'isin'} = $result->{isin};
70 0           $info{$symbol, 'name'} = $result->{name};
71            
72 0 0         $quoter->store_date(\%info, $symbol, {eurodate => $1}) if $result->{when} =~ m|([0-9]{1,2}/[0-9]{1,2}/[0-9]{4})|;
73             };
74            
75 0 0         if ($@) {
76 0           my $error = "Search failed: $@";
77 0           $info{$symbol, 'success'} = 0;
78 0           $info{$symbol, 'errormsg'} = trim($error);
79             }
80             }
81            
82             ### info : %info
83              
84 0 0         return wantarray() ? %info : \%info;
85             }
86              
87             1;
88              
89             =head1 NAME
90              
91             Finance::Quote::NZX - Obtain quotes from New Zealand's
92             Exchange www.nzx.com
93              
94             =head1 SYNOPSIS
95              
96             use Finance::Quote;
97              
98             $q = Finance::Quote->new;
99              
100             %stockinfo = $q->fetch('nzx','TPW');
101              
102             =head1 DESCRIPTION
103              
104             This module obtains information fromwww.nzx.com.
105              
106             =head1 LABELS RETURNED
107              
108             The following labels may be returned by Finance::Quote::NZX:
109             last, isin, name, currency, date, isodate
110              
111             =head1 Terms & Conditions
112              
113             Use of nzx.com is governed by any terms & conditions of that site.
114              
115             Finance::Quote is released under the GNU General Public License, version 2,
116             which explicitly carries a "No Warranty" clause.
117              
118             =cut