Quote/Sinvestor.pm

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