Debian Codegraph @

lib/Finance/Quote/NZX.pm

raw
1 #!/usr/bin/perl -w
2 # vi: set ts=4 sw=4 noai ic showmode showmatch:
3
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 # 02110-1301, USA
18
19 package Finance::Quote::NZX;
20
21 use strict;
22 use warnings;
23
24 use constant DEBUG => $ENV{DEBUG};
25 use if DEBUG, 'Smart::Comments';
26
27 use Encode;
28 use JSON qw( decode_json );
29 use LWP::UserAgent;
30 use Web::Scraper;
31 use String::Util qw(trim);
32
33 our $VERSION = '1.71'; # VERSION
34
35 our $DISPLAY = 'NZX - New Zealand Exchange';
36 our @LABELS = qw/last isin name currency date isodate/;
37 our $METHODHASH = {subroutine => \&nzx,
38 display => $DISPLAY,
39 labels => \@LABELS};
40
41 sub methodinfo {
42 return (
43 nzx => $METHODHASH,
44 );
45 }
46
47 sub labels { my %m = methodinfo(); return map {$_ => [@{$m{$_}{labels}}] } keys %m; }
48
49 sub methods {
50 my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m;
51 }
52
53 sub nzx {
54 my $quoter = shift;
55 my @symbols = @_;
56 my $ua = $quoter->user_agent();
57 my %info;
58
59 foreach my $symbol (@_) {
60 eval {
61 my $url = "https://www.nzx.com/instruments/$symbol";
62 my $reply = $ua->get($url);
63
64 unless ($reply->code == 200) {
65 $info{$symbol, 'success'} = 0;
66 $info{$symbol, 'errormsg'} = 'Symbol not found';
67 return;
68 }
69
70 # JSON inside script id="__NEXT_DATA__" type="application/json" crossorigin="">
71 my $widget = scraper {
72 process '//script[contains(@id, "__NEXT_DATA__")]/text()', "script" => 'TEXT';
73 };
74
75 my $result = $widget->scrape($reply->decoded_content);
76
77 ### RESULT : $result
78 ### [<now>] Result->script: $result->{script}
79
80 my $json = encode_utf8($result->{script});
81 my $json_data;
82 eval {$json_data = JSON::decode_json($json)};
83 if ($@) {
84 $info{ $symbol, 'success' } = 0;
85 $info{ $symbol, 'errormsg' } = $@;
86 }
87 ### [<now>] JSON Data: $json_data
88
89 unless ($json_data->{'props'}{'pageProps'}{'instrument'}{'tickerInstrumentInfo'}{'code'}
90 eq $symbol) {
91 $info{$symbol, 'success'} = 0;
92 $info{$symbol, 'errormsg'} = 'Symbol not found';
93 next;
94 }
95
96 $info{$symbol, 'success'} = 1;
97 $info{$symbol, 'currency'} = 'NZD';
98 $info{$symbol, 'symbol'} = $symbol;
99 $info{$symbol, 'last'} = $json_data->{'props'}{'pageProps'}{'instrument'}{'tickerInstrumentInfo'}{'lastPrice'};
100 $info{$symbol, 'isin'} = $json_data->{'props'}{'pageProps'}{'instrument'}{'instrumentInfo'}{'isin'};
101 $info{$symbol, 'name'} = $json_data->{'props'}{'pageProps'}{'instrument'}{'instrumentInfo'}{'name'};
102
103 my $closePriceDate = $json_data->{'props'}{'pageProps'}{'instrument'}{'tickerInstrumentInfo'}{'closePriceDate'};
104 $quoter->store_date(\%info, $symbol, {isodate => $closePriceDate});
105 };
106
107 if ($@) {
108 my $error = "Search failed: $@";
109 $info{$symbol, 'success'} = 0;
110 $info{$symbol, 'errormsg'} = trim($error);
111 }
112 }
113
114 ### info : %info
115
116 return wantarray() ? %info : \%info;
117 }
118
119 1;
120
121 __END__
122
123 =head1 NAME
124
125 Finance::Quote::NZX - Obtain quotes from New Zealand's
126 Exchange www.nzx.com
127
128 =head1 SYNOPSIS
129
130 use Finance::Quote;
131
132 $q = Finance::Quote->new;
133
134 %stockinfo = $q->fetch('nzx','MNW');
135
136 =head1 DESCRIPTION
137
138 This module obtains information from L<NZX|https://www.nzx.com/>.
139
140 =head1 LABELS RETURNED
141
142 The following labels may be returned by Finance::Quote::NZX:
143 last, isin, name, currency, date, isodate
144
145 =head1 Terms & Conditions
146
147 Use of nzx.com is governed by any terms & conditions of that site.
148
149 Finance::Quote is released under the GNU General Public License, version 2,
150 which explicitly carries a "No Warranty" clause.
151
152 =cut