| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
# SwissFundData.pm |
| 4 |
# |
| 5 |
# Obtains quotes for CH Unit Trusts from https://swissfunddata.ch/ - please |
| 6 |
# refer to the end of this file for further information. |
| 7 |
# |
| 8 |
# author: Manuel Friedli ([email protected]) |
| 9 |
# |
| 10 |
# version: 0.1 Initial version - 27 July 2025 |
| 11 |
# |
| 12 |
# This file is heavily based on MStaruk.pm by Martin Sadler |
| 13 |
# ([email protected]), version 0.1, 01 April 2013 |
| 14 |
# |
| 15 |
# This program is free software; you can redistribute it and/or modify |
| 16 |
# it under the terms of the GNU General Public License as published by |
| 17 |
# the Free Software Foundation; either version 2 of the License, or |
| 18 |
# (at your option) any later version. |
| 19 |
# |
| 20 |
# This program is distributed in the hope that it will be useful, |
| 21 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 |
# GNU General Public License for more details. |
| 24 |
# |
| 25 |
# You should have received a copy of the GNU General Public License |
| 26 |
# along with this program; if not, write to the Free Software |
| 27 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 28 |
# 02110-1301, USA |
| 29 |
# |
| 30 |
|
| 31 |
|
| 32 |
package Finance::Quote::SwissFundData; |
| 33 |
require 5.006; |
| 34 |
|
| 35 |
use strict; |
| 36 |
use warnings; |
| 37 |
|
| 38 |
# URLs |
| 39 |
use vars qw($VERSION $SFDCH_LOOK_UP $SFDCH_MAIN_URL); |
| 40 |
|
| 41 |
use LWP::UserAgent; |
| 42 |
use HTTP::Cookies; |
| 43 |
use Web::Scraper; |
| 44 |
use String::Util qw(trim); |
| 45 |
|
| 46 |
|
| 47 |
$SFDCH_MAIN_URL = "https://www.swissfunddata.ch"; |
| 48 |
$SFDCH_LOOK_UP = "https://www.swissfunddata.ch/sfdpub/en/funds/prices?text="; |
| 49 |
|
| 50 |
our $VERSION = '1.71'; # VERSION |
| 51 |
|
| 52 |
our $DISPLAY = "SwissFundData"; |
| 53 |
our @LABELS = qw/name currency date nav isodate method success errormsg/; |
| 54 |
our $METHODHASH = { |
| 55 |
subroutine => \&swissfunddata, |
| 56 |
display => $DISPLAY, |
| 57 |
labels => \@LABELS |
| 58 |
}; |
| 59 |
|
| 60 |
sub methodinfo { |
| 61 |
return ( |
| 62 |
swissfunddata => $METHODHASH, |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
sub labels { |
| 67 |
my %m = methodinfo(); |
| 68 |
return map {$_ => [@{$m{$_}{labels}}]} keys %m; |
| 69 |
} |
| 70 |
|
| 71 |
sub methods { |
| 72 |
my %m = methodinfo(); |
| 73 |
return map {$_ => $m{$_}{subroutine}} keys %m; |
| 74 |
} |
| 75 |
|
| 76 |
sub swissfunddata { |
| 77 |
my $quoter = shift; |
| 78 |
my @symbols = @_; |
| 79 |
|
| 80 |
return unless @symbols; |
| 81 |
|
| 82 |
my %info; |
| 83 |
|
| 84 |
my $cookie_jar = HTTP::Cookies->new; |
| 85 |
$cookie_jar->set_cookie(0, "sfdpub-disclaimer", "private", "/sfdpub", "www.swissfunddata.ch", "443", 0, 0, 86400, 0); |
| 86 |
|
| 87 |
my $ua = $quoter->user_agent; |
| 88 |
$ua->cookie_jar($cookie_jar); |
| 89 |
|
| 90 |
foreach (@symbols) { |
| 91 |
my $symbol = $_; |
| 92 |
|
| 93 |
$info {$symbol, "source"} = "SwissFundData"; |
| 94 |
$info {$symbol, "success"} = 1; # ever the optimist.... |
| 95 |
$info {$symbol, "errormsg"} = "Success"; |
| 96 |
|
| 97 |
# perform the look-up - if not found, return with error |
| 98 |
my $reply = $ua->get($SFDCH_LOOK_UP.$symbol); |
| 99 |
my $widget = scraper { |
| 100 |
process 'div#resultContainer table tbody tr:first-child td:nth-child(2)', 'name_isin' => ['HTML', sub {trim($_)}]; |
| 101 |
process 'div#resultContainer table tbody tr:first-child td:nth-child(3)', 'nav_currency_date' => ['TEXT', sub {trim($_)}]; |
| 102 |
}; |
| 103 |
my $text = $widget->scrape($reply); |
| 104 |
|
| 105 |
unless (exists $text->{name_isin}) { |
| 106 |
# serious error, report it and give up |
| 107 |
$info {$symbol, "success"} = 0; |
| 108 |
$info {$symbol, "errormsg"} = "Error - failed to retrieve fund data: can not read name/isin."; |
| 109 |
next; |
| 110 |
} |
| 111 |
|
| 112 |
unless (exists $text->{nav_currency_date}) { |
| 113 |
# serious error, report it and give up |
| 114 |
$info {$symbol, "success"} = 0; |
| 115 |
$info {$symbol, "errormsg"} = "Error - failed to retrieve basic fund data: can not read nav/currency/date"; |
| 116 |
next; |
| 117 |
} |
| 118 |
|
| 119 |
my ($name, $isin); |
| 120 |
if ($text->{name_isin} =~ m[<a href=".+">(.*)</a>.*<br />(.*)]) { |
| 121 |
$name = trim($1); |
| 122 |
$isin = trim($2); |
| 123 |
} |
| 124 |
|
| 125 |
if (!defined($name)) { |
| 126 |
$name = "*** UNKNOWN ***"; |
| 127 |
} |
| 128 |
if(!defined($isin)) { |
| 129 |
$isin = "*** UNKNOWN ***"; |
| 130 |
} |
| 131 |
|
| 132 |
my ($nav, $currency, $date); |
| 133 |
if ($text->{nav_currency_date} =~ m[(\d+\.\d+) (\w+) (\d{2}\.\d{2}\.\d{4})]) { |
| 134 |
$nav = trim($1); |
| 135 |
$currency = trim($2); |
| 136 |
$date = trim($3); |
| 137 |
} |
| 138 |
|
| 139 |
if (!defined($nav)) { |
| 140 |
# serious error, report it and give up |
| 141 |
$info {$symbol, "success"} = 0; |
| 142 |
$info {$symbol, "errormsg"} = "Error - failed to retrieve fund data: missing 'nav'"; |
| 143 |
next; |
| 144 |
} |
| 145 |
if (!defined($currency)) { |
| 146 |
# serious error, report it and give up |
| 147 |
$info {$symbol, "success"} = 0; |
| 148 |
$info {$symbol, "errormsg"} = "Error - failed to retrieve fund data: missing 'currency'"; |
| 149 |
next; |
| 150 |
} |
| 151 |
if (!defined($date)) { |
| 152 |
# serious error, report it and give up |
| 153 |
$info {$symbol, "success"} = 0; |
| 154 |
$info {$symbol, "errormsg"} = "Error - failed to retrieve fund data: missing 'date'"; |
| 155 |
next; |
| 156 |
} |
| 157 |
|
| 158 |
$info {$symbol, "name"} = $name; |
| 159 |
$info {$symbol, "isin"} = $isin; |
| 160 |
$info {$symbol, "nav"} = $nav; |
| 161 |
$info {$symbol, "currency"} = $currency; |
| 162 |
$quoter->store_date(\%info, $symbol, {eurodate => $date}); |
| 163 |
$info {$symbol, "method"} = "swissfunddata"; |
| 164 |
# It seems that GnuCash insists on having the time set?! |
| 165 |
$info {$symbol, "time"} = "12:00"; |
| 166 |
} |
| 167 |
|
| 168 |
return wantarray ? %info : \%info; |
| 169 |
} |
| 170 |
|
| 171 |
1; |
| 172 |
|
| 173 |
=head1 NAME |
| 174 |
|
| 175 |
Finance::Quote::SwissFindData - Obtain CH Unit Trust quotes from swissfunddata.ch. |
| 176 |
|
| 177 |
=head1 SYNOPSIS |
| 178 |
|
| 179 |
$q = Finance::Quote->new; |
| 180 |
|
| 181 |
%info = Finance::Quote->fetch("swissfunddata","<isin> ..."); # Only query swissfunddata.ch using ISINs |
| 182 |
|
| 183 |
=head1 DESCRIPTION |
| 184 |
|
| 185 |
This module fetches information from the SwissFindData Funds service, |
| 186 |
https://swissfunddata.ch/. |
| 187 |
|
| 188 |
Funds are identified by their ISIN code. |
| 189 |
|
| 190 |
This module is loaded by default on a Finance::Quote object. It's |
| 191 |
also possible to load it explicitly by placing "swissfunddata" in the argument |
| 192 |
list to Finance::Quote->new(). |
| 193 |
|
| 194 |
Information obtained by this module may be covered by SwissFundData |
| 195 |
terms and conditions. See https://swissfunddata.ch/ for details. |
| 196 |
|
| 197 |
=head2 Stocks And Indices |
| 198 |
|
| 199 |
This module provides the "swissfunddata" fetch method for fetching CH Unit |
| 200 |
Trusts and OEICs prices and other information from swissfunddata.ch. |
| 201 |
|
| 202 |
=head1 LABELS RETURNED |
| 203 |
|
| 204 |
The following labels may be returned by Finance::Quote::swissfunddata : |
| 205 |
|
| 206 |
name, isin, nav, currency, date, isodate, method, success, errormsg. |
| 207 |
|
| 208 |
|
| 209 |
=head1 SEE ALSO |
| 210 |
|
| 211 |
SwissFundData websites, https://www.swissfunddata.ch |
| 212 |
|
| 213 |
|
| 214 |
=head1 AUTHOR |
| 215 |
|
| 216 |
Manuel Friedli, E<lt>[email protected]<gt> |
| 217 |
Based heavily on the work of Martin Sadler E<lt>[email protected]<gt>, many thanks! |
| 218 |
|
| 219 |
=head1 COPYRIGHT AND LICENSE |
| 220 |
|
| 221 |
Copyright (C) 2025 by Manuel Friedli |
| 222 |
|
| 223 |
This library is free software; you can redistribute it and/or modify |
| 224 |
it under the same terms as Perl itself, either Perl version 5.10.1 or, |
| 225 |
at your option, any later version of Perl 5 you may have available. |
| 226 |
|
| 227 |
|
| 228 |
=cut |
| 229 |
|
| 230 |
__END__ |