Quote/NZX.pm

filter: Statementstmt 50.6% 40 / 79 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.7% 12 / 14 Podpod 0.0% 0 / 4 totaltotal 48.6% 52 / 107 CC 7 SCAR 30.1
CC
7
Cov
45%
nzx
56.0
__ANON__
2.0
BEGIN
1.0
CRAP 20.3
« Quote/CurrencyRates/UniRate.pm ↑ Summary Quote/CurrencyRates/Frankfurter.pm »
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 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 5 ▶use constant DEBUG => $ENV{DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶use if DEBUG, 'Smart::Comments';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 5 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 5 ▶use JSON qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
29 5 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 5 ▶use Web::Scraper;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 5 ▶use String::Util qw(trim);
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 5 our $VERSION = '1.72_02'; # TRIAL VERSION
34  
35 5 our $DISPLAY = 'NZX - New Zealand Exchange';
36 5 our @LABELS = qw/last isin name currency date isodate/;
37 5 our $METHODHASH = {subroutine => \&nzx,
38 display => $DISPLAY,
39 labels => \@LABELS};
40  
41 10 ▶sub methodinfo {
Subroutine methodinfo: called CC=1 SCAR=0.0
Pod undocumented
42 return (
43 10 nzx => $METHODHASH,
44 );
45 }
46  
47 5 ▶sub labels { my %m = methodinfo(); return map {$_ => [@{$m{$_}{labels}}] } keys %m; }
Subroutine labels: called CC=1 SCAR=0.0
Pod undocumented
48  
49 5 ▶sub methods {
Subroutine methods: called CC=1 SCAR=0.0
Pod undocumented
50 5 my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m;
51 }
52  
53 0 ▶sub nzx {
Subroutine nzx: not called CC=7 SCAR=40.3
Pod undocumented
54 0 my $quoter = shift;
55 0 my @symbols = @_;
56 0 my $ua = $quoter->user_agent();
57 0 my %info;
58  
59 0 foreach my $symbol (@_) {
60 0 eval {
61 0 my $url = "https://www.nzx.com/instruments/$symbol";
62 0 my $reply = $ua->get($url);
63  
64 0 ▶ unless ($reply->code == 200) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reply->code == 200) 0 0 0
65 0 $info{$symbol, 'success'} = 0;
66 0 $info{$symbol, 'errormsg'} = 'Symbol not found';
67 0 return;
68 }
69  
70 # JSON inside script id="__NEXT_DATA__" type="application/json" crossorigin="">
71 0 ▶ my $widget = scraper {
Subroutine __ANON__: not called CC=1 SCAR=6.9
72 0 process '//script[contains(@id, "__NEXT_DATA__")]/text()', "script" => 'TEXT';
73 0 };
74  
75 0 my $result = $widget->scrape($reply->decoded_content);
76  
77 ### RESULT : $result
78 ### [<now>] Result->script: $result->{script}
79  
80 0 my $json = encode_utf8($result->{script});
81 0 my $json_data;
82 0 eval {$json_data = JSON::decode_json($json)};
83 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
84 0 $info{ $symbol, 'success' } = 0;
85 0 $info{ $symbol, 'errormsg' } = $@;
86 }
87 ### [<now>] JSON Data: $json_data
88  
89 0 ▶ unless ($json_data->{'props'}{'pageProps'}{'instrument'}{'tickerInstrumentInfo'}{'code'}
Branch: 1 branch
BranchTrueFalseTotal
unless ($json_data->{"props"}{"pageProps"}{"instrument"}{"tickerInstrumentInfo"}{"code"} eq $symbol) 0 0 0
90 eq $symbol) {
91 0 $info{$symbol, 'success'} = 0;
92 0 $info{$symbol, 'errormsg'} = 'Symbol not found';
93 0 next;
94 }
95
96 0 $info{$symbol, 'success'} = 1;
97 0 $info{$symbol, 'currency'} = 'NZD';
98 0 $info{$symbol, 'symbol'} = $symbol;
99 0 $info{$symbol, 'last'} = $json_data->{'props'}{'pageProps'}{'instrument'}{'tickerInstrumentInfo'}{'lastPrice'};
100 0 $info{$symbol, 'isin'} = $json_data->{'props'}{'pageProps'}{'instrument'}{'instrumentInfo'}{'isin'};
101 0 $info{$symbol, 'name'} = $json_data->{'props'}{'pageProps'}{'instrument'}{'instrumentInfo'}{'name'};
102
103 0 my $closePriceDate = $json_data->{'props'}{'pageProps'}{'instrument'}{'tickerInstrumentInfo'}{'closePriceDate'};
104 0 $quoter->store_date(\%info, $symbol, {isodate => $closePriceDate});
105 };
106
107 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
108 0 my $error = "Search failed: $@";
109 0 $info{$symbol, 'success'} = 0;
110 0 $info{$symbol, 'errormsg'} = trim($error);
111 }
112 }
113
114 ### info : %info
115  
116 0 ▶ return wantarray() ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
117 }
118  
119 5 1;
120  
121 __END__
« Quote/CurrencyRates/UniRate.pm ↑ Summary Quote/CurrencyRates/Frankfurter.pm »