| 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 |
use warnings; |
| 22 |
use strict; |
| 23 |
|
| 24 |
use HTTP::Request::Common; |
| 25 |
use HTML::TreeBuilder::XPath; |
| 26 |
|
| 27 |
our $VERSION = '1.71'; # VERSION |
| 28 |
|
| 29 |
our $FONDSWEB_URL = "https://www.fondsweb.com/de/"; |
| 30 |
|
| 31 |
our $DISPLAY = 'Fondsweb, DE'; |
| 32 |
our @LABELS = qw/name symbol isin date isodate year_range nav last price currency source method type/; |
| 33 |
our $METHODHASH = {subroutine => \&fondsweb, |
| 34 |
display => $DISPLAY, |
| 35 |
labels => \@LABELS}; |
| 36 |
|
| 37 |
sub methodinfo { |
| 38 |
return ( |
| 39 |
fondsweb => $METHODHASH, |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
sub labels { |
| 44 |
my %m = methodinfo(); return map {$_ => [@{$m{$_}{labels}}] } keys %m; |
| 45 |
} |
| 46 |
|
| 47 |
sub methods { |
| 48 |
my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m; |
| 49 |
} |
| 50 |
|
| 51 |
# 123.456.789,00 -> 123456789.00 |
| 52 |
sub decimalPeriod { |
| 53 |
my $value = shift; |
| 54 |
|
| 55 |
$value =~ s/[.,]([0-9]+)$/#$1/; # temporarily replace final seperator with # |
| 56 |
$value =~ s/[.,]//g; # remove all other seperators |
| 57 |
$value =~ s/#/./; # replace # with . |
| 58 |
|
| 59 |
return $value; |
| 60 |
} |
| 61 |
|
| 62 |
sub fondsweb { |
| 63 |
|
| 64 |
my $quoter = shift; |
| 65 |
my @symbols = @_; |
| 66 |
my $te = HTML::TableExtract->new( depth => 0, count => 6 ); |
| 67 |
my %info; |
| 68 |
|
| 69 |
# Iterate over each symbol |
| 70 |
foreach my $symbol (@symbols) { |
| 71 |
my $tree = HTML::TreeBuilder::XPath->new; |
| 72 |
my $url = $FONDSWEB_URL . $symbol; |
| 73 |
#~ debug_ua( $quoter->user_agent ); |
| 74 |
|
| 75 |
# The site check the user agent |
| 76 |
$quoter->user_agent->agent("Mozilla/5.0 (X11; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0"); |
| 77 |
my $reply = $quoter->user_agent->request(GET $url); |
| 78 |
|
| 79 |
# Check response |
| 80 |
unless ($reply->is_success) { |
| 81 |
$info{ $symbol, "success" } = 0; |
| 82 |
$info{ $symbol, "errmsg" } = join ' ', $reply->code, $reply->message; |
| 83 |
} else { |
| 84 |
# Parse the HTML tree |
| 85 |
$tree->parse( $reply->decoded_content ); |
| 86 |
|
| 87 |
# Find data using xpath |
| 88 |
# name |
| 89 |
my $name = $tree->findvalue( '//h1[@class="fw--h1 fw--fondsModule-head-content-headline"]'); |
| 90 |
$info{ $symbol, 'name' } = $name; |
| 91 |
|
| 92 |
# isin |
| 93 |
my $isin_raw = $tree->findvalue( '//span[@class="text_bold"]'); |
| 94 |
my @isin = $isin_raw =~ m/^(\w\w\d+)\w./; |
| 95 |
my $sym=$isin[0]; |
| 96 |
my $symlen = length($sym); |
| 97 |
if($symlen>12) { |
| 98 |
$sym = substr($isin[0], 0, 12); |
| 99 |
} |
| 100 |
$info{ $symbol, 'isin' } = $sym; |
| 101 |
$info{ $symbol, 'symbol' } = $sym; |
| 102 |
|
| 103 |
# date, isodate |
| 104 |
my $raw_date = $tree->findvalue( '//i[@data-key="nav"]/..' ); |
| 105 |
my @date = $raw_date =~ m/.(\d\d)\.(\d\d)\.(\d\d\d\d)./; |
| 106 |
$quoter->store_date(\%info, $symbol, {eurodate => "$date[0]/$date[1]/$date[2]"} ); |
| 107 |
|
| 108 |
# year_range, in this case use table extract |
| 109 |
$te->parse($reply->decoded_content); |
| 110 |
# the 6th table |
| 111 |
my $details = $te->table(0, 6); |
| 112 |
my $lastRowIndex = @{$details->rows} - 1; |
| 113 |
# extract data with re |
| 114 |
my $highest = decimalPeriod($details->cell($lastRowIndex - 1, 1)); |
| 115 |
my $lowest = decimalPeriod($details->cell($lastRowIndex, 1)); |
| 116 |
$info{ $symbol, "year_range" } = $lowest . ' - ' . $highest; |
| 117 |
|
| 118 |
# nav, last, currency |
| 119 |
my $raw_nav_currency = $tree->findvalue( '//div[@class="fw--fondDetail-price"]' ); |
| 120 |
my @nav_currency = $raw_nav_currency =~ m/^([0-9,.]+)\s(\w+)/; |
| 121 |
$info{ $symbol, 'nav' } = decimalPeriod($nav_currency[0]); |
| 122 |
$info{ $symbol, 'last' } = $info{ $symbol, 'nav' }; |
| 123 |
$info{ $symbol, 'currency' } = $nav_currency[-1]; |
| 124 |
|
| 125 |
# Other metadata |
| 126 |
$info{ $symbol, 'method' } = 'fondsweb'; |
| 127 |
$info{ $symbol, "type" } = "fund"; |
| 128 |
$info{ $symbol, "success" } = 1; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
return wantarray ? %info : \%info; |
| 133 |
} |
| 134 |
|
| 135 |
__END__ |
| 136 |
|
| 137 |
=head1 NAME |
| 138 |
|
| 139 |
Finance::Quote::Fondsweb - Obtain price data from Fondsweb (Germany) |
| 140 |
|
| 141 |
=head1 VERSION |
| 142 |
|
| 143 |
This documentation describes version 1.00 of Fondsweb.pm, December 28, 2018. |
| 144 |
|
| 145 |
=head1 SYNOPSIS |
| 146 |
|
| 147 |
use Finance::Quote; |
| 148 |
|
| 149 |
$q = Finance::Quote->new; |
| 150 |
|
| 151 |
%info = $q->fetch("fondsweb", "LU0804734787"); |
| 152 |
|
| 153 |
=head1 DESCRIPTION |
| 154 |
|
| 155 |
This module obtains information from Fondsweb (Germany), |
| 156 |
L<https://www.fondsweb.com/>. |
| 157 |
|
| 158 |
Information returned by this module is governed by Fondsweb |
| 159 |
(Germany)'s terms and conditions. |
| 160 |
|
| 161 |
=head1 FUND SYMBOLS |
| 162 |
|
| 163 |
Use the ISIN number |
| 164 |
|
| 165 |
e.g. For L<https://www.fondsweb.com/de/LU0804734787>, |
| 166 |
one would supply LU0804734787 as the symbol argument on the fetch API call. |
| 167 |
|
| 168 |
=head1 LABELS RETURNED |
| 169 |
|
| 170 |
The following labels are returned by Finance::Quote::Fondsweb: |
| 171 |
|
| 172 |
- currency |
| 173 |
- date |
| 174 |
- isin |
| 175 |
- isodate |
| 176 |
- last |
| 177 |
- method |
| 178 |
- name |
| 179 |
- nav |
| 180 |
- type |
| 181 |
|
| 182 |
|
| 183 |
=head1 REQUIREMENTS |
| 184 |
|
| 185 |
Perl 5.012 |
| 186 |
HTML::TableExtract |
| 187 |
HTML::TreeBuilder::XPath |
| 188 |
|
| 189 |
=head1 ACKNOWLEDGEMENTS |
| 190 |
|
| 191 |
Inspired by other modules already present with Finance::Quote |
| 192 |
|
| 193 |
=head1 AUTHOR |
| 194 |
|
| 195 |
Diego Marcolungo |
| 196 |
|
| 197 |
=head1 LICENSE AND COPYRIGHT |
| 198 |
|
| 199 |
Copyright (C) 2018, Diego Marcolungo. |
| 200 |
|
| 201 |
This program is free software: you can redistribute it and/or modify |
| 202 |
it under the terms of the GNU General Public License as published by |
| 203 |
the Free Software Foundation, either version 3 of the License, or |
| 204 |
(at your option) any later version. |
| 205 |
|
| 206 |
=head1 DISCLAIMER |
| 207 |
|
| 208 |
This program is distributed in the hope that it will be useful, |
| 209 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 210 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 211 |
GNU General Public License for more details. |
| 212 |
|
| 213 |
=head1 SEE ALSO |
| 214 |
|
| 215 |
Fondsweb (Germany), L<https://www.fondsweb.com/> |
| 216 |
|
| 217 |
=cut |