| 1 |
#!/usr/bin/perl -w |
| 2 |
# vi: set ts=2 sw=2 noai expandtab ic showmode showmatch: |
| 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::Sinvestor; |
| 19 |
|
| 20 |
use strict; |
| 21 |
use warnings; |
| 22 |
use HTML::Entities; |
| 23 |
use String::Util qw(trim); |
| 24 |
|
| 25 |
use constant DEBUG => $ENV{DEBUG}; |
| 26 |
use if DEBUG, 'Smart::Comments'; |
| 27 |
|
| 28 |
use LWP::UserAgent; |
| 29 |
use Web::Scraper; |
| 30 |
use Encode qw(encode_utf8); |
| 31 |
|
| 32 |
our $VERSION = '1.71'; # VERSION |
| 33 |
|
| 34 |
my $SINVESTOR_URL = 'https://web.s-investor.de/app/detail.htm?isin='; |
| 35 |
|
| 36 |
our $DISPLAY = 'Sinvestor'; |
| 37 |
# see https://web.s-investor.de/app/webauswahl.jsp for "Institutsliste" |
| 38 |
our $FEATURES = {'INST_ID' => 'Institut Id (default: 0000057 for "Sparkasse Krefeld")', |
| 39 |
'EXCHANGE' => 'select market place (i.e. "gettex", "Xetra", "Tradegate")'}; |
| 40 |
our @LABELS = qw/symbol isin last close exchange exchanges volume open price change p_change date time low high ask bid source/; |
| 41 |
our $METHODHASH = {subroutine => \&sinvestor, |
| 42 |
display => $DISPLAY, |
| 43 |
labels => \@LABELS, |
| 44 |
features => $FEATURES}; |
| 45 |
|
| 46 |
sub labels { |
| 47 |
my %m = methodinfo(); return map {$_ => [@{$m{$_}{labels}}] } keys %m; |
| 48 |
} |
| 49 |
|
| 50 |
sub methodinfo { |
| 51 |
return ( |
| 52 |
sinvestor => $METHODHASH, |
| 53 |
europe => $METHODHASH, |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
sub methods { |
| 58 |
my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m; |
| 59 |
} |
| 60 |
|
| 61 |
sub strip_exchange_name { |
| 62 |
my $exchange_name = shift; |
| 63 |
$exchange_name =~ s/^(Zürich) - SWX$/$1/g; |
| 64 |
return $exchange_name; |
| 65 |
} |
| 66 |
|
| 67 |
sub get_utf8_string { |
| 68 |
return encode_utf8(shift); |
| 69 |
} |
| 70 |
|
| 71 |
sub get_de_number { |
| 72 |
my $number = shift; |
| 73 |
$number = trim($number); |
| 74 |
$number =~ s/\.//g; |
| 75 |
$number =~ s/,/\./; |
| 76 |
return ($number =~ /^([-+]?[0-9]+(\.[0-9]+)?)/) ? $1 : undef; |
| 77 |
} |
| 78 |
|
| 79 |
sub get_iso_currency { |
| 80 |
my ($currency) = @_; |
| 81 |
$currency =~ s/Euro/EUR/ and return $currency; |
| 82 |
$currency =~ s/Schweizer Franken/CHF/ and return $currency; |
| 83 |
$currency =~ s/US Dollar/USD/ and return $currency; |
| 84 |
$currency =~ s/^Taiwan-Dollar.*/TWD/ and return $currency; |
| 85 |
# TODO: add more currencies .. |
| 86 |
} |
| 87 |
|
| 88 |
sub td_search { |
| 89 |
my ($tree, $text) = @_; |
| 90 |
my $td = $tree->look_down('_tag'=>'td', sub { $_[0]->as_text eq $text }) or return; |
| 91 |
my @tds = $td->parent->look_down('_tag'=>'td'); |
| 92 |
return map { $tds[$_]->as_text } (1..$#tds); |
| 93 |
} |
| 94 |
|
| 95 |
sub sinvestor { |
| 96 |
my $quoter = shift; |
| 97 |
my $inst_id = exists $quoter->{module_specific_data}->{sinvestor}->{INST_ID} ? |
| 98 |
$quoter->{module_specific_data}->{sinvestor}->{INST_ID} : |
| 99 |
'0000057'; |
| 100 |
|
| 101 |
my $exchange_code = exists $quoter->{module_specific_data}->{sinvestor}->{EXCHANGE} ? |
| 102 |
$quoter->{module_specific_data}->{sinvestor}->{EXCHANGE} : |
| 103 |
undef; |
| 104 |
|
| 105 |
my %exchange2code = ( 'Gettex' => 'GTX', |
| 106 |
'Tradegate' => 'TDG', |
| 107 |
'Stuttgart' => 'STU', |
| 108 |
'Frankfurt' => 'FRA', |
| 109 |
'Xetra' => 'GER', |
| 110 |
'Paris' => 'PAR', |
| 111 |
"Düsseldorf" => 'DUS', |
| 112 |
'Berlin' => 'BER', |
| 113 |
'Hamburg' => 'HAM', |
| 114 |
'Hannover' => 'HAN', |
| 115 |
"München" => 'MUN', |
| 116 |
'Refinitiv CT' => 'RCT', |
| 117 |
'Zürich' => 'SWX', |
| 118 |
'Zürich - SWX' => 'SWX', |
| 119 |
'NASDAQ - Pink Sheets' => 'PNK', |
| 120 |
# undef means: this exchange is not selectable by name |
| 121 |
'Quotrix' => undef, # '0QT', 'QTX', |
| 122 |
'KVG Fondskurse' => undef, # 'LIP', |
| 123 |
'Lipper Fondsdaten' => undef, # 'LIP' |
| 124 |
); |
| 125 |
|
| 126 |
my %exchange2code_uc = ( map { uc($_) => $exchange2code{$_} } keys %exchange2code ); |
| 127 |
|
| 128 |
if (defined($exchange_code) and $exchange_code !~ /^[A-Z0-9]{3}$/) { |
| 129 |
# we need the exchange_code for the querry |
| 130 |
if (exists $exchange2code_uc{uc($exchange_code)} |
| 131 |
and defined $exchange2code_uc{uc($exchange_code)}) { |
| 132 |
$exchange_code = $exchange2code_uc{uc($exchange_code)}; |
| 133 |
} else { |
| 134 |
die("unsupported exchange(-code): $exchange_code"); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
my $ua = $quoter->user_agent(); |
| 139 |
my $agent = $ua->agent; |
| 140 |
$ua->agent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'); |
| 141 |
|
| 142 |
my %info; |
| 143 |
|
| 144 |
foreach my $symbol (@_) { |
| 145 |
my $url = $SINVESTOR_URL |
| 146 |
. $symbol |
| 147 |
. '&INST_ID=' |
| 148 |
. $inst_id; |
| 149 |
|
| 150 |
$url .= '&boerse=' . $exchange_code if defined($exchange_code); |
| 151 |
|
| 152 |
eval { |
| 153 |
my $tree = HTML::TreeBuilder->new_from_url($url); |
| 154 |
### [<now>] tree: $tree |
| 155 |
|
| 156 |
my $lastvalue = $tree->look_down('class'=>'si_seitenbezeichnung'); |
| 157 |
|
| 158 |
if (defined($lastvalue)) { |
| 159 |
$info{ $symbol, 'success' } = 0; |
| 160 |
$info{ $symbol, 'errormsg' } = 'Invalid institute id. Get a valid institute id from https://web.s-investor.de/app/webauswahl.jsp'; |
| 161 |
} else { |
| 162 |
$lastvalue = $tree->look_down('id'=>'kursdaten') or die("Not found"); |
| 163 |
|
| 164 |
my $isin = get_utf8_string(td_search($lastvalue, 'ISIN')); |
| 165 |
my $sharename = get_utf8_string(td_search($lastvalue, 'Bezeichnung')); |
| 166 |
my $exchange = get_utf8_string(td_search($lastvalue, "B\xf6rse/Contributor")); |
| 167 |
|
| 168 |
my $price = get_de_number(td_search($lastvalue, 'Letzter Kurs')); |
| 169 |
|
| 170 |
my $currency = get_iso_currency(td_search($lastvalue, "W\xe4hrung")); |
| 171 |
|
| 172 |
# TODO: per trade or day? |
| 173 |
my $volume = get_de_number(td_search($lastvalue, 'Volumen/Trade')); |
| 174 |
#my $volume = get_de_number(td_search($lastvalue, 'Volumen/Tag')); |
| 175 |
|
| 176 |
my $table = ($tree->look_down('_tag'=>'table'))[1]; |
| 177 |
my ($bid, $ask) = map { get_de_number($_) } td_search($lastvalue, 'Kurs'); |
| 178 |
|
| 179 |
$table = $tree->look_down('_tag'=>'table', 'id'=>'detailHandelsplatz'); |
| 180 |
my %exchanges = (); |
| 181 |
foreach ($table->look_down('_tag'=>'tr', 'class'=>'si_click_nav rowLink')) { |
| 182 |
my $a = $_->attr('data-dest'); |
| 183 |
if ($a and $a =~ /boerse=([^&"]+)/) { |
| 184 |
my $code = $1; |
| 185 |
my $name = encode_utf8(($_->look_down('_tag'=>'td'))[0]->as_text); |
| 186 |
$exchanges{ $name } = $code; |
| 187 |
$name = strip_exchange_name($name); |
| 188 |
$exchanges{ $name } = $code if not exists $exchanges{ $name }; |
| 189 |
} |
| 190 |
} |
| 191 |
if (defined($exchange_code)) { |
| 192 |
my $key = strip_exchange_name($exchange); |
| 193 |
unless(exists($exchanges{ $key }) and $exchanges{ $key } eq $exchange_code) { |
| 194 |
die("$symbol not found on marketplace: $exchange_code"); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
my @searchvalue = $tree->look_down('class'=>'contentBox oneColum'); |
| 199 |
my $isFound = 0; |
| 200 |
foreach my $t (@searchvalue) |
| 201 |
{ |
| 202 |
if (ref(($t->content_list)[0]) eq "HTML::Element" and ($t->content_list)[0]{'_content'}[0]{'_content'}[0] eq 'Aktuelle Vergleichszahlen') |
| 203 |
{ |
| 204 |
$isFound = 1; |
| 205 |
$info{$symbol, 'success'} = 1; |
| 206 |
$info{$symbol, 'method'} = 'Sinvestor'; |
| 207 |
$info{$symbol, 'source'} = $url; |
| 208 |
$info{$symbol, 'symbol'} = $isin; |
| 209 |
$info{$symbol, 'isin'} = $isin; |
| 210 |
$info{$symbol, 'name'} = $sharename; |
| 211 |
$info{$symbol, 'volume'} = $volume; |
| 212 |
$info{$symbol, 'currency'} = $currency; |
| 213 |
$info{$symbol, 'ask'} = $ask; |
| 214 |
$info{$symbol, 'bid'} = $bid; |
| 215 |
$info{$symbol, 'exchange'} = $exchange; |
| 216 |
$info{$symbol, 'exchanges'} = [ grep { exists $exchange2code_uc{uc($_)} |
| 217 |
and defined $exchange2code_uc{uc($_)} |
| 218 |
} sort keys %exchanges ]; |
| 219 |
$info{$symbol, 'last'} = $price; |
| 220 |
$info{$symbol, 'price'} = $price; # TODO: useless label (and not specified in README.md) |
| 221 |
$info{$symbol, 'close'} = get_de_number(td_search($t, 'VT-Schluss')); |
| 222 |
$info{$symbol, 'change'} = get_de_number(td_search($t, 'Diff.')); |
| 223 |
$info{$symbol, 'p_change'} = get_de_number(td_search($t, 'Diff.%')); |
| 224 |
$info{$symbol, 'open'} = get_de_number(td_search($t, "Er\xf6ffnung")); |
| 225 |
$info{$symbol, 'low'} = get_de_number(td_search($t, 'Tief')); |
| 226 |
$info{$symbol, 'high'} = get_de_number(td_search($t, 'Hoch')); |
| 227 |
|
| 228 |
my ($child) = td_search($t, 'Datum/Zeit'); |
| 229 |
my $date = substr(trim($child), 0, 8); |
| 230 |
my $time = substr(trim($child), 9, 5); # CE(S)T |
| 231 |
$quoter->store_date(\%info, $symbol, {eurodate => $date}); |
| 232 |
$info{$symbol, 'time'} = $1 if $time =~ /^([0-9]{2}:[0-9]{2})/; |
| 233 |
|
| 234 |
if (DEBUG) { |
| 235 |
my %unknown_exchanges = map { $_ => $exchanges{$_} } |
| 236 |
grep { not exists $exchange2code_uc{uc($_)} } |
| 237 |
sort keys %exchanges; |
| 238 |
### unknown_exchanges: %unknown_exchanges |
| 239 |
$info{$symbol, '_unknown_exchanges'} = { %unknown_exchanges }; |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
if (!$isFound) |
| 245 |
{ |
| 246 |
$info{$symbol, 'success'} = 0; |
| 247 |
$info{$symbol, 'errormsg'} = "Error retreiving $symbol: $@"; |
| 248 |
} |
| 249 |
} |
| 250 |
}; |
| 251 |
if ($@) { |
| 252 |
$info{$symbol, 'success'} = 0; |
| 253 |
$info{$symbol, 'errormsg'} = "Error retreiving $symbol: $@"; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
} |
| 258 |
$ua->agent($agent); |
| 259 |
|
| 260 |
return wantarray() ? %info : \%info; |
| 261 |
} |
| 262 |
|
| 263 |
1; |
| 264 |
|
| 265 |
=head1 NAME |
| 266 |
|
| 267 |
Finance::Quote::Sinvestor - Obtain quotes from S-Investor platform. |
| 268 |
|
| 269 |
=head1 SYNOPSIS |
| 270 |
|
| 271 |
use Finance::Quote; |
| 272 |
|
| 273 |
$q = Finance::Quote->new; |
| 274 |
or |
| 275 |
$q = Finance::Quote->new('Sinvestor', 'sinvestor' => {INST_ID => 'your institute id'}); |
| 276 |
or |
| 277 |
$q = Finance::Quote->new('Sinvestor', 'sinvestor' => {EXCHANGE => 'Xetra'}); |
| 278 |
|
| 279 |
%info = Finance::Quote->fetch("Sinvestor", "DE000ENAG999"); # Only query Sinvestor |
| 280 |
%info = Finance::Quote->fetch("europe", "brd"); # Failover to other sources OK. |
| 281 |
|
| 282 |
=head1 DESCRIPTION |
| 283 |
|
| 284 |
This module fetches information from https://s-investor.de/, the investment platform |
| 285 |
of the German Sparkasse banking group. It fetches share prices from various |
| 286 |
marketplaces. The marketplace is returned in the "exchange" field. |
| 287 |
|
| 288 |
Suitable for shares, ETFs and funds that are traded in Germany. |
| 289 |
|
| 290 |
This module is loaded by default on a Finance::Quote object. It's also possible |
| 291 |
to load it explicitly by placing "Sinvestor" in the argument list to |
| 292 |
Finance::Quote->new(). |
| 293 |
|
| 294 |
This module provides "Sinvestor" and "europe" fetch methods. |
| 295 |
|
| 296 |
Information obtained by this module may be covered by s-investor.de terms and |
| 297 |
conditions. |
| 298 |
|
| 299 |
=head1 EXCHANGE |
| 300 |
|
| 301 |
https://www.s-investor.de/ supports different market places. A default is not specified. |
| 302 |
|
| 303 |
"Xetra" alias "GER" |
| 304 |
"Tradegate" alias "TDG" |
| 305 |
"gettex" alias "GTX" |
| 306 |
"Berlin" alias "BER" |
| 307 |
... any many more ... |
| 308 |
|
| 309 |
The EXCHANGE may be set by providing a module specific hash to |
| 310 |
Finance::Quote->new as in the above example (optional). |
| 311 |
|
| 312 |
=head1 INST_ID |
| 313 |
|
| 314 |
https://s-investor.de/ supports different institute IDs. The default value "0000057" is |
| 315 |
used (Krefeld) if no institute ID is provided. A list of institute IDs is provided here: |
| 316 |
https://web.s-investor.de/app/webauswahl.jsp |
| 317 |
|
| 318 |
The INST_ID may be set by providing a module specific hash to |
| 319 |
Finance::Quote->new as in the above example (optional). |
| 320 |
|
| 321 |
=head1 LABELS RETURNED |
| 322 |
|
| 323 |
The following labels are returned: |
| 324 |
currency |
| 325 |
exchange |
| 326 |
exchanges |
| 327 |
last |
| 328 |
method |
| 329 |
source |
| 330 |
success |
| 331 |
symbol |
| 332 |
isin |
| 333 |
date |
| 334 |
time |
| 335 |
volume |
| 336 |
price |
| 337 |
close |
| 338 |
open |
| 339 |
low |
| 340 |
high |
| 341 |
change |
| 342 |
p_change |
| 343 |
ask |
| 344 |
bid |