| 1 |
# Finance::Quote Perl module to retrieve quotes from Finanzpartner.de |
| 2 |
# vi: set ts=2 sw=2 noai ic showmode showmatch: |
| 3 |
# Copyright (C) 2007 Jan Willamowius <[email protected]> |
| 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 2 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, write to the Free Software |
| 17 |
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
|
| 19 |
package Finance::Quote::Finanzpartner; |
| 20 |
|
| 21 |
use strict; |
| 22 |
|
| 23 |
use constant DEBUG => $ENV{DEBUG}; |
| 24 |
use if DEBUG, 'Smart::Comments'; |
| 25 |
|
| 26 |
use Web::Scraper; |
| 27 |
use Encode; |
| 28 |
|
| 29 |
our $VERSION = '1.71'; # VERSION |
| 30 |
|
| 31 |
my $FINANZPARTNER_URL = "https://www.finanzpartner.de/fi/"; |
| 32 |
|
| 33 |
our $DISPLAY = 'Finanzpartner, DE'; |
| 34 |
our @LABELS = qw/name date price last method/; |
| 35 |
our $METHODHASH = {subroutine => \&finanzpartner, |
| 36 |
display => $DISPLAY, |
| 37 |
labels => \@LABELS}; |
| 38 |
|
| 39 |
sub methodinfo { |
| 40 |
return ( |
| 41 |
finanzpartner => $METHODHASH, |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
sub labels { |
| 46 |
my %m = methodinfo(); return map {$_ => [@{$m{$_}{labels}}] } keys %m; |
| 47 |
} |
| 48 |
|
| 49 |
sub methods { |
| 50 |
my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m; |
| 51 |
} |
| 52 |
|
| 53 |
sub finanzpartner |
| 54 |
{ |
| 55 |
my $quoter = shift; # The Finance::Quote object. |
| 56 |
my @stocks = @_; |
| 57 |
my $ua = $quoter->user_agent(); |
| 58 |
my %info; |
| 59 |
|
| 60 |
foreach my $stock (@stocks) { |
| 61 |
eval { |
| 62 |
my @headers = ( |
| 63 |
"authority" => "www.finanzpartner.de", |
| 64 |
"sec-ch-ua" => '"Google Chrome";v="87", " Not;A Brand";v="99", "Chromium";v="87"', |
| 65 |
"sec-ch-ua-mobile" => "?0", |
| 66 |
"upgrade-insecure-requests" => "1", |
| 67 |
"user-agent" => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36', |
| 68 |
"accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", |
| 69 |
"accept-language" => "en-US,en;q=0.9", |
| 70 |
"sec-ch-ua" => "\"Google Chrome\";v=\"87\", \" Not;A Brand\";v=\"99\", \"Chromium\";v=\"87\"", |
| 71 |
"sec-fetch-dest" => "document", |
| 72 |
"sec-fetch-mode" => "navigate", |
| 73 |
"sec-fetch-site" => "none", |
| 74 |
); |
| 75 |
|
| 76 |
my $url = $FINANZPARTNER_URL . $stock . '/'; |
| 77 |
|
| 78 |
### url : $url |
| 79 |
|
| 80 |
my $reply = $ua->get($url, @headers); |
| 81 |
|
| 82 |
my $processor = scraper { |
| 83 |
process 'span.kurs-m.pull-left', 'price[]' => 'TEXT'; |
| 84 |
process 'span.kurs.pull-left', 'price_alternative[]' => 'TEXT'; |
| 85 |
process 'h1 > small', 'isin[]' => 'TEXT'; |
| 86 |
process 'div.col-md-2', 'date[]' => 'TEXT'; |
| 87 |
process 'h1 > span', 'name[]' => 'TEXT'; |
| 88 |
}; |
| 89 |
|
| 90 |
my $data = $processor->scrape(decode_utf8 $reply->content); |
| 91 |
|
| 92 |
# If price does not exists, then price_alternative should exist. In that case, put price_alternative into price. |
| 93 |
if(exists $data->{price_alternative}) { |
| 94 |
$data->{price} = $data->{price_alternative}; |
| 95 |
} |
| 96 |
|
| 97 |
### data: $data |
| 98 |
|
| 99 |
die "Unexpected price format" unless exists $data->{price} and $data->{price}->[0] =~ /^([0-9.]+) ([A-Z]+)$/; |
| 100 |
$info{$stock, "last"} = $1; |
| 101 |
$info{$stock, "currency"} = $2; |
| 102 |
|
| 103 |
die "Unexpected date format" unless exists $data->{date} and $data->{date}->[0] =~ /([0-9]{2}[.][0-9]{2}[.][0-9]{4})$/; |
| 104 |
$quoter->store_date(\%info, $stock, {eurodate => $1}); |
| 105 |
|
| 106 |
$info{$stock,"method"} = "finanzpartner"; |
| 107 |
$info{$stock,"symbol"} = $stock; |
| 108 |
$info{$stock,"success"} = 1; |
| 109 |
}; |
| 110 |
|
| 111 |
if ($@) { |
| 112 |
$info{$stock,"errormsg"} = $@; |
| 113 |
$info{$stock,"success"} = 0; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
return wantarray ? %info : \%info; |
| 118 |
} |
| 119 |
|
| 120 |
1; |
| 121 |
|
| 122 |
=head1 NAME |
| 123 |
|
| 124 |
Finance::Quote::Finanzpartner - Obtain quotes from Finanzpartner.de. |
| 125 |
|
| 126 |
=head1 SYNOPSIS |
| 127 |
|
| 128 |
use Finance::Quote; |
| 129 |
|
| 130 |
$q = Finance::Quote->new("Finanzpartner"); |
| 131 |
|
| 132 |
%info = $q->fetch("finanzpartner","LU0055732977"); |
| 133 |
|
| 134 |
=head1 DESCRIPTION |
| 135 |
|
| 136 |
This module obtains quotes from Finanzpartner.de (http://www.finanzpartner.de) by WKN or ISIN. |
| 137 |
|
| 138 |
=head1 LABELS RETURNED |
| 139 |
|
| 140 |
The following labels may be returned by Finance::Quote::Finanzpartner: |
| 141 |
name, date, price, last, method. |
| 142 |
|
| 143 |
=head1 SEE ALSO |
| 144 |
|
| 145 |
Finanzpartner, http://www.finanzpartner.de/ |
| 146 |
|
| 147 |
Finance::Quote; |
| 148 |
|
| 149 |
=cut |