| 1 |
#!/usr/bin/perl -w |
| 2 |
# |
| 3 |
# vi: set ts=4 sw=4 ic noai expandtab showmode showmatch: |
| 4 |
# |
| 5 |
# Copyright (C) 1998, Dj Padzensky <[email protected]> |
| 6 |
# Copyright (C) 1998, 1999 Linas Vepstas <[email protected]> |
| 7 |
# Copyright (C) 2000, Yannick LE NY <[email protected]> |
| 8 |
# Copyright (C) 2000, Paul Fenwick <[email protected]> |
| 9 |
# Copyright (C) 2000, Brent Neal <[email protected]> |
| 10 |
# Copyright (C) 2000, Volker Stuerzl <[email protected]> |
| 11 |
# Copyright (C) 2002, Rainer Dorsch <[email protected]> |
| 12 |
# Copyright (C) 2022, Andre Joost <[email protected]> |
| 13 |
# Copyright (C) 2025, Bruce Schuck <[email protected]> |
| 14 |
# |
| 15 |
# |
| 16 |
# This program is free software; you can redistribute it and/or modify |
| 17 |
# it under the terms of the GNU General Public License as published by |
| 18 |
# the Free Software Foundation; either version 2 of the License, or |
| 19 |
# (at your option) any later version. |
| 20 |
# |
| 21 |
# This program is distributed in the hope that it will be useful, |
| 22 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 |
# GNU General Public License for more details. |
| 25 |
# |
| 26 |
# You should have received a copy of the GNU General Public License |
| 27 |
# along with this program; if not, write to the Free Software |
| 28 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 29 |
# 02110-1301, USA |
| 30 |
# |
| 31 |
# |
| 32 |
# This code derived from Padzensky's work on package Finance::YahooQuote, |
| 33 |
# but extends its capabilites to encompas a greater number of data sources. |
| 34 |
# |
| 35 |
# This code was developed as part of GnuCash <http://www.gnucash.org/> |
| 36 |
# |
| 37 |
# Changes: |
| 38 |
# Rewritten for issue #516: 2025-10-21, Bruce Schuck |
| 39 |
# Parses JSON retrieved from |
| 40 |
# https://internal.api.union-investment.de/beta/web/funddata/fundsearch?segment=web_de&type=fondssuche&api-version=beta-2.0.0 |
| 41 |
# See module for required API Key |
| 42 |
|
| 43 |
package Finance::Quote::Union; |
| 44 |
|
| 45 |
use strict; |
| 46 |
|
| 47 |
use constant DEBUG => $ENV{DEBUG}; |
| 48 |
use if DEBUG, 'Smart::Comments', '###'; |
| 49 |
|
| 50 |
use JSON qw(decode_json); |
| 51 |
|
| 52 |
our $VERSION = '1.71'; # VERSION |
| 53 |
|
| 54 |
# This url retrieve a JSON data structure containing data for all funds |
| 55 |
our $UNION_URL = 'https://internal.api.union-investment.de/beta/web/funddata/fundsearch?segment=web_de&type=fondssuche&api-version=beta-2.0.0'; |
| 56 |
|
| 57 |
our $DISPLAY = 'Union - German Funds'; |
| 58 |
our @LABELS = qw/exchange name date isodate price method currency/; |
| 59 |
our $METHODHASH = {subroutine => \&unionfunds, |
| 60 |
display => $DISPLAY, |
| 61 |
labels => \@LABELS}; |
| 62 |
|
| 63 |
sub methodinfo { |
| 64 |
return ( |
| 65 |
unionfunds => $METHODHASH, |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
sub labels { |
| 70 |
my %m = methodinfo(); |
| 71 |
return map {$_ => [@{$m{$_}{labels}}] } keys %m; |
| 72 |
} |
| 73 |
|
| 74 |
sub methods { |
| 75 |
my %m = methodinfo(); |
| 76 |
return map {$_ => $m{$_}{subroutine} } keys %m; |
| 77 |
} |
| 78 |
|
| 79 |
# ======================================================================= |
| 80 |
# The unionfunds routine gets quotes of UNION funds (Union Invest) |
| 81 |
# The URL returns a JSON data structure containing a table containing |
| 82 |
# quote data for all funds |
| 83 |
# |
| 84 |
# This subroutine was written by Bruce Schuck <[email protected]> |
| 85 |
|
| 86 |
sub unionfunds |
| 87 |
{ |
| 88 |
my $quoter = shift; |
| 89 |
my @funds = @_; |
| 90 |
return unless @funds; |
| 91 |
my $ua = $quoter->user_agent; |
| 92 |
my (%info, $json, $results); |
| 93 |
|
| 94 |
# Set headers. API key is sent as a header. |
| 95 |
my @ua_headers = ( |
| 96 |
'Accept' => 'application/json', |
| 97 |
'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36', |
| 98 |
'x-api-key' => '6d5b7ad050e948ce99516c20fbe37425', |
| 99 |
); |
| 100 |
|
| 101 |
# Website not supplying intermediate certificate causing |
| 102 |
# GET to fail |
| 103 |
# The website installed a new certificate. The following line |
| 104 |
# is no longer required. |
| 105 |
# $ua->ssl_opts(verify_hostname => 0, SSL_verify_mode => 0x00); |
| 106 |
|
| 107 |
# Get JSON data |
| 108 |
my $response = $ua->get($UNION_URL, @ua_headers); |
| 109 |
### [<now>] Response Code: $response->code |
| 110 |
|
| 111 |
unless ($response->is_success) { |
| 112 |
foreach my $fund (@funds) { |
| 113 |
$info{$fund, "success"} = 0; |
| 114 |
$info{$fund, "errormsg"} = "Error accessing $UNION_URL"; |
| 115 |
} |
| 116 |
return wantarray() ? %info : \%info; |
| 117 |
} |
| 118 |
|
| 119 |
# The body should be JSON |
| 120 |
eval {$json = decode_json($response->content)}; |
| 121 |
if($@) { |
| 122 |
foreach my $fund (@funds) { |
| 123 |
$info{$fund, "success"} = 0; |
| 124 |
$info{$fund, "errormsg"} = "No JSON data returned"; |
| 125 |
} |
| 126 |
return wantarray() ? %info : \%info; |
| 127 |
} |
| 128 |
# ### [<now>] JSON: $json |
| 129 |
|
| 130 |
my $componentarray = $json->{'content'}{'container'}{'component'}; |
| 131 |
### Component Array: $componentarray |
| 132 |
|
| 133 |
unless ($componentarray) { |
| 134 |
foreach my $fund (@funds) { |
| 135 |
$info{$fund, "success"} = 0; |
| 136 |
$info{$fund, "errormsg"} = "Unexpected JSON data"; |
| 137 |
} |
| 138 |
return wantarray() ? %info : \%info; |
| 139 |
} |
| 140 |
|
| 141 |
# Loop through componentarray looking for result array |
| 142 |
foreach my $component( @$componentarray ) { |
| 143 |
if ( $component->{'result'} ) { |
| 144 |
$results = $component->{'result'}; |
| 145 |
last; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
unless ( $results ) { |
| 150 |
foreach my $fund (@funds) { |
| 151 |
$info{$fund, "success"} = 0; |
| 152 |
$info{$fund, "errormsg"} = "JSON data does not include results"; |
| 153 |
} |
| 154 |
return wantarray() ? %info : \%info; |
| 155 |
} |
| 156 |
|
| 157 |
foreach my $fund (@funds) { |
| 158 |
foreach my $funddata (@$results) { |
| 159 |
if ($fund eq $funddata->{'tableRows'}[0]{'isin'}{'value'} ) { |
| 160 |
$info{$fund, 'success'} = 1; |
| 161 |
$info{$fund, 'name' } = $funddata->{'tableRows'}[0]{'fundName'}{'value'}; |
| 162 |
$info{$fund, 'exchange'} = 'UNION'; |
| 163 |
$info{$fund, 'symbol'} = $fund; |
| 164 |
$info{$fund, 'price'} = $funddata->{'tableRows'}[0]{'returnPrice'}{'valueSortable'}; |
| 165 |
$info{$fund, 'last'} = $funddata->{'tableRows'}[0]{'returnPrice'}{'valueSortable'}; |
| 166 |
my $date = $funddata->{'tableRows'}[0]{'date'}{'value'}; |
| 167 |
my ($price, $currency) = split(/ /, $funddata->{'tableRows'}[0]{'returnPrice'}{'value'}); |
| 168 |
$info{$fund, 'currency'} = $currency; |
| 169 |
$quoter->store_date(\%info, $fund, {eurodate => $date}); |
| 170 |
last; |
| 171 |
} |
| 172 |
} |
| 173 |
unless ($info{$fund, 'success'}) { |
| 174 |
$info{$fund, 'success'} = 0; |
| 175 |
$info{$fund, 'errormsg'} = 'Symbol not found'; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
return wantarray() ? %info : \%info; |
| 180 |
|
| 181 |
} |
| 182 |
|
| 183 |
1; |
| 184 |
|
| 185 |
__END__ |
| 186 |
|
| 187 |
=head1 NAME |
| 188 |
|
| 189 |
Finance::Quote::Union - Obtain quotes from UNION (Union Investment). |
| 190 |
|
| 191 |
=head1 SYNOPSIS |
| 192 |
|
| 193 |
use Finance::Quote; |
| 194 |
|
| 195 |
$q = Finance::Quote->new; |
| 196 |
|
| 197 |
%stockinfo = $q->fetch("unionfunds","DE0008491002"); |
| 198 |
|
| 199 |
=head1 DESCRIPTION |
| 200 |
|
| 201 |
This module obtains information about UNION managed funds. |
| 202 |
|
| 203 |
Information returned by this module is governed by UNION's terms |
| 204 |
and conditions. |
| 205 |
|
| 206 |
Note that previous versions of the module required the WKN, |
| 207 |
now the ISIN is needed as symbol value. |
| 208 |
|
| 209 |
=head1 LABELS RETURNED |
| 210 |
|
| 211 |
The following labels may be returned by Finance::Quote::UNION: |
| 212 |
|
| 213 |
=over |
| 214 |
|
| 215 |
=item exchange |
| 216 |
|
| 217 |
=item name |
| 218 |
|
| 219 |
=item date |
| 220 |
|
| 221 |
=item price |
| 222 |
|
| 223 |
=item last |
| 224 |
|
| 225 |
=back |
| 226 |
|
| 227 |
=head1 SEE ALSO |
| 228 |
|
| 229 |
UNION (Union Investment), https://www.union-investment.de/ |
| 230 |
|
| 231 |
=cut |