| 1 |
#!/usr/bin/perl -w |
| 2 |
# vi: set ts=2 sw=2 noai ic showmode showmatch: |
| 3 |
# |
| 4 |
# Copyright (C) 2024, Bruce Schuck <[email protected]> |
| 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 |
use strict; |
| 26 |
use warnings; |
| 27 |
|
| 28 |
use Encode qw(encode_utf8); |
| 29 |
use HTML::TreeBuilder; |
| 30 |
use HTTP::Request::Common; |
| 31 |
use JSON qw( decode_json ); |
| 32 |
use Date::Parse qw(str2time); |
| 33 |
use POSIX qw(strftime); |
| 34 |
|
| 35 |
use constant DEBUG => $ENV{DEBUG}; |
| 36 |
use if DEBUG, 'Smart::Comments', '###'; |
| 37 |
|
| 38 |
our $VERSION = '1.71'; # VERSION |
| 39 |
|
| 40 |
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 |
our $DISPLAY = 'OnVista - Germany'; |
| 46 |
our $FEATURES = { 'EXCHANGE' => 'select market place (i.e. "GER", "Xetra", "Tradegate")' }; |
| 47 |
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 |
our $METHODHASH = {subroutine => \&onvista, |
| 49 |
display => $DISPLAY, |
| 50 |
labels => \@LABELS, |
| 51 |
features => $FEATURES}; |
| 52 |
|
| 53 |
sub methodinfo { |
| 54 |
return ( |
| 55 |
onvista => $METHODHASH, |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
sub labels { my %m = methodinfo(); return map {$_ => [@{$m{$_}{labels}}] } keys %m; } |
| 60 |
|
| 61 |
sub methods { |
| 62 |
my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m; |
| 63 |
} |
| 64 |
|
| 65 |
sub onvista { |
| 66 |
|
| 67 |
my $quoter = shift; |
| 68 |
my @stocks = @_; |
| 69 |
my (%info, $tree, $url, $reply, $json, $json_decoded); |
| 70 |
my $ua = $quoter->user_agent(); |
| 71 |
|
| 72 |
foreach my $stock (@stocks) { |
| 73 |
|
| 74 |
$url = $ONVISTA_URL . $stock; |
| 75 |
$reply = $ua->request( GET $url); |
| 76 |
|
| 77 |
my $code = $reply->code; |
| 78 |
my $desc = HTTP::Status::status_message($code); |
| 79 |
my $headers = $reply->headers_as_string; |
| 80 |
my $body = $reply->decoded_content; |
| 81 |
|
| 82 |
### Body: $body |
| 83 |
|
| 84 |
if ( $code == 200 ) { |
| 85 |
|
| 86 |
# Use HTML::TreeBuilder to parse HTML in $body |
| 87 |
$tree = HTML::TreeBuilder->new; |
| 88 |
if ($tree->parse($body)) { |
| 89 |
|
| 90 |
$tree->eof; |
| 91 |
unless ( $json = encode_utf8 (($tree->look_down(_tag => 'script', id => '__NEXT_DATA__', type => 'application/json')->content_list())[0]) ) { |
| 92 |
$info{ $stock, "success" } = 0; |
| 93 |
$info{ $stock, "errormsg" } = |
| 94 |
"Error retrieving quote for $stock. No data returned"; |
| 95 |
next; |
| 96 |
} |
| 97 |
|
| 98 |
### [<now>] JSON: $json |
| 99 |
|
| 100 |
$json_decoded = decode_json $json; |
| 101 |
### [<now>] JSON Decoded: $json_decoded |
| 102 |
|
| 103 |
my $result_array; |
| 104 |
if ($json_decoded->{'props'}{'pageProps'}{'data'}{'snapshot'}{'instrument'}) { |
| 105 |
$result_array = [ $json_decoded->{'props'}{'pageProps'}{'data'}{'snapshot'}{'instrument'} ]; |
| 106 |
} else { |
| 107 |
$result_array = $json_decoded->{'props'}{'pageProps'}{'facets'}[0]{'results'}; |
| 108 |
} |
| 109 |
### [<now>] Result Array: $result_array |
| 110 |
my $item; |
| 111 |
foreach my $arrayitem ( @$result_array ) { |
| 112 |
### [<now>] ArrayItem: $arrayitem |
| 113 |
if ( ($arrayitem->{'symbol'} && $arrayitem->{'symbol'} eq $stock) |
| 114 |
or ($arrayitem->{'wkn'} && $arrayitem->{'wkn'} eq $stock) |
| 115 |
or ($arrayitem->{'isin'} && $arrayitem->{'isin'} eq $stock) |
| 116 |
) { |
| 117 |
$item = $arrayitem; |
| 118 |
last; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
# By default set URL to first in array |
| 123 |
# For US stocks, the symbol may not match stock |
| 124 |
$item ||= $result_array->[0]; |
| 125 |
map { $info{ $stock, $_ } = $item->{$_} } qw(symbol wkn isin); |
| 126 |
$url = $item->{'urls'}{'WEBSITE'}; |
| 127 |
|
| 128 |
unless ( $url ) { |
| 129 |
$info{ $stock, "success" } = 0; |
| 130 |
$info{ $stock, "errormsg" } = "No data found for $stock."; |
| 131 |
next; |
| 132 |
} |
| 133 |
|
| 134 |
### [<now>] New URL: $url |
| 135 |
$reply = $ua->request( GET $url); |
| 136 |
|
| 137 |
$code = $reply->code; |
| 138 |
$desc = HTTP::Status::status_message($code); |
| 139 |
$headers = $reply->headers_as_string; |
| 140 |
$body = $reply->decoded_content; |
| 141 |
|
| 142 |
unless ( $code == 200 ) { |
| 143 |
$info{ $stock, "success" } = 0; |
| 144 |
$info{ $stock, "errormsg" } = "Error accessing $url ($desc)."; |
| 145 |
next; |
| 146 |
} |
| 147 |
|
| 148 |
# Create HTML::TreeBuilder object from 2nd URL's body |
| 149 |
$tree = HTML::TreeBuilder->new; |
| 150 |
unless ($tree->parse($body)) { |
| 151 |
$info{ $stock, "success" } = 0; |
| 152 |
$info{ $stock, "errormsg" } = "Error parsing HTML from $url."; |
| 153 |
next; |
| 154 |
} |
| 155 |
$tree->eof; |
| 156 |
|
| 157 |
unless ( $json = encode_utf8(($tree->look_down(_tag => 'script', id => '__NEXT_DATA__', type => 'application/json')->content_list())[0]) ) { |
| 158 |
$info{ $stock, "success" } = 0; |
| 159 |
$info{ $stock, "errormsg" } = |
| 160 |
"Error retrieving quote for $stock. No data returned"; |
| 161 |
next; |
| 162 |
} |
| 163 |
|
| 164 |
### [<now>] 2nd JSON: $json |
| 165 |
|
| 166 |
eval {$json_decoded = decode_json encode_utf8 $json}; |
| 167 |
if($@) { |
| 168 |
$info{ $stock, 'success' } = 0; |
| 169 |
$info{ $stock, 'errormsg' } = $@; |
| 170 |
next; |
| 171 |
} |
| 172 |
|
| 173 |
my $exchange = exists $quoter->{module_specific_data}->{onvista}->{EXCHANGE} ? |
| 174 |
$quoter->{module_specific_data}->{onvista}->{EXCHANGE} : undef; |
| 175 |
|
| 176 |
my $markets = $json_decoded->{'props'}{'pageProps'}{'data'}{'snapshot'}{'quoteList'}{'list'}; |
| 177 |
$info{ $stock, 'exchanges' } = [ map { $_->{'market'}{'name'} } @$markets ]; |
| 178 |
|
| 179 |
my $json_quote; |
| 180 |
if ($exchange) { |
| 181 |
($json_quote) = grep { $_->{'market'}{'name'} eq $exchange |
| 182 |
or $_->{'market'}{'codeExchange'} eq $exchange |
| 183 |
} @$markets; |
| 184 |
unless($json_quote) { |
| 185 |
$info{ $stock, "success" } = 0; |
| 186 |
$info{ $stock, "errormsg" } = |
| 187 |
"Error retrieving quote for $stock. No data returned for $exchange"; |
| 188 |
next; |
| 189 |
} |
| 190 |
} else { |
| 191 |
$json_quote = $json_decoded->{'props'}{'pageProps'}{'data'}{'snapshot'}{'quote'}; |
| 192 |
} |
| 193 |
|
| 194 |
### [<now>] 2nd JSON Decoded: $json_decoded |
| 195 |
|
| 196 |
$info{ $stock, "success" } = 1; |
| 197 |
$info{ $stock, 'method' } = 'onvista'; |
| 198 |
$info{ $stock, 'source' } = $url; |
| 199 |
$info{ $stock, 'name' } = $json_decoded->{'props'}{'pageProps'}{'data'}{'snapshot'}{'instrument'}{'name'}; |
| 200 |
|
| 201 |
map { $info{ $stock, $_ } = $json_quote->{$_} } qw(open high low last volume ask bid); |
| 202 |
$info{ $stock, 'price' } = $json_quote->{'last'}; |
| 203 |
$info{ $stock, 'currency' } = $json_quote->{'isoCurrency'}; |
| 204 |
$info{ $stock, 'exchange' } = $json_quote->{'market'}{'name'}; |
| 205 |
$info{ $stock, 'close' } = $json_quote->{'previousLast'}; |
| 206 |
$info{ $stock, 'change' } = $json_quote->{'performance'}; |
| 207 |
$info{ $stock, 'p_change' } = $json_quote->{'performancePct'}; |
| 208 |
$quoter->store_date(\%info, $stock, {isodate => substr $json_quote->{'datetimeLast'}, 0, 10}); |
| 209 |
|
| 210 |
#$info{ $stock, 'time' } = substr $date, 11, 5; # UTC |
| 211 |
my $utc_timestamp = str2time($json_quote->{'datetimeLast'}); |
| 212 |
$info{ $stock, 'time' } = strftime("%H:%M", localtime($utc_timestamp)); # local time zone |
| 213 |
} else { |
| 214 |
$tree->eof; |
| 215 |
$info{ $stock, "success" } = 0; |
| 216 |
$info{ $stock, "errormsg" } = |
| 217 |
"Error retrieving quote for $stock. Could not parse HTML returned from $url."; |
| 218 |
} |
| 219 |
|
| 220 |
} else { # HTTP Request failed (code != 200) |
| 221 |
$info{ $stock, "success" } = 0; |
| 222 |
$info{ $stock, "errormsg" } = |
| 223 |
"Error retrieving quote for $stock. Attempt to fetch the URL $url resulted in HTTP response $code ($desc)"; |
| 224 |
} |
| 225 |
|
| 226 |
} # end foreach stock |
| 227 |
|
| 228 |
return wantarray() ? %info : \%info; |
| 229 |
return \%info; |
| 230 |
|
| 231 |
} # end onvista subroutine |
| 232 |
|
| 233 |
1; |
| 234 |
|
| 235 |
__END__ |
| 236 |
|
| 237 |
=head1 NAME |
| 238 |
|
| 239 |
Finance::Quote::OnVista - Obtain quotes from Frankfurt Stock Exchange. |
| 240 |
|
| 241 |
=head1 SYNOPSIS |
| 242 |
|
| 243 |
use Finance::Quote; |
| 244 |
|
| 245 |
$q = Finance::Quote->new; |
| 246 |
or |
| 247 |
$q = Finance::Quote->new("OnVista", "onvista" => { "EXCHANGE" => "Xetra" }); |
| 248 |
|
| 249 |
%info = $q->fetch("onvista", "sap"); # Only query onvista |
| 250 |
|
| 251 |
@exchanges = @{ $info{ "sap", "exchanges" } }; # List of available marketplaces |
| 252 |
|
| 253 |
=head1 DESCRIPTION |
| 254 |
|
| 255 |
This module fetches information from L<https://onvista.de/>. |
| 256 |
|
| 257 |
This module is loaded by default on a Finance::Quote object. It's also possible |
| 258 |
to load it explicitly by placing "onvista" in the argument list to |
| 259 |
Finance::Quote->new(). |
| 260 |
|
| 261 |
This module provides "onvista" fetch methods. It was written |
| 262 |
to replace a non-working Tradeville.pm module. |
| 263 |
|
| 264 |
Information obtained by this module may be covered by Frankfurt Stock |
| 265 |
Exchange terms and conditions. |
| 266 |
|
| 267 |
=head1 EXCHANGE |
| 268 |
|
| 269 |
https://onvista.de/ supports different market places. A default is not specified. |
| 270 |
|
| 271 |
"Xetra" alias "GER" |
| 272 |
"Tradegate" alias "GAT" |
| 273 |
"gettex" alias "TRO" |
| 274 |
"London Stock Exchange" alias "LSE" |
| 275 |
... any many more ... |
| 276 |
|
| 277 |
The EXCHANGE may be set by providing a module specific hash to |
| 278 |
Finance::Quote->new as in the above example (optional). |
| 279 |
|
| 280 |
=head1 LABELS RETURNED |
| 281 |
|
| 282 |
The following labels are returned: |
| 283 |
|
| 284 |
=over |
| 285 |
|
| 286 |
=item name |
| 287 |
|
| 288 |
=item symbol |
| 289 |
|
| 290 |
=item isin |
| 291 |
|
| 292 |
=item wkn |
| 293 |
|
| 294 |
=item open |
| 295 |
|
| 296 |
=item close |
| 297 |
|
| 298 |
=item high |
| 299 |
|
| 300 |
=item low |
| 301 |
|
| 302 |
=item price |
| 303 |
|
| 304 |
=item bid |
| 305 |
|
| 306 |
=item ask |
| 307 |
|
| 308 |
=item date |
| 309 |
|
| 310 |
=item time |
| 311 |
|
| 312 |
=item currency |
| 313 |
|
| 314 |
=item change |
| 315 |
|
| 316 |
=item p_change |
| 317 |
|
| 318 |
=item source |
| 319 |
|
| 320 |
=back |