Quote/SwissFundData.pm

filter: Statementstmt 37.7% 37 / 98 Branchbran 0.0% 0 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 71.4% 10 / 14 Podpod 0.0% 0 / 4 totaltotal 34.0% 47 / 138 CC 13 SCAR 45.2
CC
13
Cov
31%
swissfunddata
182.0
__ANON__
2.0
__ANON__
2.0
CRAP 91.6
« Quote/Deka.pm ↑ Summary Quote/Bourso.pm »
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 (manuel@fritteli.ch)
9 #
10 # version: 0.1 Initial version - 27 July 2025
11 #
12 # This file is heavily based on MStaruk.pm by Martin Sadler
13 # (martinsadler@users.sourceforge.net), 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 5 require 5.006;
34  
35 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 # URLs
39 5 ▶use vars qw($VERSION $SFDCH_LOOK_UP $SFDCH_MAIN_URL);
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 5 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 5 ▶use HTTP::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 5 ▶use Web::Scraper;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 5 ▶use String::Util qw(trim);
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46  
47 5 $SFDCH_MAIN_URL = "https://www.swissfunddata.ch";
48 5 $SFDCH_LOOK_UP = "https://www.swissfunddata.ch/sfdpub/en/funds/prices?text=";
49  
50 5 our $VERSION = '1.72_02'; # TRIAL VERSION
51  
52 5 our $DISPLAY = "SwissFundData";
53 5 our @LABELS = qw/name currency date nav isodate method success errormsg/;
54 5 our $METHODHASH = {
55 subroutine => \&swissfunddata,
56 display => $DISPLAY,
57 labels => \@LABELS
58 };
59  
60 10 ▶sub methodinfo {
Subroutine methodinfo: called CC=1 SCAR=0.0
Pod undocumented
61 return (
62 10 swissfunddata => $METHODHASH,
63 );
64 }
65  
66 5 ▶sub labels {
Subroutine labels: called CC=1 SCAR=0.0
Pod undocumented
67 5 my %m = methodinfo();
68 5 return map {$_ => [@{$m{$_}{labels}}]} keys %m;
69 }
70  
71 5 ▶sub methods {
Subroutine methods: called CC=1 SCAR=0.0
Pod undocumented
72 5 my %m = methodinfo();
73 5 return map {$_ => $m{$_}{subroutine}} keys %m;
74 }
75  
76 0 ▶sub swissfunddata {
Subroutine swissfunddata: not called CC=13 SCAR=52.0
Pod undocumented
77 0 my $quoter = shift;
78 0 my @symbols = @_;
79  
80 0 ▶ return unless @symbols;
Branch: 1 branch
BranchTrueFalseTotal
unless @symbols 0 0 0
81  
82 0 my %info;
83  
84 0 my $cookie_jar = HTTP::Cookies->new;
85 0 $cookie_jar->set_cookie(0, "sfdpub-disclaimer", "private", "/sfdpub", "www.swissfunddata.ch", "443", 0, 0, 86400, 0);
86  
87 0 my $ua = $quoter->user_agent;
88 0 $ua->cookie_jar($cookie_jar);
89  
90 0 foreach (@symbols) {
91 0 my $symbol = $_;
92  
93 0 $info {$symbol, "source"} = "SwissFundData";
94 0 $info {$symbol, "success"} = 1; # ever the optimist....
95 0 $info {$symbol, "errormsg"} = "Success";
96  
97 # perform the look-up - if not found, return with error
98 0 my $reply = $ua->get($SFDCH_LOOK_UP.$symbol);
99 0 ▶ my $widget = scraper {
Subroutine __ANON__: not called CC=1 SCAR=6.9
100 0 ▶ process 'div#resultContainer table tbody tr:first-child td:nth-child(2)', 'name_isin' => ['HTML', sub {trim($_)}];
Subroutine __ANON__: not called CC=1 SCAR=6.9
101 0 ▶ process 'div#resultContainer table tbody tr:first-child td:nth-child(3)', 'nav_currency_date' => ['TEXT', sub {trim($_)}];
Subroutine __ANON__: not called CC=1 SCAR=6.9
102 0 };
103 0 my $text = $widget->scrape($reply);
104
105 0 ▶ unless (exists $text->{name_isin}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $text->{"name_isin"}) 0 0 0
106 # serious error, report it and give up
107 0 $info {$symbol, "success"} = 0;
108 0 $info {$symbol, "errormsg"} = "Error - failed to retrieve fund data: can not read name/isin.";
109 0 next;
110 }
111  
112 0 ▶ unless (exists $text->{nav_currency_date}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $text->{"nav_currency_date"}) 0 0 0
113 # serious error, report it and give up
114 0 $info {$symbol, "success"} = 0;
115 0 $info {$symbol, "errormsg"} = "Error - failed to retrieve basic fund data: can not read nav/currency/date";
116 0 next;
117 }
118  
119 0 my ($name, $isin);
120 0 ▶ if ($text->{name_isin} =~ m[<a href=".+">(.*)</a>.*<br />(.*)]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text->{"name_isin"} =~ m[<a href=".+">(.*)</a>.*<br />(.*)]) 0 0 0
121 0 $name = trim($1);
122 0 $isin = trim($2);
123 }
124
125 0 ▶ if (!defined($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 0 0
126 0 $name = "*** UNKNOWN ***";
127 }
128 0 ▶ if(!defined($isin)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $isin) 0 0 0
129 0 $isin = "*** UNKNOWN ***";
130 }
131  
132 0 my ($nav, $currency, $date);
133 0 ▶ if ($text->{nav_currency_date} =~ m[(\d+\.\d+) (\w+) (\d{2}\.\d{2}\.\d{4})]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text->{"nav_currency_date"} =~ /(\d+\.\d+) (\w+) (\d{2}\.\d{2}\.\d{4})/) 0 0 0
134 0 $nav = trim($1);
135 0 $currency = trim($2);
136 0 $date = trim($3);
137 }
138  
139 0 ▶ if (!defined($nav)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nav) 0 0 0
140 # serious error, report it and give up
141 0 $info {$symbol, "success"} = 0;
142 0 $info {$symbol, "errormsg"} = "Error - failed to retrieve fund data: missing 'nav'";
143 0 next;
144 }
145 0 ▶ if (!defined($currency)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $currency) 0 0 0
146 # serious error, report it and give up
147 0 $info {$symbol, "success"} = 0;
148 0 $info {$symbol, "errormsg"} = "Error - failed to retrieve fund data: missing 'currency'";
149 0 next;
150 }
151 0 ▶ if (!defined($date)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $date) 0 0 0
152 # serious error, report it and give up
153 0 $info {$symbol, "success"} = 0;
154 0 $info {$symbol, "errormsg"} = "Error - failed to retrieve fund data: missing 'date'";
155 0 next;
156 }
157
158 0 $info {$symbol, "name"} = $name;
159 0 $info {$symbol, "isin"} = $isin;
160 0 $info {$symbol, "nav"} = $nav;
161 0 $info {$symbol, "currency"} = $currency;
162 0 $quoter->store_date(\%info, $symbol, {eurodate => $date});
163 0 $info {$symbol, "method"} = "swissfunddata";
164 # It seems that GnuCash insists on having the time set?!
165 0 $info {$symbol, "time"} = "12:00";
166 }
167  
168 0 ▶ return wantarray ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
169 }
170  
171 5 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>manuel@fritteli.chE<gt>
217 Based heavily on the work of Martin Sadler E<lt>martinsadler@users.sourceforge.netE<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__
« Quote/Deka.pm ↑ Summary Quote/Bourso.pm »