Debian Codegraph @

lib/Finance/Quote/XETRA.pm

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