Quote/Tradegate.pm

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