Quote/Fondsweb.pm

filter: Statementstmt 34.7% 25 / 72 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 77.7% 7 / 9 Podpod 0.0% 0 / 5 totaltotal 34.7% 32 / 92 CC 5 SCAR 25.0
CC
5
Cov
32%
fondsweb
30.0
decimalPeriod
2.0
BEGIN
1.0
CRAP 12.2
« Quote/Currencies.pm ↑ Summary Quote/CSE.pm »
1 #
2 # Copyright (C) 2018, Diego Marcolungo
3 # vi: set ts=2 sw=2 noai ic showmode showmatch:
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17  
18 package Finance::Quote::Fondsweb;
19  
20  
21 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 5 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶use HTML::TreeBuilder::XPath;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 5 our $VERSION = '1.72_01'; # TRIAL VERSION
28  
29 5 our $FONDSWEB_URL = "https://www.fondsweb.com/de/";
30  
31 5 our $DISPLAY = 'Fondsweb, DE';
32 5 our @LABELS = qw/name symbol isin date isodate year_range nav last price currency source method type/;
33 5 our $METHODHASH = {subroutine => \&fondsweb,
34 display => $DISPLAY,
35 labels => \@LABELS};
36  
37 10 ▶sub methodinfo {
Subroutine methodinfo: called CC=1 SCAR=0.0
Pod undocumented
38 return (
39 10 fondsweb => $METHODHASH,
40 );
41 }
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 5 ▶sub methods {
Subroutine methods: called CC=1 SCAR=0.0
Pod undocumented
48 5 my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m;
49 }
50  
51 # 123.456.789,00 -> 123456789.00
52 0 ▶sub decimalPeriod {
Subroutine decimalPeriod: not called CC=1 SCAR=6.9
Pod undocumented
53 0 my $value = shift;
54  
55 0 $value =~ s/[.,]([0-9]+)$/#$1/; # temporarily replace final seperator with #
56 0 $value =~ s/[.,]//g; # remove all other seperators
57 0 $value =~ s/#/./; # replace # with .
58  
59 0 return $value;
60 }
61  
62 0 ▶sub fondsweb {
Subroutine fondsweb: not called CC=5 SCAR=34.0
Pod undocumented
63
64 0 my $quoter = shift;
65 0 my @symbols = @_;
66 0 my $te = HTML::TableExtract->new( depth => 0, count => 6 );
67 0 my %info;
68
69 # Iterate over each symbol
70 0 foreach my $symbol (@symbols) {
71 0 my $tree = HTML::TreeBuilder::XPath->new;
72 0 my $url = $FONDSWEB_URL . $symbol;
73 #~ debug_ua( $quoter->user_agent );
74
75 # The site check the user agent
76 0 $quoter->user_agent->agent("Mozilla/5.0 (X11; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0");
77 0 my $reply = $quoter->user_agent->request(GET $url);
78
79 # Check response
80 0 ▶ unless ($reply->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reply->is_success) { } 0 0 0
81 0 $info{ $symbol, "success" } = 0;
82 0 $info{ $symbol, "errmsg" } = join ' ', $reply->code, $reply->message;
83 } else {
84 # Parse the HTML tree
85 0 $tree->parse( $reply->decoded_content );
86
87 # Find data using xpath
88 # name
89 0 my $name = $tree->findvalue( '//h1[@class="fw--h1 fw--fondsModule-head-content-headline"]');
90 0 $info{ $symbol, 'name' } = $name;
91
92 # isin
93 0 my $isin_raw = $tree->findvalue( '//span[@class="text_bold"]');
94 0 my @isin = $isin_raw =~ m/^(\w\w\d+)\w./;
95 0 my $sym=$isin[0];
96 0 my $symlen = length($sym);
97 0 ▶ if($symlen>12) {
Branch: 1 branch
BranchTrueFalseTotal
if ($symlen > 12) 0 0 0
98 0 $sym = substr($isin[0], 0, 12);
99 }
100 0 $info{ $symbol, 'isin' } = $sym;
101 0 $info{ $symbol, 'symbol' } = $sym;
102
103 # date, isodate
104 0 my $raw_date = $tree->findvalue( '//i[@data-key="nav"]/..' );
105 0 my @date = $raw_date =~ m/.(\d\d)\.(\d\d)\.(\d\d\d\d)./;
106 0 $quoter->store_date(\%info, $symbol, {eurodate => "$date[0]/$date[1]/$date[2]"} );
107
108 # year_range, in this case use table extract
109 0 $te->parse($reply->decoded_content);
110 # the 6th table
111 0 my $details = $te->table(0, 6);
112 0 my $lastRowIndex = @{$details->rows} - 1;
113 # extract data with re
114 0 my $highest = decimalPeriod($details->cell($lastRowIndex - 1, 1));
115 0 my $lowest = decimalPeriod($details->cell($lastRowIndex, 1));
116 0 $info{ $symbol, "year_range" } = $lowest . ' - ' . $highest;
117
118 # nav, last, currency
119 0 my $raw_nav_currency = $tree->findvalue( '//div[@class="fw--fondDetail-price"]' );
120 0 my @nav_currency = $raw_nav_currency =~ m/^([0-9,.]+)\s(\w+)/;
121 0 $info{ $symbol, 'nav' } = decimalPeriod($nav_currency[0]);
122 0 $info{ $symbol, 'last' } = $info{ $symbol, 'nav' };
123 0 $info{ $symbol, 'currency' } = $nav_currency[-1];
124
125 # Other metadata
126 0 $info{ $symbol, 'method' } = 'fondsweb';
127 0 $info{ $symbol, "type" } = "fund";
128 0 $info{ $symbol, "success" } = 1;
129 }
130 }
131  
132 0 ▶ return wantarray ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
133 }
134  
135 __END__
« Quote/Currencies.pm ↑ Summary Quote/CSE.pm »