Debian Codegraph @

lib/Finance/Quote/Comdirect.pm

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