File Coverage

blib/lib/Finance/Quote/Comdirect.pm
Criterion Covered Total %
statement 23 64 35.9
branch 0 18 0.0
condition 0 12 0.0
subroutine 9 11 81.8
pod 0 3 0.0
total 32 108 29.6


line stmt bran cond sub pod time code
1             # This program is free software; you can redistribute it and/or modify
2             # it under the terms of the GNU General Public License as published by
3             # the Free Software Foundation; either version 2 of the License, or
4             # (at your option) any later version.
5             #
6             # This program is distributed in the hope that it will be useful,
7             # but WITHOUT ANY WARRANTY; without even the implied warranty of
8             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9             # GNU General Public License for more details.
10             #
11             # You should have received a copy of the GNU General Public License
12             # along with this program; if not, write to the Free Software
13             # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
14             # 02110-1301, USA
15              
16             package Finance::Quote::Comdirect;
17              
18 5     5   2724 use strict;
  5         12  
  5         171  
19 5     5   27 use warnings;
  5         13  
  5         236  
20              
21 5     5   32 use constant DEBUG => $ENV{DEBUG};
  5         10  
  5         315  
22 5     5   33 use if DEBUG, 'Smart::Comments';
  5         11  
  5         37  
23              
24 5     5   228 use LWP::UserAgent;
  5         12  
  5         46  
25 5     5   174 use Web::Scraper;
  5         12  
  5         51  
26 5     5   403 use String::Util qw(trim);
  5         13  
  5         4075  
27              
28             our $VERSION = '1.57_03'; # TRIAL VERSION
29              
30             our @labels = qw/last date isodate/;
31              
32             sub labels {
33 5     5 0 22 return ( comdirect => \@labels );
34             }
35              
36             sub methods {
37 5     5 0 22 return ( comdirect => \&comdirect );
38             }
39              
40             sub comdirect {
41 0     0 0   my $quoter = shift;
42 0           my @symbols = @_;
43 0           my $ua = $quoter->user_agent();
44 0           my %info;
45              
46 0           foreach my $symbol (@_) {
47 0           eval {
48 0           my $url = 'https://www.comdirect.de/inf/search/all.html?SEARCH_VALUE=' . $symbol;
49 0           my $reply = $ua->get($url);
50              
51             ### [<now>] Fetched: $url
52             my $data = scraper {
53 0     0     process '/html/body/div[3]/div/div[2]/div[6]/div[1]/div/div/div[2]/div/div/table//td', 'table[]' => ['TEXT', sub{trim($_)}];
  0            
54 0           process '/html/body/div[3]/div/div[2]/div[1]/div[1]/div/h1/text()', 'name' => ['TEXT', sub{trim($_)}];
  0            
55 0           process '/html/body/div[3]/div/div[2]/div[1]/div[1]/div/div[2]/h2/text()[2]', 'isin' => ['TEXT', sub{trim($_)}];
  0            
56 0           };
57              
58 0           my $result = $data->scrape($reply);
59            
60             ### Parsed: $result
61              
62             # Zeit appears twice as row label, so we need to differentiate them before converting to hash
63 0           my $i = 0;
64 0 0         my @table = map {$_ eq 'Zeit' ? $_ . $i++ : $_} @{$result->{table}};
  0            
  0            
65 0           my %table = @table;
66            
67             die "Missing expected fields" unless
68             exists $table{Zeit0} and
69             exists $table{Aktuell} and
70             exists $table{Hoch} and
71             exists $table{Tief} and
72 0 0 0       exists $table{"Er\x{f6}ffnung"};
      0        
      0        
      0        
73            
74 0           $table{Aktuell} =~ s/,/./;
75 0           $table{Hoch} =~ s/,/./;
76 0           $table{Tief} =~ s/,/./;
77 0           $table{"Er\x{f6}ffnung"} =~ s/,/./;
78              
79 0 0         $info{$symbol, 'last'} = $1 if $table{Aktuell} =~ /^([0-9.]+)/;
80 0 0         $info{$symbol, 'currency'} = $1 if $table{Aktuell} =~ /([A-Z]+)$/;
81 0           $info{$symbol, 'open'} = $table{"Er\x{f6}ffnung"};
82 0           $info{$symbol, 'high'} = $table{Hoch};
83 0           $info{$symbol, 'low'} = $table{Tief};
84 0 0         $info{$symbol, 'name'} = $result->{name} if exists $result->{name};
85 0 0         $info{$symbol, 'isin'} = $result->{isin} if exists $result->{isin};
86              
87 0 0         $quoter->store_date(\%info, $symbol, {eurodate => $1}) if $table{Zeit0} =~ /([0-9]{2}[.][0-9]{2}[.][0-9]{2})/;
88            
89 0           $info{$symbol, 'method'} = 'comdirect';
90 0           $info{$symbol, 'success'} = 1;
91             };
92            
93 0 0         if ($@) {
94 0           my $error = "Comdirect failed: $@";
95 0           $info{$symbol, 'success'} = 0;
96 0           $info{$symbol, 'errormsg'} = trim($error);
97             }
98             }
99              
100 0 0         return wantarray() ? %info : \%info;
101             }
102              
103             1;
104              
105             =head1 NAME
106              
107             Finance::Quote::Comdirect - Obtain quotes from https://www.comdirect.de
108              
109             =head1 SYNOPSIS
110              
111             use Finance::Quote;
112              
113             $q = Finance::Quote->new;
114              
115             %info = Finance::Quote->fetch('comdirect', 'DE0007664039');
116             %info = Finance::Quote->fetch('comdirect', 'Volkswagen');
117             %info = Finance::Quote->fetch('comdirect', 'VWAGY');
118              
119             =head1 DESCRIPTION
120              
121             This module fetches information from https://www.comdirect.de.
122              
123             This module is loaded by default on a Finance::Quote object. It's also possible
124             to load it explicitly by placing 'Comdirect' in the argument list to
125             Finance::Quote->new().
126              
127             =head1 LABELS RETURNED
128              
129             The following labels may be returned by Finance::Quote::Comdirect:
130             isodate, last, currency, open, high, low, name, isin, method, success
131              
132             =head1 TERMS & CONDITIONS
133              
134             Use of www.comdirect.de is governed by any terms & conditions of that site.
135              
136             Finance::Quote is released under the GNU General Public License, version 2,
137             which explicitly carries a "No Warranty" clause.
138              
139             =cut
140