Quote/Sinvestor.pm

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