Debian Codegraph @

lib/Finance/Quote/Tradegate.pm

raw
1 #!/usr/bin/perl -w
2 # This program is free software; you can redistribute it and/or modify
3 # it under the terms of the GNU General Public License as published by
4 # the Free Software Foundation; either version 2 of the License, or
5 # (at your option) any later version.
6 #
7 # This program is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # GNU General Public License for more details.
11 #
12 # You should have received a copy of the GNU General Public License
13 # along with this program; if not, write to the Free Software
14 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 # 02110-1301, USA
16
17 package Finance::Quote::Tradegate;
18
19 use strict;
20 use warnings;
21 use HTML::Entities;
22
23 use constant DEBUG => $ENV{DEBUG};
24 use if DEBUG, 'Smart::Comments';
25
26 use LWP::UserAgent;
27 use Web::Scraper;
28
29 our $VERSION = '1.71'; # VERSION
30
31 my $TRADEGATE_URL = 'https://web.s-investor.de/app/detail.htm?boerse=TDG&isin=';
32
33 our $DISPLAY = 'Tradegate';
34 # see https://web.s-investor.de/app/webauswahl.jsp for "Institutsliste"
35 our $FEATURES = {'INST_ID' => 'Institut Id (default: 0000057 for "Sparkasse Krefeld")' };
36 our @LABELS = qw/symbol isin last close exchange volume open price change p_change date time low high/;
37 our $METHODHASH = {subroutine => \&tradegate,
38 display => $DISPLAY,
39 labels => \@LABELS,
40 features => $FEATURES};
41
42 sub labels {
43 my %m = methodinfo(); return map {$_ => [@{$m{$_}{labels}}] } keys %m;
44 }
45
46 sub methodinfo {
47 return (
48 tradegate => $METHODHASH,
49 europe => $METHODHASH,
50 );
51 }
52
53 sub methods {
54 my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m;
55 }
56
57 sub tradegate {
58 my $quoter = shift;
59 my $inst_id = exists $quoter->{module_specific_data}->{tradegate}->{INST_ID} ?
60 $quoter->{module_specific_data}->{tradegate}->{INST_ID} :
61 '0000057';
62 my $ua = $quoter->user_agent();
63 my $agent = $ua->agent;
64 $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');
65
66 my %info;
67 my $url;
68 my $reply;
69
70 foreach my $symbol (@_) {
71 eval {
72 my $url = $TRADEGATE_URL
73 . $symbol
74 . '&INST_ID='
75 . $inst_id;
76
77 my $symlen = length($symbol);
78
79 my $tree = HTML::TreeBuilder->new_from_url($url);
80
81 my $lastvalue = $tree->look_down('class'=>'si_seitenbezeichnung');
82
83 if (defined($lastvalue)) {
84 $info{ $symbol, 'success' } = 0;
85 $info{ $symbol, 'errormsg' } = 'Invalid institute id. Get a valid institute id from https://web.s-investor.de/app/webauswahl.jsp';
86 } else {
87 $lastvalue = $tree->look_down('id'=>'kursdaten');
88
89 my $td1 = ($lastvalue->look_down('_tag'=>'td'))[1];
90 my @child = $td1->content_list;
91 my $isin = $child[0];
92
93 $td1 = ($lastvalue->look_down('_tag'=>'td'))[3];
94 @child = $td1->content_list;
95 my $sharename = $child[0];
96
97 $td1 = ($lastvalue->look_down('_tag'=>'td'))[5];
98 @child = $td1->content_list;
99 my $exchange = $child[0];
100
101 $td1 = ($lastvalue->look_down('_tag'=>'td'))[7];
102 @child = $td1->content_list;
103 my $raw = $child[0];
104 $raw =~ s/^\s+|\s+$//g;
105 my $date = substr($raw, 0, 8);
106 my $time = substr($raw, 9, 5); # CE(S)T
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 $table = ($lastvalue->look_down('_tag'=>'table'))[1];
127
128 $td1 = ($table->look_down('_tag'=>'td'))[1];
129 @child = $td1->content_list;
130 my $low = $child[0];
131 $low =~ s/\.//g;
132 $low =~ s/,/\./;
133
134 $td1 = ($table->look_down('_tag'=>'td'))[2];
135 @child = $td1->content_list;
136 my $high = $child[0];
137 $high =~ s/\.//g;
138 $high =~ s/,/\./;
139
140 my @searchvalue = $tree->look_down('class'=>'contentBox oneColum');
141 my $isFound = 0;
142 foreach (@searchvalue)
143 {
144 if (ref(($_->content_list)[0]) eq "HTML::Element" and ($_->content_list)[0]{'_content'}[0]{'_content'}[0] eq 'Aktuelle Vergleichszahlen')
145 {
146 $isFound = 1;
147
148 #-- open
149 $td1 = ($_->look_down('_tag'=>'td'))[4];
150 @child = $td1->content_list;
151 my $open = $child[0];
152 $open =~ s/\.//g;
153 $open =~ s/,/\./;
154
155 #-- change (absolute change)
156 $td1 = ($_->look_down('_tag'=>'td'))[13];
157 @child = $td1->content_list;
158 my $change = $child[0];
159 $change =~ s/\.//g;
160 $change =~ s/,/\./;
161 my $encchange = encode_entities($change);
162 my @splitcchange= split ('&',$encchange);
163 $change = $splitcchange[0];
164
165 #-- p_change (relative change)
166 $td1 = ($_->look_down('_tag'=>'td'))[16];
167 @child = $td1->content_list;
168 my $p_change =$child[0];
169 $p_change =~ s/[\.|%]//g;
170 $p_change =~ s/,/\./;
171
172 #-- close
173 $td1 = ($_->look_down('_tag'=>'td'))[34];
174 @child = $td1->content_list;
175 my $close = $child[0];
176 $close =~ s/\.//g;
177 $close =~ s/,/\./;
178 my $encclose = encode_entities($close);
179 my @splitclose= split ('&',$encclose);
180 $close = $splitclose[0];
181
182 $info{$symbol, 'success'} = 1;
183 $info{$symbol, 'method'} = 'Tradegate';
184 $info{$symbol, 'symbol'} = $isin;
185 $info{$symbol, 'isin'} = $isin;
186 $info{$symbol, 'name'} = $sharename;
187 $info{$symbol, 'exchange'} = $exchange;
188 $info{$symbol, 'last'} = $price;
189 $info{$symbol, 'price'} = $price;
190 $info{$symbol, 'close'} = $close;
191 $info{$symbol, 'change'} = $change;
192 $info{$symbol, 'p_change'} = $p_change;
193 $info{$symbol, 'volume'} = $volume;
194 $info{$symbol, 'currency'} = $currency;
195 #$info{$symbol, 'date'} = $date;
196 $quoter->store_date(\%info, $symbol, {eurodate => $date});
197 $info{$symbol, 'time'} = $time;
198 $info{$symbol, 'open'} = $open;
199 $info{$symbol, 'low'} = $low;
200 $info{$symbol, 'high'} = $high;
201 }
202 }
203
204 if (!$isFound)
205 {
206 $info{$symbol, 'success'} = 0;
207 $info{$symbol, 'errormsg'} = "Error retreiving $symbol: $@";
208 }
209 }
210 };
211 if ($@) {
212 $info{$symbol, 'success'} = 0;
213 $info{$symbol, 'errormsg'} = "Error retreiving $symbol: $@";
214 }
215
216
217 }
218 $ua->agent($agent);
219
220 return wantarray() ? %info : \%info;
221 }
222
223 1;
224
225 =head1 NAME
226
227 Finance::Quote::Tradegate - Obtain quotes from S-Investor platform.
228
229 =head1 SYNOPSIS
230
231 use Finance::Quote;
232
233 $q = Finance::Quote->new;
234 or
235 $q = Finance::Quote->new('Tradegate', 'tradegate' => {INST_ID => 'your institute id'});
236
237 %info = Finance::Quote->fetch("Tradegate", "DE000ENAG999"); # Only query Tradegate
238 %info = Finance::Quote->fetch("europe", "brd"); # Failover to other sources OK.
239
240 =head1 DESCRIPTION
241
242 This module fetches information from https://s-investor.de/, the investment platform
243 of the German Sparkasse banking group. It fetches share prices from tradegate,
244 a major German trading platform.
245
246 Suitable for shares and ETFs that are traded in Germany.
247
248 This module is loaded by default on a Finance::Quote object. It's also possible
249 to load it explicitly by placing "Tradegate" in the argument list to
250 Finance::Quote->new().
251
252 This module provides "Tradegate" and "europe" fetch methods.
253
254 Information obtained by this module may be covered by s-investor.de terms and
255 conditions.
256
257 =head1 INST_ID
258
259 https://s-investor.de/ supports different institute IDs. The default value "0000057" is
260 used (Krefeld) if no institute ID is provided. A list of institute IDs is provided here:
261 https://web.s-investor.de/app/webauswahl.jsp
262
263 The INST_ID may be set by providing a module specific hash to
264 Finance::Quote->new as in the above example (optional).
265
266 =head1 LABELS RETURNED
267
268 The following labels are returned:
269 currency
270 exchange
271 last
272 method
273 success
274 symbol
275 isin
276 date
277 time
278 volume
279 price
280 close
281 open
282 low
283 high
284 change
285 p_change