Quote/OnVista.pm

filter: Statementstmt 31.6% 45 / 142 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 92.8% 13 / 14 Podpod 0.0% 0 / 4 totaltotal 26.4% 58 / 219 CC 24 SCAR 59.6
CC
24
Cov
24%
onvista
600.0
BEGIN
1.0
BEGIN
1.0
CRAP 389.5
« Quote/YahooWeb.pm ↑ Summary Quote/Sinvestor.pm »
1 #!/usr/bin/perl -w
2 # vi: set ts=2 sw=2 noai expandtab ic showmode showmatch:
3 #
4 # Copyright (C) 2024, Bruce Schuck <bschuck@asgard-systems.com>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA
20 #
21 # 2024-10-06 Complete rewrite of module for F::Q issue #414
22  
23 package Finance::Quote::OnVista;
24  
25 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 5 ▶use Encode qw(encode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 5 ▶use HTML::TreeBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 5 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 5 ▶use JSON qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
32 5 ▶use Date::Parse qw(str2time);
Subroutine BEGIN: called CC=1 SCAR=0.0
33 5 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 5 ▶use constant DEBUG => $ENV{DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
36 5 ▶use if DEBUG, 'Smart::Comments', '###';
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 5 our $VERSION = '1.72'; # VERSION
39  
40 5 my $ONVISTA_URL = 'https://www.onvista.de/suche/';
41  
42 # Change DISPLAY and method values in code below
43 # Modify LABELS to those returned by the method
44  
45 5 our $DISPLAY = 'OnVista - Germany';
46 5 our $FEATURES = { 'EXCHANGE' => 'select market place (i.e. "GER", "Xetra", "Tradegate")' };
47 5 our @LABELS = qw/symbol isin wkn name open close high low last date volume currency exchange method ask bid change p_change time exchanges source/;
48 5 our $METHODHASH = {subroutine => \&onvista,
49 display => $DISPLAY,
50 labels => \@LABELS,
51 features => $FEATURES};
52  
53 10 ▶sub methodinfo {
Subroutine methodinfo: called CC=1 SCAR=0.0
Pod undocumented
54 return (
55 10 onvista => $METHODHASH,
56 germany => $METHODHASH,
57 );
58 }
59  
60 5 ▶sub labels {
Subroutine labels: called CC=1 SCAR=0.0
Pod undocumented
61 5 my %m = methodinfo();
62 5 return map {$_ => [@{$m{$_}{labels}}] } keys %m;
63 }
64  
65 5 ▶sub methods {
Subroutine methods: called CC=1 SCAR=0.0
Pod undocumented
66 5 my %m = methodinfo();
67 5 return map {$_ => $m{$_}{subroutine} } keys %m;
68 }
69  
70 0 ▶sub onvista {
Subroutine onvista: not called CC=24 SCAR=64.0
Pod undocumented
71  
72 0 my $quoter = shift;
73 0 my @stocks = @_;
74 0 my (%info, $tree, $url, $reply, $json, $json_decoded);
75 0 my $ua = $quoter->user_agent();
76  
77 0 foreach my $stock (@stocks) {
78  
79 0 $url = $ONVISTA_URL . $stock;
80 0 $reply = $ua->request( GET $url);
81  
82 0 my $code = $reply->code;
83 0 my $desc = HTTP::Status::status_message($code);
84 0 my $headers = $reply->headers_as_string;
85 0 my $body = $reply->decoded_content;
86  
87 ### Body: $body
88  
89 0 ▶ if ( $code == 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 200) { } 0 0 0
90  
91 # Use HTML::TreeBuilder to parse HTML in $body
92 0 $tree = HTML::TreeBuilder->new;
93 0 ▶ if ($tree->parse($body)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree->parse($body)) { } 0 0 0
94  
95 0 $tree->eof;
96 0 ▶ unless ( $json = encode_utf8 (($tree->look_down(_tag => 'script', id => '__NEXT_DATA__', type => 'application/json')->content_list())[0]) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json = &encode_utf8(scalar(($tree->look_down("_tag", "script", "id", "__NEXT_DATA__", "type", "application/json")->content_list)[0]))) 0 0 0
97 0 $info{ $stock, "success" } = 0;
98 0 $info{ $stock, "errormsg" } =
99 "Error retrieving quote for $stock. No data returned";
100 0 next;
101 }
102  
103 ### [<now>] JSON: $json
104  
105 0 $json_decoded = decode_json $json;
106 ### [<now>] JSON Decoded: $json_decoded
107  
108 0 my $result_array;
109 0 ▶ if ($json_decoded->{'props'}{'pageProps'}{'data'}{'snapshot'}{'instrument'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json_decoded->{'props'}{'pageProps'}{'data'}{'snapshot'}{'instrument'}) { } 0 0 0
110 0 $result_array = [ $json_decoded->{'props'}{'pageProps'}{'data'}{'snapshot'}{'instrument'} ];
111 } else {
112 0 $result_array = $json_decoded->{'props'}{'pageProps'}{'facets'}[0]{'results'};
113 }
114 ### [<now>] Result Array: $result_array
115 0 my $item;
116 0 foreach my $arrayitem ( @$result_array ) {
117 ### [<now>] ArrayItem: $arrayitem
118 0 ▶ if ( ($arrayitem->{'symbol'} && $arrayitem->{'symbol'} eq $stock)
Branch: 1 branch
BranchTrueFalseTotal
if ($arrayitem->{"symbol"} and $arrayitem->{"symbol"} eq $stock or $arrayitem->{"wkn"} and $arrayitem->{"wkn"} eq $stock or $arrayitem->{"isin"} and $arrayitem->{"isin"} eq $stock) 0 0 0
Condition0%0/15
$arrayitem->{"symbol"} and $arrayitem->{"symbol"} eq $stock or $arrayitem->{"wkn"} and $arrayitem->{"wkn"} eq $stock or $arrayitem->{"isin"} and $arrayitem->{"isin"} eq $stock
l!l&&r!l&&!r
000
$arrayitem->{"symbol"} and $arrayitem->{"symbol"} eq $stock or $arrayitem->{"wkn"} and $arrayitem->{"wkn"} eq $stock
l!l&&r!l&&!r
000
$arrayitem->{"symbol"} and $arrayitem->{"symbol"} eq $stock
!ll&&!rl&&r
000
$arrayitem->{"wkn"} and $arrayitem->{"wkn"} eq $stock
!ll&&!rl&&r
000
$arrayitem->{"isin"} and $arrayitem->{"isin"} eq $stock
!ll&&!rl&&r
000
MC/DC0%0/6
$arrayitem->{"symbol"} and $arrayitem->{"symbol"} eq $stock or $arrayitem->{"wkn"} and $arrayitem->{"wkn"} eq $stock or $arrayitem->{"isin"} and $arrayitem->{"isin"} eq $stock
$arrayitem->{"symbol"} $arrayitem->{"symbol"} eq $stock $arrayitem->{"wkn"} $arrayitem->{"wkn"} eq $stock $arrayitem->{"isin"} $arrayitem->{"isin"} eq $stock
Truth table0%0/15
A and B or C and D or E and F
A= $arrayitem->{"symbol"}B= $arrayitem->{"symbol"} eq $stockC= $arrayitem->{"wkn"}D= $arrayitem->{"wkn"} eq $stockE= $arrayitem->{"isin"}F= $arrayitem->{"isin"} eq $stock
ABCDEFresult
11XXXX1
0X11XX1
1011XX1
0X0X111
0X10111
100X111
1010111
0X0X0X0
0X0X100
0X100X0
0X10100
100X0X0
100X100
10100X0
1010100
119 or ($arrayitem->{'wkn'} && $arrayitem->{'wkn'} eq $stock)
120 or ($arrayitem->{'isin'} && $arrayitem->{'isin'} eq $stock)
121 ) {
122 0 $item = $arrayitem;
123 0 last;
124 }
125 }
126  
127 # By default set URL to first in array
128 # For US stocks, the symbol may not match stock
129 0 ▶ $item ||= $result_array->[0];
Condition0%0/3
$item ||= $result_array->[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$item ||= $result_array->[0]
$item $result_array->[0]
Truth table0%0/3
A ||= B
A= $itemB= $result_array->[0]
ABresult
1X1
011
000
130 0 map { $info{ $stock, $_ } = $item->{$_} } qw(symbol wkn isin);
131 0 $url = $item->{'urls'}{'WEBSITE'};
132  
133 0 ▶ unless ( $url ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url) 0 0 0
134 0 $info{ $stock, "success" } = 0;
135 0 $info{ $stock, "errormsg" } = "No data found for $stock.";
136 0 next;
137 }
138  
139 ### [<now>] New URL: $url
140 0 $reply = $ua->request( GET $url);
141  
142 0 $code = $reply->code;
143 0 $desc = HTTP::Status::status_message($code);
144 0 $headers = $reply->headers_as_string;
145 0 $body = $reply->decoded_content;
146  
147 0 ▶ unless ( $code == 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code == 200) 0 0 0
148 0 $info{ $stock, "success" } = 0;
149 0 $info{ $stock, "errormsg" } = "Error accessing $url ($desc).";
150 0 next;
151 }
152  
153 # Create HTML::TreeBuilder object from 2nd URL's body
154 0 $tree = HTML::TreeBuilder->new;
155 0 ▶ unless ($tree->parse($body)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tree->parse($body)) 0 0 0
156 0 $info{ $stock, "success" } = 0;
157 0 $info{ $stock, "errormsg" } = "Error parsing HTML from $url.";
158 0 next;
159 }
160 0 $tree->eof;
161  
162 0 ▶ unless ( $json = encode_utf8(($tree->look_down(_tag => 'script', id => '__NEXT_DATA__', type => 'application/json')->content_list())[0]) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json = &encode_utf8(scalar(($tree->look_down("_tag", "script", "id", "__NEXT_DATA__", "type", "application/json")->content_list)[0]))) 0 0 0
163 0 $info{ $stock, "success" } = 0;
164 0 $info{ $stock, "errormsg" } =
165 "Error retrieving quote for $stock. No data returned";
166 0 next;
167 }
168  
169 ### [<now>] 2nd JSON: $json
170  
171 0 eval {$json_decoded = decode_json encode_utf8 $json};
172 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
173 0 $info{ $stock, 'success' } = 0;
174 0 $info{ $stock, 'errormsg' } = $@;
175 0 next;
176 }
177  
178 my $exchange = exists $quoter->{module_specific_data}->{onvista}->{EXCHANGE} ?
179 0 ▶ $quoter->{module_specific_data}->{onvista}->{EXCHANGE} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $quoter->{'module_specific_data'}{'onvista'}{'EXCHANGE'} ? : 0 0 0
180  
181 0 my $markets = $json_decoded->{'props'}{'pageProps'}{'data'}{'snapshot'}{'quoteList'}{'list'};
182 0 $info{ $stock, 'exchanges' } = [ map { $_->{'market'}{'name'} } @$markets ];
183  
184 0 my $json_quote;
185 0 ▶ if ($exchange) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exchange) { } 0 0 0
186 0 ($json_quote) = grep { $_->{'market'}{'name'} eq $exchange
187 0 ▶ or $_->{'market'}{'codeExchange'} eq $exchange
Condition0%0/3
$_->{'market'}{'name'} eq $exchange || $_->{'market'}{'codeExchange'} eq $exchange
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->{'market'}{'name'} eq $exchange || $_->{'market'}{'codeExchange'} eq $exchange
$_->{'market'}{'name'} eq $exchange $_->{'market'}{'codeExchange'} eq $exchange
Truth table0%0/3
A || B
A= $_->{'market'}{'name'} eq $exchangeB= $_->{'market'}{'codeExchange'} eq $exchange
ABresult
1X1
011
000
188 } @$markets;
189 0 ▶ unless($json_quote) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json_quote) 0 0 0
190 0 $info{ $stock, "success" } = 0;
191 0 $info{ $stock, "errormsg" } =
192 "Error retrieving quote for $stock. No data returned for $exchange";
193 0 next;
194 }
195 } else {
196 0 $json_quote = $json_decoded->{'props'}{'pageProps'}{'data'}{'snapshot'}{'quote'};
197 }
198  
199 ### [<now>] 2nd JSON Decoded: $json_decoded
200  
201 0 $info{ $stock, "success" } = 1;
202 0 $info{ $stock, 'method' } = 'onvista';
203 0 $info{ $stock, 'source' } = $url;
204 0 $info{ $stock, 'name' } = $json_decoded->{'props'}{'pageProps'}{'data'}{'snapshot'}{'instrument'}{'name'};
205  
206 0 map { $info{ $stock, $_ } = $json_quote->{$_} } qw(open high low last volume ask bid);
207 0 $info{ $stock, 'price' } = $json_quote->{'last'};
208 0 $info{ $stock, 'currency' } = $json_quote->{'isoCurrency'};
209 0 $info{ $stock, 'exchange' } = $json_quote->{'market'}{'name'};
210 0 $info{ $stock, 'close' } = $json_quote->{'previousLast'};
211 0 $info{ $stock, 'change' } = $json_quote->{'performance'};
212 0 $info{ $stock, 'p_change' } = $json_quote->{'performancePct'};
213 0 $quoter->store_date(\%info, $stock, {isodate => substr $json_quote->{'datetimeLast'}, 0, 10});
214  
215 #$info{ $stock, 'time' } = substr $date, 11, 5; # UTC
216 0 my $utc_timestamp = str2time($json_quote->{'datetimeLast'});
217 0 $info{ $stock, 'time' } = strftime("%H:%M", localtime($utc_timestamp)); # local time zone
218 } else {
219 0 $tree->eof;
220 0 $info{ $stock, "success" } = 0;
221 0 $info{ $stock, "errormsg" } =
222 "Error retrieving quote for $stock. Could not parse HTML returned from $url.";
223 }
224  
225 } else { # HTTP Request failed (code != 200)
226 0 $info{ $stock, "success" } = 0;
227 0 $info{ $stock, "errormsg" } =
228 "Error retrieving quote for $stock. Attempt to fetch the URL $url resulted in HTTP response $code ($desc)";
229 }
230  
231 } # end foreach stock
232  
233 0 ▶ return wantarray() ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
234 0 return \%info;
235  
236 } # end onvista subroutine
237  
238 5 1;
239  
240 __END__
« Quote/YahooWeb.pm ↑ Summary Quote/Sinvestor.pm »