| 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 <djpadz@padz.net> | ||||||||||
| 6 | # Copyright (C) 1998, 1999 Linas Vepstas <linas@linas.org> | ||||||||||
| 7 | # Copyright (C) 2000, Yannick LE NY <y-le-ny@ifrance.com> | ||||||||||
| 8 | # Copyright (C) 2000, Paul Fenwick <pjf@cpan.org> | ||||||||||
| 9 | # Copyright (C) 2000, Brent Neal <brentn@users.sourceforge.net> | ||||||||||
| 10 | # Copyright (C) 2000, Volker Stuerzl <volker.stuerzl@gmx.de> | ||||||||||
| 11 | # Copyright (C) 2002, Rainer Dorsch <rainer.dorsch@informatik.uni-stuttgart.de> | ||||||||||
| 12 | # Copyright (C) 2022, Andre Joost <andrejoost@gmx.de> | ||||||||||
| 13 | # Copyright (C) 2025, Bruce Schuck <bschuck@asgard-systems.com> | ||||||||||
| 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 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 46 | |||||||||||
| 47 | 5 | ▶ | use constant DEBUG => $ENV{DEBUG}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 48 | 5 | ▶ | use if DEBUG, 'Smart::Comments', '###'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 49 | |||||||||||
| 50 | 5 | ▶ | use JSON qw(decode_json); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 51 | |||||||||||
| 52 | 5 | our $VERSION = '1.72_02'; # TRIAL VERSION | |||||||||
| 53 | |||||||||||
| 54 | # This url retrieve a JSON data structure containing data for all funds | ||||||||||
| 55 | 5 | 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 | 5 | our $DISPLAY = 'Union - German Funds'; | |||||||||
| 58 | 5 | our @LABELS = qw/exchange name date isodate price method currency/; | |||||||||
| 59 | 5 | our $METHODHASH = {subroutine => \&unionfunds, | |||||||||
| 60 | display => $DISPLAY, | ||||||||||
| 61 | labels => \@LABELS}; | ||||||||||
| 62 | |||||||||||
| 63 | 10 | ▶ | sub methodinfo { | ||||||||
|
Subroutine
methodinfo: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 64 | return ( | ||||||||||
| 65 | 10 | unionfunds => $METHODHASH, | |||||||||
| 66 | germany => $METHODHASH, | ||||||||||
| 67 | ); | ||||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 5 | ▶ | sub labels { | ||||||||
|
Subroutine
labels: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 71 | 5 | my %m = methodinfo(); | |||||||||
| 72 | 5 | return map {$_ => [@{$m{$_}{labels}}] } keys %m; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 5 | ▶ | sub methods { | ||||||||
|
Subroutine
methods: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 76 | 5 | my %m = methodinfo(); | |||||||||
| 77 | 5 | return map {$_ => $m{$_}{subroutine} } keys %m; | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | # ======================================================================= | ||||||||||
| 81 | # The unionfunds routine gets quotes of UNION funds (Union Invest) | ||||||||||
| 82 | # The URL returns a JSON data structure containing a table containing | ||||||||||
| 83 | # quote data for all funds | ||||||||||
| 84 | # | ||||||||||
| 85 | # This subroutine was written by Bruce Schuck <bschuck@asgard-systems.com> | ||||||||||
| 86 | |||||||||||
| 87 | sub unionfunds | ||||||||||
| 88 | 0 | ▶ | { | ||||||||
|
Subroutine
unionfunds: not called CC=21 SCAR=61.4
Pod
undocumented
| |||||||||||
| 89 | 0 | my $quoter = shift; | |||||||||
| 90 | 0 | my @funds = @_; | |||||||||
| 91 | 0 | ▶ | return unless @funds; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 0 | my $ua = $quoter->user_agent; | |||||||||
| 93 | 0 | my (%info, $json, $results); | |||||||||
| 94 | |||||||||||
| 95 | # Set headers. API key is sent as a header. | ||||||||||
| 96 | 0 | my @ua_headers = ( | |||||||||
| 97 | 'Accept' => 'application/json', | ||||||||||
| 98 | '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', | ||||||||||
| 99 | 'x-api-key' => '6d5b7ad050e948ce99516c20fbe37425', | ||||||||||
| 100 | ); | ||||||||||
| 101 | |||||||||||
| 102 | # Website not supplying intermediate certificate causing | ||||||||||
| 103 | # GET to fail | ||||||||||
| 104 | # The website installed a new certificate. The following line | ||||||||||
| 105 | # is no longer required. | ||||||||||
| 106 | # $ua->ssl_opts(verify_hostname => 0, SSL_verify_mode => 0x00); | ||||||||||
| 107 | |||||||||||
| 108 | # Get JSON data | ||||||||||
| 109 | 0 | my $response = $ua->get($UNION_URL, @ua_headers); | |||||||||
| 110 | ### [<now>] Response Code: $response->code | ||||||||||
| 111 | |||||||||||
| 112 | 0 | ▶ | unless ($response->is_success) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | 0 | foreach my $fund (@funds) { | |||||||||
| 114 | 0 | $info{$fund, "success"} = 0; | |||||||||
| 115 | 0 | $info{$fund, "errormsg"} = "Error accessing $UNION_URL"; | |||||||||
| 116 | } | ||||||||||
| 117 | 0 | ▶ | return wantarray() ? %info : \%info; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | # The body should be JSON | ||||||||||
| 121 | 0 | eval {$json = decode_json($response->content)}; | |||||||||
| 122 | 0 | ▶ | if($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 123 | 0 | foreach my $fund (@funds) { | |||||||||
| 124 | 0 | $info{$fund, "success"} = 0; | |||||||||
| 125 | 0 | $info{$fund, "errormsg"} = "No JSON data returned"; | |||||||||
| 126 | } | ||||||||||
| 127 | 0 | ▶ | return wantarray() ? %info : \%info; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | } | ||||||||||
| 129 | # ### [<now>] JSON: $json | ||||||||||
| 130 | |||||||||||
| 131 | 0 | my $componentarray = $json->{'content'}{'container'}{'component'}; | |||||||||
| 132 | ### Component Array: $componentarray | ||||||||||
| 133 | |||||||||||
| 134 | 0 | ▶ | unless ($componentarray) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | 0 | foreach my $fund (@funds) { | |||||||||
| 136 | 0 | $info{$fund, "success"} = 0; | |||||||||
| 137 | 0 | $info{$fund, "errormsg"} = "Unexpected JSON data"; | |||||||||
| 138 | } | ||||||||||
| 139 | 0 | ▶ | return wantarray() ? %info : \%info; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 140 | } | ||||||||||
| 141 | |||||||||||
| 142 | # Loop through componentarray looking for result array | ||||||||||
| 143 | 0 | foreach my $component( @$componentarray ) { | |||||||||
| 144 | 0 | ▶ | if ( $component->{'result'} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 145 | 0 | $results = $component->{'result'}; | |||||||||
| 146 | 0 | last; | |||||||||
| 147 | } | ||||||||||
| 148 | } | ||||||||||
| 149 | |||||||||||
| 150 | 0 | ▶ | unless ( $results ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 151 | 0 | foreach my $fund (@funds) { | |||||||||
| 152 | 0 | $info{$fund, "success"} = 0; | |||||||||
| 153 | 0 | $info{$fund, "errormsg"} = "JSON data does not include results"; | |||||||||
| 154 | } | ||||||||||
| 155 | 0 | ▶ | return wantarray() ? %info : \%info; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 156 | } | ||||||||||
| 157 | |||||||||||
| 158 | 0 | foreach my $fund (@funds) { | |||||||||
| 159 | 0 | foreach my $funddata (@$results) { | |||||||||
| 160 | 0 | ▶ | if ($fund eq $funddata->{'tableRows'}[0]{'isin'}{'value'} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 161 | 0 | $info{$fund, 'success'} = 1; | |||||||||
| 162 | 0 | $info{$fund, 'name' } = $funddata->{'tableRows'}[0]{'fundName'}{'value'}; | |||||||||
| 163 | 0 | $info{$fund, 'exchange'} = 'UNION'; | |||||||||
| 164 | 0 | $info{$fund, 'symbol'} = $fund; | |||||||||
| 165 | 0 | $info{$fund, 'price'} = $funddata->{'tableRows'}[0]{'returnPrice'}{'valueSortable'}; | |||||||||
| 166 | 0 | $info{$fund, 'last'} = $funddata->{'tableRows'}[0]{'returnPrice'}{'valueSortable'}; | |||||||||
| 167 | 0 | my $date = $funddata->{'tableRows'}[0]{'date'}{'value'}; | |||||||||
| 168 | 0 | my ($price, $currency) = split(/ /, $funddata->{'tableRows'}[0]{'returnPrice'}{'value'}); | |||||||||
| 169 | 0 | $info{$fund, 'currency'} = $currency; | |||||||||
| 170 | 0 | $quoter->store_date(\%info, $fund, {eurodate => $date}); | |||||||||
| 171 | 0 | last; | |||||||||
| 172 | } | ||||||||||
| 173 | } | ||||||||||
| 174 | 0 | ▶ | unless ($info{$fund, 'success'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 175 | 0 | $info{$fund, 'success'} = 0; | |||||||||
| 176 | 0 | $info{$fund, 'errormsg'} = 'Symbol not found'; | |||||||||
| 177 | } | ||||||||||
| 178 | } | ||||||||||
| 179 | |||||||||||
| 180 | 0 | ▶ | return wantarray() ? %info : \%info; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 181 | |||||||||||
| 182 | } | ||||||||||
| 183 | |||||||||||
| 184 | 5 | 1; | |||||||||
| 185 | |||||||||||
| 186 | __END__ | ||||||||||