Quote/XETRA.pm

filter: Statementstmt 29.8% 40 / 134 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.3% 12 / 13 Podpod 0.0% 0 / 5 totaltotal 31.3% 52 / 166 CC 10 SCAR 39.2
CC
10
Cov
27%
xetra
110.0
BEGIN
1.0
BEGIN
1.0
CRAP 50.5
« Quote/TSP.pm ↑ Summary Quote/Finanzpartner.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::XETRA;
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  
31 5 our $VERSION = '1.72_02'; # TRIAL VERSION
32  
33 5 my $xetra_URL = 'https://web.s-investor.de/app/detail.htm?boerse=GER&isin=';
34  
35 5 our $DISPLAY = 'XETRA - German Sparkasse banking group';
36 5 our $FEATURES = {'INST_ID' => 'Required Institution ID'};
37 5 our @LABELS = qw/symbol last close exchange volume open price change p_change/;
38 5 our $METHODHASH = {subroutine => \&xetra,
39 display => $DISPLAY,
40 labels => \@LABELS,
41 features => $FEATURES};
42  
43 10 ▶sub methodinfo {
Subroutine methodinfo: called CC=1 SCAR=0.0
Pod undocumented
44 return (
45 10 xetra => $METHODHASH,
46 europe => $METHODHASH,
47 germany => $METHODHASH,
48 );
49 }
50  
51 5 ▶sub labels { my %m = methodinfo(); return map {$_ => [@{$m{$_}{labels}}] } keys %m; }
Subroutine labels: called CC=1 SCAR=0.0
Pod undocumented
52  
53 5 ▶sub methods {
Subroutine methods: called CC=1 SCAR=0.0
Pod undocumented
54 5 my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m;
55 }
56  
57 1 ▶sub parameters {
Subroutine parameters: called CC=1 SCAR=0.0
Pod undocumented
58 1 return ('INST_ID');
59 }
60  
61 0 ▶sub xetra {
Subroutine xetra: not called CC=10 SCAR=47.0
Pod undocumented
62 0 my $quoter = shift;
63 my $inst_id = exists $quoter->{module_specific_data}->{xetra}->{INST_ID} ?
64 $quoter->{module_specific_data}->{xetra}->{INST_ID} :
65 0 ▶ '0000057';
Branch: 1 branch
BranchTrueFalseTotal
exists $quoter->{'module_specific_data'}{'xetra'}{'INST_ID'} ? : 0 0 0
66 0 my $ua = $quoter->user_agent();
67 0 my $agent = $ua->agent;
68 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');
69  
70 0 my %info;
71 my $url;
72 0 my $reply;
73  
74 0 foreach my $symbol (@_) {
75 0 eval {
76 0 my $url = $xetra_URL
77 . $symbol
78 . '&INST_ID='
79 . $inst_id;
80  
81 0 my $symlen = length($symbol);
82  
83 0 my $tree = HTML::TreeBuilder->new_from_url($url);
84  
85 0 my $lastvalue = $tree->look_down('class'=>'si_seitenbezeichnung');
86  
87 0 ▶ if (defined($lastvalue)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lastvalue) { } 0 0 0
88 0 $info{ $symbol, 'success' } = 0;
89 0 $info{ $symbol, 'errormsg' } = 'Invalid institute id. Get a valid institute id from https://web.s-investor.de/app/webauswahl.jsp';
90 } else {
91 0 $lastvalue = $tree->look_down('id'=>'kursdaten');
92  
93 0 my $td1 = ($lastvalue->look_down('_tag'=>'td'))[1];
94 0 my @child = $td1->content_list;
95 0 my $isin = $child[0];
96  
97 0 $td1 = ($lastvalue->look_down('_tag'=>'td'))[3];
98 0 @child = $td1->content_list;
99 0 my $sharename = $child[0];
100  
101 0 $td1 = ($lastvalue->look_down('_tag'=>'td'))[5];
102 0 @child = $td1->content_list;
103 0 my $exchange = $child[0];
104  
105 0 $td1 = ($lastvalue->look_down('_tag'=>'td'))[7];
106 0 @child = $td1->content_list;
107 0 my $date = substr(trim($child[0]), 0, 8);
108  
109 0 $td1 = ($lastvalue->look_down('_tag'=>'td'))[9];
110 0 @child = $td1->content_list;
111 0 my $price = $child[0];
112 0 $price =~ s/\.//g;
113 0 $price =~ s/,/\./;
114 0 my $encprice = encode_entities($price);
115 0 my @splitprice= split ('&',$encprice);
116 0 $price = $splitprice[0];
117  
118 0 $td1 = ($lastvalue->look_down('_tag'=>'td'))[11];
119 0 @child = $td1->content_list;
120 0 my $currency = $child[0];
121 0 $currency =~ s/Euro/EUR/;
122  
123 0 $td1 = ($lastvalue->look_down('_tag'=>'td'))[13];
124 0 @child = $td1->content_list;
125 0 my $volume = $child[0];
126  
127 0 my @searchvalue = $tree->look_down('class'=>'contentBox oneColum');
128 0 my $isFound = 0;
129 0 foreach (@searchvalue)
130 {
131 # Get number of child elements.
132 # Skip this tree member
133 0 my $num_elements = $_->content_list();
134 0 ▶ next unless $num_elements > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $num_elements > 1 0 0 0
135 0 ▶ if (($_->content_list)[0]{'_content'}[0]{'_content'}[0] eq 'Aktuelle Vergleichszahlen')
Branch: 1 branch
BranchTrueFalseTotal
if (+($_->content_list)[0]->{"_content"}[0]{"_content"}[0] eq "Aktuelle Vergleichszahlen") 0 0 0
136 {
137 0 $isFound = 1;
138  
139 #-- change (absolute change)
140 0 $td1 = ($_->look_down('_tag'=>'td'))[13];
141 0 @child = $td1->content_list;
142 0 my $change = $child[0];
143 0 $change =~ s/\.//g;
144 0 $change =~ s/,/\./;
145 0 my $encchange = encode_entities($change);
146 0 my @splitcchange= split ('&',$encchange);
147 0 $change = $splitcchange[0];
148  
149 #-- p_change (relative change)
150 0 $td1 = ($_->look_down('_tag'=>'td'))[16];
151 0 @child = $td1->content_list;
152 0 my $p_change =$child[0];
153 0 $p_change =~ s/[\.|%]//g;
154 0 $p_change =~ s/,/\./;
155  
156 #-- close
157 0 $td1 = ($_->look_down('_tag'=>'td'))[34];
158 0 @child = $td1->content_list;
159 0 my $close = $child[0];
160 0 $close =~ s/\.//g;
161 0 $close =~ s/,/\./;
162 0 my $encclose = encode_entities($close);
163 0 my @splitclose= split ('&',$encclose);
164 0 $close = $splitclose[0];
165  
166 0 $info{$symbol, 'success'} = 1;
167 0 $info{$symbol, 'method'} = 'xetra';
168 0 $info{$symbol, 'symbol'} = $isin;
169 0 $info{$symbol, 'name'} = $sharename;
170 0 $info{$symbol, 'exchange'} = $exchange;
171 0 $info{$symbol, 'last'} = $price;
172 0 $info{$symbol, 'price'} = $price;
173 0 $info{$symbol, 'close'} = $close;
174 0 $info{$symbol, 'change'} = $change;
175 0 $info{$symbol, 'p_change'} = $p_change;
176 0 $info{$symbol, 'volume'} = $volume;
177 0 $info{$symbol, 'currency'} = $currency;
178 #$info{$symbol, 'date'} = $date;
179 0 $quoter->store_date(\%info, $symbol, {eurodate => $date});
180 # leave foreach loop since we have the data
181 0 last;
182 }
183 }
184  
185 0 ▶ if (!$isFound)
Branch: 1 branch
BranchTrueFalseTotal
unless ($isFound) 0 0 0
186 {
187 0 $info{$symbol, 'success'} = 0;
188 0 $info{$symbol, 'errormsg'} = "Error retreiving $symbol: $@";
189 }
190 }
191 };
192 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
193 0 $info{$symbol, 'success'} = 0;
194 0 $info{$symbol, 'errormsg'} = "Error retreiving $symbol: $@";
195 }
196  
197  
198 }
199 0 $ua->agent($agent);
200  
201 0 ▶ return wantarray() ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
202 }
203  
204 5 1;
205  
206 =head1 NAME
207  
208 Finance::Quote::xetra - Obtain quotes from S-Investor platform.
209  
210 =head1 SYNOPSIS
211  
212 use Finance::Quote;
213  
214 $q = Finance::Quote->new;
215 or
216 $q = Finance::Quote->new('XETRA', 'xetra' => {INST_ID => 'your institute id'});
217  
218 %info = Finance::Quote->fetch("xetra", "DE000ENAG999"); # Only query xetra
219 %info = Finance::Quote->fetch("europe", "brd"); # Failover to other sources OK.
220  
221 =head1 DESCRIPTION
222  
223 This module fetches information from https://s-investor.de/, the investment platform
224 of the German Sparkasse banking group. It fetches share prices from XETRA,
225 a major German trading platform. The prices on XETRA serve as the basis for calculating
226 the DAX and other stock market indices.
227  
228 Suitable for shares and ETFs that are traded in Germany.
229  
230 This module is loaded by default on a Finance::Quote object. It's also possible
231 to load it explicitly by placing "XETRA" in the argument list to
232 Finance::Quote->new().
233  
234 This module provides "xetra" and "europe" fetch methods.
235  
236 Information obtained by this module may be covered by s-investor.de terms and
237 conditions.
238  
239 =head1 INST_ID
240  
241 https://s-investor.de/ supports different institute IDs. The default value "0000057" is
242 used (Krefeld) if no institute ID is provided. A list of institute IDs is provided here:
243 https://web.s-investor.de/app/webauswahl.jsp
244  
245 The INST_ID may be set by providing a module specific hash to
246 Finance::Quote->new as in the above example (optional).
247  
248 =head1 LABELS RETURNED
249  
250 The following labels are returned:
251 currency
252 exchange
253 last
254 method
255 success
256 symbol
257 volume
258 price
259 close
260 change
261 p_change
« Quote/TSP.pm ↑ Summary Quote/Finanzpartner.pm »