Quote/Comdirect.pm

filter: Statementstmt 29.5% 41 / 139 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 92.3% 12 / 13 Podpod 0.0% 0 / 4 totaltotal 21.8% 53 / 243 CC 36 SCAR 69.1
CC
36
Cov
19%
comdirect
1332.0
BEGIN
1.0
BEGIN
1.0
CRAP 999.2
↑ Summary Quote/FTfunds.pm »
1 # vi: set ts=2 sw=2 noai expandtab ic showmode showmatch:
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::Comdirect;
18  
19 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 5 ▶use constant DEBUG => $ENV{DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 ▶use if DEBUG, 'Smart::Comments';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 5 ▶use HTML::TableExtract;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 ▶use HTML::TreeBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 5 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 5 ▶use String::Util qw(trim);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 5 ▶use Encode qw(encode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 5 our $VERSION = '1.72_01'; # TRIAL VERSION
32  
33 5 our $DISPLAY = 'Comdirect - Frankfurt and other exchanges';
34 5 our $FEATURES = { 'EXCHANGE' => 'select market place (i.e. "gettex", "Xetra", "Tradegate")' };
35 5 our @LABELS = qw/symbol name open high low last date time p_change ask bid currency isin wkn method exchange exchanges/;
36 5 our $METHODHASH = {subroutine => \&comdirect,
37 display => $DISPLAY,
38 labels => \@LABELS,
39 features => $FEATURES};
40  
41 10 ▶sub methodinfo {
Subroutine methodinfo: called CC=1 SCAR=0.0
Pod undocumented
42 return (
43 10 comdirect => $METHODHASH,
44 germany => $METHODHASH,
45 );
46 }
47  
48 5 ▶sub labels {
Subroutine labels: called CC=1 SCAR=0.0
Pod undocumented
49 5 my %m = methodinfo(); return map {$_ => [@{$m{$_}{labels}}] } keys %m;
50 }
51  
52 5 ▶sub methods {
Subroutine methods: called CC=1 SCAR=0.0
Pod undocumented
53 5 my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m;
54 }
55  
56  
57 0 ▶sub comdirect {
Subroutine comdirect: not called CC=36 SCAR=71.9
Pod undocumented
58 0 my $quoter = shift;
59 0 my @symbols = @_;
60 0 my $ua = $quoter->user_agent();
61 0 my (%info, %pricetable);
62  
63 # Issue #491 - Website needs cookies enabled.
64 0 $ua->cookie_jar(HTTP::Cookies->new);
65  
66 0 foreach my $symbol (@_) {
67 0 my %infotable;
68 0 my $try = 0;
69 0 my $url = 'https://www.comdirect.de/inf/search/all.html?SEARCH_VALUE=' . $symbol;
70  
71 0 RETRY:
72 ++$try;
73 0 my $reply = $ua->get($url);
74  
75 0 ▶ unless ($reply->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reply->is_success) 0 0 0
76 0 $info{ $symbol, "success" } = 0;
77 0 $info{ $symbol, "errormsg" } = join ' ', $reply->code, $reply->message;
78 0 next;
79 }
80 0 my $body = $reply->decoded_content;
81  
82 ### [<now>] Body: $body
83  
84 0 my $tree = HTML::TreeBuilder->new;
85 0 ▶ unless ($tree->parse($body)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tree->parse($body)) 0 0 0
86 0 $info{ $symbol, "success" } = 0;
87 0 $info{ $symbol, "errormsg" } = 'Parse body failed';
88 0 next;
89 }
90  
91 my $exchange = exists $quoter->{module_specific_data}->{comdirect}->{EXCHANGE} ?
92 0 ▶ $quoter->{module_specific_data}->{comdirect}->{EXCHANGE} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $quoter->{'module_specific_data'}{'comdirect'}{'EXCHANGE'} ? : 0 0 0
93  
94 0 my $select = $tree->look_down(_tag => 'select', name=> 'ID_NOTATION', id=> "marketSelect");
95 0 ▶ unless($select) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($select) 0 0 0
96 0 $info{ $symbol, "success" } = 0;
97 0 $info{ $symbol, "errormsg" } = 'Parse marketplaces failed.';
98 0 next;
99 }
100  
101 0 my %exchange2nid = map { encode_utf8($_->as_text) => $_->attr('value') }
102 0 ▶ grep { ref eq 'HTML::Element' and $_->tag eq 'option' } $select->content_list;
Condition0%0/3
ref $_ eq 'HTML::Element' && $_->tag eq 'option'
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_ eq 'HTML::Element' && $_->tag eq 'option'
ref $_ eq 'HTML::Element' $_->tag eq 'option'
Truth table0%0/3
A && B
A= ref $_ eq 'HTML::Element'B= $_->tag eq 'option'
ABresult
0X0
100
111
103  
104 0 my $option = $select->look_down(_tag => 'option', selected => 'selected');
105 0 ▶ unless($option) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($option) 0 0 0
106 0 $info{ $symbol, "success" } = 0;
107 0 $info{ $symbol, "errormsg" } = 'Parse selected marketplace failed.';
108 0 next;
109 }
110  
111 0 ▶ if ($exchange and $exchange ne encode_utf8($option->as_text)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exchange and $exchange ne &encode_utf8(scalar $option->as_text)) 0 0 0
Condition0%0/3
$exchange and $exchange ne &encode_utf8(scalar $option->as_text)
!ll&&!rl&&r
000
MC/DC0%0/2
$exchange and $exchange ne &encode_utf8(scalar $option->as_text)
$exchange $exchange ne &encode_utf8(scalar $option->as_text)
Truth table0%0/3
A and B
A= $exchangeB= $exchange ne &encode_utf8(scalar $option->as_text)
ABresult
0X0
100
111
112 0 ▶ unless (exists($exchange2nid{$exchange}) and $try < 2) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $exchange2nid{$exchange} and $try < 2) 0 0 0
Condition0%0/3
exists $exchange2nid{$exchange} and $try < 2
!ll&&!rl&&r
000
MC/DC0%0/2
exists $exchange2nid{$exchange} and $try < 2
exists $exchange2nid{$exchange} $try < 2
Truth table0%0/3
A and B
A= exists $exchange2nid{$exchange}B= $try < 2
ABresult
0X0
100
111
113 0 $info{ $symbol, "success" } = 0;
114 0 $info{ $symbol, "errormsg" } = 'Marketplace not found: ' . "'".$exchange."'";
115 0 next;
116 }
117  
118 0 my $u = $reply->request->url;
119 0 $u->query_param("ID_NOTATION" => $exchange2nid{$exchange});
120 0 $url = $u->as_string;
121  
122 0 goto RETRY;
123 }
124  
125 0 $info{$symbol, 'exchanges'} = [ sort keys %exchange2nid ];
126 0 $info{$symbol, 'exchange'} = encode_utf8($option->as_text);
127 0 $info{$symbol, 'notation_id'} = $option->attr('value');
128  
129 0 my $h1 = $tree->look_down(_tag => 'h1');
130 0 ▶ if ($h1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h1) 0 0 0
131 0 $info{$symbol, 'name'} = trim(encode_utf8($h1->as_text));
132 }
133  
134 0 my $div = $tree->look_down(_tag => 'div', class => "realtime-indicator");
135 0 ▶ if ($div) {
Branch: 1 branch
BranchTrueFalseTotal
if ($div) 0 0 0
136 0 my @span = $div->look_down(_tag => 'span');
137 0 ▶ if (scalar(@span) >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @span >= 2) 0 0 0
138 0 ▶ $info{$symbol, 'last'} = $1 if trim($span[-2]->as_text) =~ /^([0-9.]+)/;
Branch: 1 branch
BranchTrueFalseTotal
if trim($span[-2]->as_text) =~ /^([0-9.]+)/ 0 0 0
139 0 ▶ $info{$symbol, 'currency'} = $1 if trim($span[-1]->as_text) =~ /^([A-Z]+)/;
Branch: 1 branch
BranchTrueFalseTotal
if trim($span[-1]->as_text) =~ /^([A-Z]+)/ 0 0 0
140 }
141 }
142  
143 0 my $te = HTML::TableExtract->new( count => 4, attribs => { class => 'simple-table' } );
144 ### [<now>] TE: $te
145 0 ▶ unless ( $te->parse($body) and $te->first_table_found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($te->parse($body) and $te->first_table_found) 0 0 0
Condition0%0/3
$te->parse($body) and $te->first_table_found
!ll&&!rl&&r
000
MC/DC0%0/2
$te->parse($body) and $te->first_table_found
$te->parse($body) $te->first_table_found
Truth table0%0/3
A and B
A= $te->parse($body)B= $te->first_table_found
ABresult
0X0
100
111
146 0 $info{ $symbol, "success" } = 0;
147 0 $info{ $symbol, "errormsg" } = 'No price data found';
148 0 next;
149 }
150  
151 0 foreach my $row ($te->rows) {
152 ### [<now>] Row: $row
153 0 ▶ if (defined($row->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row->[0]) 0 0 0
154 0 $infotable{$row->[0]} = $row->[1];
155 }
156 }
157  
158 0 $te = HTML::TableExtract->new( count => 2, attribs => { class => 'simple-table' } );
159 ### [<now>] TE: $te
160 0 ▶ unless ( $te->parse($body) and $te->first_table_found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($te->parse($body) and $te->first_table_found) 0 0 0
Condition0%0/3
$te->parse($body) and $te->first_table_found
!ll&&!rl&&r
000
MC/DC0%0/2
$te->parse($body) and $te->first_table_found
$te->parse($body) $te->first_table_found
Truth table0%0/3
A and B
A= $te->parse($body)B= $te->first_table_found
ABresult
0X0
100
111
161 0 $info{ $symbol, "success" } = 0;
162 0 $info{ $symbol, "errormsg" } = 'No price data found';
163 0 next;
164 }
165  
166 0 foreach my $row ($te->rows) {
167 ### [<now>] Row: $row
168 0 ▶ if ($row->[0] eq 'Zeit' && $pricetable{'Zeit'}) {next}
Branch: 1 branch
BranchTrueFalseTotal
if ($row->[0] eq "Zeit" and $pricetable{"Zeit"}) 0 0 0
Condition0%0/3
$row->[0] eq "Zeit" and $pricetable{"Zeit"}
!ll&&!rl&&r
000
MC/DC0%0/2
$row->[0] eq "Zeit" and $pricetable{"Zeit"}
$row->[0] eq "Zeit" $pricetable{"Zeit"}
Truth table0%0/3
A and B
A= $row->[0] eq "Zeit"B= $pricetable{"Zeit"}
ABresult
0X0
100
111
169 0 $pricetable{$row->[0]} = trim($row->[1]);
170 }
171 ### [<now>] Pricetable hash: %pricetable
172  
173 0 ▶ unless (exists $pricetable{Zeit}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $pricetable{"Zeit"}) 0 0 0
174 0 $info{ $symbol, "success" } = 0;
175 0 $info{ $symbol, "errormsg" } = 'Parse failed.';
176 0 next;
177 }
178  
179 0 my %mapping = ( 'high' => 'Hoch',
180 'low' => 'Tief',
181 'bid' => 'Geld',
182 'ask' => 'Brief',
183 'open' => "Er\x{f6}ffnung",
184 'close' => "Schluss Vortag" );
185  
186 0 ▶ while ((my $fqkey, my $cbkey) = each (%mapping)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %mapping) 0 0 0
187 $info{$symbol, $fqkey} = $1.'.'.$2
188 if (exists($pricetable{$cbkey})
189 and defined($pricetable{$cbkey})
190 0 ▶ and $pricetable{$cbkey} =~ /^(\d+),(\d+)/);
Branch: 1 branch
BranchTrueFalseTotal
if exists $pricetable{$cbkey} and defined $pricetable{$cbkey} and $pricetable{$cbkey} =~ /^(\d+),(\d+)/ 0 0 0
Condition0%0/6
exists $pricetable{$cbkey} and defined $pricetable{$cbkey} and $pricetable{$cbkey} =~ /^(\d+),(\d+)/
!ll&&!rl&&r
000
exists $pricetable{$cbkey} and defined $pricetable{$cbkey}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $pricetable{$cbkey} and defined $pricetable{$cbkey} and $pricetable{$cbkey} =~ /^(\d+),(\d+)/
exists $pricetable{$cbkey} defined $pricetable{$cbkey} $pricetable{$cbkey} =~ /^(\d+),(\d+)/
Truth table0%0/4
A and B and C
A= exists $pricetable{$cbkey}B= defined $pricetable{$cbkey}C= $pricetable{$cbkey} =~ /^(\d+),(\d+)/
ABCresult
0XX0
10X0
1100
1111
191 }
192  
193 0 ▶ $info{$symbol, 'p_change'} = $1 if $pricetable{"Diff. Vortag"} =~ /([+-][0-9]+\.[0-9]+)\x{a0}%/;
Branch: 1 branch
BranchTrueFalseTotal
if $pricetable{"Diff. Vortag"} =~ /([+-][0-9]+\.[0-9]+)\x{a0}%/ 0 0 0
194  
195 0 $info{$symbol, 'isin'} = $infotable{ISIN};
196 0 $info{$symbol, 'wkn'} = $infotable{WKN};
197 0 $info{$symbol, 'symbol'} = $infotable{Symbol};
198  
199 # ISIN and WKN are not always included in the table
200 0 ▶ unless ( defined $info{$symbol. 'isin'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $info{$symbol . 'isin'}) 0 0 0
201 0 my $meta_block = $tree->look_down(
202 '_tag', 'meta',
203 'itemprop', 'description'
204 );
205 0 ▶ if ($meta_block) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta_block) 0 0 0
206 0 my $content = $meta_block->attr('content');
207 0 my ($wkn) = $content =~ /WKN:\s*([A-Z0-9]{6})\b/i;
208 0 my ($isin) = $content =~ /ISIN:\s*([A-Z]{2}[A-Z0-9]{10})\b/i;
209 0 $info{$symbol, 'wkn'} = $wkn;
210 0 $info{$symbol, 'isin'} = $isin;
211 }
212 }
213  
214 0 ▶ if ($pricetable{Zeit} =~ /([0-9]{2}[.][0-9]{2}[.][0-9]{2}) ([ 0-9][0-9]:[0-9][0-9])/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pricetable{"Zeit"} =~ /([0-9]{2}[.][0-9]{2}[.][0-9]{2}) ([ 0-9][0-9]:[0-9][0-9])/) 0 0 0
215 0 $quoter->store_date(\%info, $symbol, {eurodate => $1});
216 0 $info{$symbol, 'time'} = $2;
217 }
218  
219 0 $info{$symbol, 'method'} = 'comdirect';
220 0 $info{$symbol, 'success'} = 1;
221  
222 }
223  
224 0 ▶ return wantarray() ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
225 }
226  
227 5 1;
228  
229 =head1 NAME
230  
231 Finance::Quote::Comdirect - Obtain quotes from https://www.comdirect.de
232  
233 =head1 SYNOPSIS
234  
235 use Finance::Quote;
236  
237 $q = Finance::Quote->new;
238 or
239 $q = Finance::Quote->new("Comdirect", "comdirect" => { "EXCHANGE" => "Xetra" });
240  
241 %info = Finance::Quote->fetch('comdirect', 'DE0007664039');
242 %info = Finance::Quote->fetch('comdirect', 'Volkswagen');
243 %info = Finance::Quote->fetch('comdirect', 'VWAGY');
244  
245 @exchanges = @{ $info{ "VWAGY", "exchanges" } }; # List of available marketplaces
246  
247 =head1 DESCRIPTION
248  
249 This module fetches information from https://www.comdirect.de.
250  
251 This module is loaded by default on a Finance::Quote object. It's also possible
252 to load it explicitly by placing 'Comdirect' in the argument list to
253 Finance::Quote->new().
254  
255 =head1 EXCHANGE
256  
257 https://www.comdirect.de/ supports different marketplaces:
258  
259 "gettex"
260 "Xetra"
261 "Frankfurt"
262 "Tradegate"
263 ... any many more ...
264  
265 The EXCHANGE 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 may be returned by Finance::Quote::Comdirect:
271 isodate, time, last, currency, open, high, low, name, isin, wkn,
272 p_change, ask, bid, method, exchange, success, exchanges
273  
274 =head1 TERMS & CONDITIONS
275  
276 Use of www.comdirect.de is governed by any terms & conditions of that site.
277  
278 Finance::Quote is released under the GNU General Public License, version 2,
279 which explicitly carries a "No Warranty" clause.
280  
281 =cut
282  
↑ Summary Quote/FTfunds.pm »