Quote/FTfunds.pm

filter: Statementstmt 28.4% 41 / 144 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 91.6% 11 / 12 Podpod 0.0% 0 / 4 totaltotal 22.9% 52 / 227 CC 31 SCAR 66.0
CC
31
Cov
20%
ftfunds_fund
992.0
BEGIN
1.0
BEGIN
1.0
CRAP 732.5
« Quote/Comdirect.pm ↑ Summary Quote/Consorsbank.pm »
1 #!/usr/bin/perl -w
2 # vi: set ts=2 sw=2 noai expandtab ic showmode showmatch:
3  
4 # ftfunds.pm
5 #
6 # Obtains quotes for UK Unit Trusts from http://funds.ft.com/ - please
7 # refer to the end of this file for further information.
8 #
9 # author: Martin Sadler (martinsadler@users.sourceforge.net)
10 #
11 # Version 0.1 Initial version - 06 Sep 2010
12 #
13 # Version 0.2 Better look-up - 19 Sep 2010
14 #
15 # Version 0.3 name changed to "ftfunds"
16 # (all lower case) and tidy-up - 31 Jan 2011
17 #
18 # Version 0.4 Allows alphanumerics MEXIDs
19 # and back to "FTfunds" - 28 Feb 2011
20 #
21 # Version 1.0 Changed to work with the new
22 # format of funds.ft.com - 14 Sep 2011
23 #
24 # Version 2.0 Changed to work with the latest
25 # format of funds.ft.com - 31 Mar 2013
26 #
27 # This program is free software; you can redistribute it and/or modify
28 # it under the terms of the GNU General Public License as published by
29 # the Free Software Foundation; either version 2 of the License, or
30 # (at your option) any later version.
31 #
32 # This program is distributed in the hope that it will be useful,
33 # but WITHOUT ANY WARRANTY; without even the implied warranty of
34 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 # GNU General Public License for more details.
36 #
37 # You should have received a copy of the GNU General Public License
38 # along with this program; if not, write to the Free Software
39 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
40 # 02110-1301, USA
41 #
42  
43  
44 package Finance::Quote::FTfunds;
45 5 require 5.005;
46  
47 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 # Set DEBUG => 0 for no debug messages, => 1 for first level, => 2 for 2nd level, etc.
51  
52 5 ▶use constant DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 # URLs
55 5 ▶use vars qw($VERSION $FTFUNDS_LOOK_UD $FTFUNDS_LOOK_LD $FTFUNDS_MAIN_URL);
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 5 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 5 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 5 ▶use HTTP::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 5 ▶use HTML::TokeParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 # use Data::Dumper;
62  
63 5 our $VERSION = '1.72_01'; # TRIAL VERSION
64  
65 5 $FTFUNDS_MAIN_URL = "https://markets.ft.com";
66 5 $FTFUNDS_LOOK_LD = "https://markets.ft.com/data/funds/tearsheet/summary?s=";
67 5 $FTFUNDS_LOOK_UD = "http://funds.ft.com/UnlistedTearsheet/Summary?s=";
68  
69 # this will work with ISIN codes only.
70  
71 # FIXME -
72  
73 5 our $DISPLAY = 'FTfunds - Financial Times, UK';
74 5 our @LABELS =
75 qw/name currency last date time price nav source iso_date method net p_change success errormsg/;
76 5 our $METHODHASH = {subroutine => \&ftfunds_fund,
77 display => $DISPLAY,
78 labels => \@LABELS};
79  
80 10 ▶sub methodinfo {
Subroutine methodinfo: called CC=1 SCAR=0.0
Pod undocumented
81 return (
82 10 ftfunds => $METHODHASH,
83 ukfunds => $METHODHASH,
84 );
85 }
86  
87 5 ▶sub labels {
Subroutine labels: called CC=1 SCAR=0.0
Pod undocumented
88 5 my %m = methodinfo();
89 5 return map {$_ => [@{$m{$_}{labels}}] } keys %m;
90 }
91  
92 5 ▶sub methods {
Subroutine methods: called CC=1 SCAR=0.0
Pod undocumented
93 5 my %m = methodinfo();
94 5 return map {$_ => $m{$_}{subroutine} } keys %m;
95 }
96  
97 #
98 # =======================================================================
99  
100 0 ▶sub ftfunds_fund {
Subroutine ftfunds_fund: not called CC=31 SCAR=69.0
Pod undocumented
101 0 my $quoter = shift;
102 0 my @symbols = @_;
103  
104 0 ▶ return unless @symbols;
Branch: 1 branch
BranchTrueFalseTotal
unless @symbols 0 0 0
105  
106 0 my %fundquote;
107  
108 0 my $ua = $quoter->user_agent;
109 0 my $cj = HTTP::Cookies->new();
110 0 $ua->cookie_jar( $cj );
111  
112 0 foreach (@symbols)
113 {
114 0 my $code = $_;
115  
116 0 my $code_type = "** Invalid **";
117 0 ▶ if ($code =~ m/^[a-zA-Z]{2}[a-zA-Z0-9]{9}\d(.*)/ && !$1) { $code_type = "ISIN"; }
Branch: 3 branches
BranchTrueFalseTotal
if ($code =~ /^[a-zA-Z]{2}[a-zA-Z0-9]{9}\d(.*)/ and not $1) { } 0 0 0
elsif ($code =~ /^[a-zA-Z0-9]{6}\d(.*)/ and not $1) { } 0 0 0
elsif ($code =~ /^[a-zA-Z0-9]{4,6}(.*)/ and not $1) { } 0 0 0
Condition0%0/3
$code =~ /^[a-zA-Z]{2}[a-zA-Z0-9]{9}\d(.*)/ and not $1
!ll&&!rl&&r
000
MC/DC0%0/2
$code =~ /^[a-zA-Z]{2}[a-zA-Z0-9]{9}\d(.*)/ and not $1
$code =~ /^[a-zA-Z]{2}[a-zA-Z0-9]{9}\d(.*)/ not $1
Truth table0%0/3
A and not(B)
A= $code =~ /^[a-zA-Z]{2}[a-zA-Z0-9]{9}\d(.*)/B= not $1
ABresult
0X0
100
111
118 0 ▶ elsif ($code =~ m/^[a-zA-Z0-9]{6}\d(.*)/ && !$1 ) { $code_type = "SEDOL"; }
Condition0%0/3
$code =~ /^[a-zA-Z0-9]{6}\d(.*)/ and not $1
!ll&&!rl&&r
000
MC/DC0%0/2
$code =~ /^[a-zA-Z0-9]{6}\d(.*)/ and not $1
$code =~ /^[a-zA-Z0-9]{6}\d(.*)/ not $1
Truth table0%0/3
A and not(B)
A= $code =~ /^[a-zA-Z0-9]{6}\d(.*)/B= not $1
ABresult
0X0
100
111
119 0 ▶ elsif ($code =~ m/^[a-zA-Z0-9]{4,6}(.*)/ && !$1) { $code_type = "MEXID"; }
Condition0%0/3
$code =~ /^[a-zA-Z0-9]{4,6}(.*)/ and not $1
!ll&&!rl&&r
000
MC/DC0%0/2
$code =~ /^[a-zA-Z0-9]{4,6}(.*)/ and not $1
$code =~ /^[a-zA-Z0-9]{4,6}(.*)/ not $1
Truth table0%0/3
A and not(B)
A= $code =~ /^[a-zA-Z0-9]{4,6}(.*)/B= not $1
ABresult
0X0
100
111
120  
121 # current version can only use ISIN - report an error and exit if any other type
122  
123 0 ▶ if ($code_type ne "ISIN")
Branch: 1 branch
BranchTrueFalseTotal
if ($code_type ne "ISIN") 0 0 0
124 {
125 0 $fundquote {$code,"success"} = 0;
126 0 $fundquote {$code,"errormsg"} = "Error - invalid symbol";
127 0 next;
128 }
129  
130 0 $fundquote {$code,"success"} = 1; # ever the optimist....
131 0 $fundquote {$code,"errormsg"} = "Success";
132  
133 # perform the look-up - if not found, return with error
134  
135 0 my @ua_headers = (
136 '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',
137 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
138 );
139  
140 # try listed funds first...
141  
142 0 my $webdoc = $ua->get($FTFUNDS_LOOK_LD.$code, @ua_headers);
143 0 ▶ if (!$webdoc->is_success)
Branch: 1 branch
BranchTrueFalseTotal
unless ($webdoc->is_success) 0 0 0
144 {
145 # serious error, report it and give up
146 0 $fundquote {$code,"success"} = 0;
147 0 $fundquote {$code,"errormsg"} =
148 "Error - failed to retrieve fund data : HTTP status = ".$webdoc->status_line;
149 0 next;
150 }
151  
152 0 DEBUG and print "\nTitle = ",$webdoc->title,"\n";
153 0 DEBUG and print "\nStatus = ",$webdoc->status_line, "\n";
154 # DEBUG > 1 and print "\nCookie Jar = : \n",Dumper($cj),"\n\n";
155  
156 0 $fundquote {$code, "source"} = $FTFUNDS_LOOK_LD.$code;
157  
158 # if page contains "<h2>0 results</h2>" it's not found...
159 # ... try unlisted funds...
160  
161 0 ▶ if ($webdoc->content =~
Branch: 1 branch
BranchTrueFalseTotal
if ($webdoc->content =~ m[<h2>(0 results)</h2>]) 0 0 0
162 m[<h2>(0 results)</h2>] )
163 {
164 0 DEBUG and print "\nTrying unlisted funds for ",$code," : ",$1,"\n";
165 0 $webdoc = $ua->get($FTFUNDS_LOOK_UD.$code, @ua_headers);
166 0 ▶ if (!$webdoc->is_success)
Branch: 1 branch
BranchTrueFalseTotal
unless ($webdoc->is_success) 0 0 0
167 {
168 # serious error, report it and give up
169 0 $fundquote {$code,"success"} = 0;
170 0 $fundquote {$code,"errormsg"} =
171 "Error - failed to retrieve fund data : HTTP status = ".$webdoc->status_line;
172 0 next;
173 }
174  
175 0 DEBUG and print "\nTitle = ",$webdoc->title,"\n";
176 0 DEBUG and print "\nStatus = ",$webdoc->status_line, "\n";
177 # DEBUG > 1 and print "\nCookie Jar = : \n",Dumper($cj),"\n\n";
178  
179 0 $fundquote {$code, "source"} = $FTFUNDS_LOOK_UD.$code;
180 }
181  
182 0 $fundquote {$code, "symbol"} = $code;
183  
184 # Find name by simple regexp
185  
186 0 my $name;
187 0 ▶ if ($webdoc->content =~
Branch: 1 branch
BranchTrueFalseTotal
if ($webdoc->content =~ /<title>(.*) [Ss]ummary - FT.com/) 0 0 0
188 m[<title>(.*) [Ss]ummary - FT.com] )
189 {
190 0 $name = $1 ;
191 }
192 0 ▶ if (!defined($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 0 0
193 # not a serious error - don't report it ....
194 # ... but set a useful message ....
195 0 $fundquote {$code,"errormsg"} = "Warning - failed to find fund name";
196 0 $name = "*** UNKNOWN ***";
197 # ... and continue
198 }
199 0 $fundquote {$code, "name"} = $name; # set name
200  
201 # Find price and currency
202 0 my $currency;
203 my $price;
204 0 ▶ if ($webdoc->content =~
Branch: 1 branch
BranchTrueFalseTotal
if ($webdoc->content =~ m[<span class="mod-ui-data-list__label"[^>]*>Price [(]([A-Z]{3})[)]</span><span class="mod-ui-data-list__value">([\.\,0-9]*)</span>]) 0 0 0
205 m[<span class="mod-ui-data-list__label"[^>]*>Price [(]([A-Z]{3})[)]</span><span class="mod-ui-data-list__value">([\.\,0-9]*)</span>] )
206 {
207 0 $currency = $1;
208 0 $price = $2;
209 }
210 0 ▶ if (!defined($currency)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $currency) 0 0 0
211 # serious error, report it and give up
212 0 $fundquote {$code,"success"} = 0;
213 0 $fundquote {$code,"errormsg"} = "Error - failed to find a currency";
214 0 next;
215 }
216 0 ▶ if (!defined($price)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $price) 0 0 0
217 # serious error, report it and give up
218 0 $fundquote {$code,"success"} = 0;
219 0 $fundquote {$code,"errormsg"} = "Error - failed to find a price";
220 0 next;
221 }
222 0 ▶ if ($price =~ m[([0-9]*),([\.0-9]*)])
Branch: 1 branch
BranchTrueFalseTotal
if ($price =~ /([0-9]*),([\.0-9]*)/) 0 0 0
223 {
224 0 $price = $1 * 1000 + $2;
225 }
226  
227 # Find net and percent-age change
228 0 my $net;
229 my $pchange;
230 0 ▶ if ($webdoc->content =~
Branch: 1 branch
BranchTrueFalseTotal
if ($webdoc->content =~ m[<span class="mod-ui-data-list__label">Today's Change</span><span class="mod-ui-data-list__value"><span [^>]*><i [^>]*></i>(-?[\.0-9]*) / (-?[\.0-9]*)%</span>]) 0 0 0
231 m[<span class="mod-ui-data-list__label">Today's Change</span><span class="mod-ui-data-list__value"><span [^>]*><i [^>]*></i>(-?[\.0-9]*) / (-?[\.0-9]*)%</span>] )
232 {
233 0 $net = $1 ;
234 0 $pchange = $2;
235 }
236 0 ▶ if (!defined($net)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $net) 0 0 0
237 # not a serious error - don't report it ....
238 # $fundquote {$code,"success"} = 0;
239 # ... but set a useful message ....
240 0 $fundquote {$code,"errormsg"} = "Warning - failed to find a net change.";
241 0 $net = "-0.00"; # ???? is this OK ????
242 # ... and continue
243 }
244 0 ▶ if (!defined($pchange)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pchange) 0 0 0
245 # not a serious error - don't report it ....
246 # $fundquote {$code,"success"} = 0;
247 # ... but set a useful message ....
248 0 $fundquote {$code,"errormsg"} = "Warning - failed to find a %-change";
249 0 $pchange = "-0.00"; # ???? is this OK ????
250 # ... and continue
251 }
252 0 ▶ if ($net =~ m[([0-9]*),([\.0-9]*)])
Branch: 1 branch
BranchTrueFalseTotal
if ($net =~ /([0-9]*),([\.0-9]*)/) 0 0 0
253 {
254 0 $net = $1 * 1000 + $2;
255 }
256 0 ▶ if ($pchange =~ m[([0-9]*),([\.0-9]*)])
Branch: 1 branch
BranchTrueFalseTotal
if ($pchange =~ /([0-9]*),([\.0-9]*)/) 0 0 0
257 {
258 0 $pchange = $1 * 1000 + $2;
259 }
260  
261 # deal with GBX pricing of UK unit trusts
262 0 ▶ if ($currency eq "GBX")
Branch: 1 branch
BranchTrueFalseTotal
if ($currency eq "GBX") 0 0 0
263 {
264 0 $currency = "GBP" ;
265 0 $price = $price / 100 ;
266 0 $net = $net / 100 ;
267 }
268  
269 # now set prices, net change and currency
270  
271 0 $fundquote {$code, "price"} = $price;
272 0 $fundquote {$code, "last"} = $price;
273 0 $fundquote {$code, "nav"} = $price;
274 0 $fundquote {$code, "net"} = $net;
275 0 $fundquote {$code, "currency"} = $currency;
276 0 $fundquote {$code, "p_change"} = $pchange; # set %-change
277  
278 # Find time
279  
280 # NB. version 2.0 - there is no time quoted on the current (31/3/2013) factsheet page for unit trusts
281 # ... this code left in in case it is available in later revisions of the page
282  
283 0 my $time;
284 0 ▶ if ($webdoc->content =~ m[......... some string that will identify the time ............] )
Branch: 1 branch
BranchTrueFalseTotal
if ($webdoc->content =~ /......... some string that will identify the time ............/) 0 0 0
285 {
286 0 ▶ if ($1 =~ m[(\d\d:\d\d)] ) # strip any trailing text (Timezone, etc.)
Branch: 1 branch
BranchTrueFalseTotal
if ($1 =~ /(\d\d:\d\d)/) 0 0 0
287 {
288 0 $time = $1;
289 }
290 }
291 0 ▶ if (!defined($time)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $time) 0 0 0
292 # not a serious error - don't report it ....
293 # $fundquote {$code,"success"} = 0;
294 # ... but set a useful message ....
295 0 $fundquote {$code,"errormsg"} = "Warning - failed to find a time";
296 0 $time = "17:00"; # set to 17:00 if no time supplied ???
297 # gnucash insists on having a valid-format
298 # ... and continue
299 }
300  
301 0 $fundquote {$code, "time"} = $time; # set time
302  
303 # Find date
304  
305 0 my $date;
306 0 ▶ if ($webdoc->content =~
Branch: 1 branch
BranchTrueFalseTotal
if ($webdoc->content =~ /([A-Za-z]{3}) ([0-9]{2}) ([0-9]{4})/) 0 0 0
307 m[([A-Za-z]{3}) ([0-9]{2}) ([0-9]{4})] )
308 {
309 0 $date = "$2/$1/$3" ;
310 }
311 0 ▶ if (!defined($date)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $date) { } 0 0 0
312 # not a serious error - don't report it ....
313 # $fundquote {$code,"success"} = 0;
314 # ... but set a useful message ....
315 0 $fundquote {$code,"errormsg"} = "Warning - failed to find a date";
316 # use today's date
317 0 $quoter->store_date(\%fundquote, $code, {today => 1});
318 # ... and continue
319 }
320 else
321 {
322 0 $quoter->store_date(\%fundquote, $code, {eurodate => $date});
323 }
324  
325 0 $fundquote {$code, "method"} = "ftfunds"; # set method
326 0 sleep 1; # go to sleep for a while to give the web-site a breather
327  
328 } # end of "foreach (@symbols)"
329  
330 0 ▶ return wantarray ? %fundquote : \%fundquote;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
331 }
332  
333 5 1;
334  
335 =head1 NAME
336  
337 Finance::Quote::FTfunds - Obtain UK Unit Trust quotes from FT.com (Financial Times).
338  
339 =head1 SYNOPSIS
340  
341 $q = Finance::Quote->new;
342  
343 %info = Finance::Quote->fetch("ftfunds","<isin> ..."); # Only query FT.com using ISINs
344  
345 =head1 DESCRIPTION
346  
347 This module fetches information from the Financial Times Funds service,
348 http://funds.ft.com. There are over 47,000 UK Unit Trusts and OEICs quoted,
349 as well as many Offshore Funds and Exhange Traded Funds (ETFs). It converts
350 any funds quoted in GBX (pence) to GBP, dividing the price by 100 in the
351 process.
352  
353 Funds are identified by their ISIN code, a 12 character alphanumeric code.
354 Although the web site also allows searching by fund name, this version of
355 Finance::Quote::FTfunds only implements ISIN lookup. To determine the ISIN for
356 funds of interest to you, visit the funds.ft.com site and use the flexible search
357 facilities to identify the funds of interest. The factsheet display for any given
358 fund displays the ISIN along with other useful information.
359  
360 This module is loaded by default on a Finance::Quote object. It's
361 also possible to load it explicitly by placing "ftfunds" in the argument
362 list to Finance::Quote->new().
363  
364 Information obtained by this module may be covered by funds.ft.com
365 terms and conditions See http://funds.ft.com/ and http://ft.com for details.
366  
367 =head2 Stocks And Indices
368  
369 This module provides both the "ftfunds" and "ukfunds" fetch methods for
370 fetching UK and Offshore Unit Trusts and OEICs prices and other information
371 from funds.ft.com. Please use the "ukfunds" fetch method if you wish to have
372 failover with future sources for UK and Offshore Unit Trusts and OEICs - the
373 author has plans to develop Finance::Quote modules for the London Stock Exchange
374 and Morningstar unit trust services. Using the "ftfunds" method will guarantee
375 that your information only comes from the funds.ft.com website.
376  
377 =head1 LABELS RETURNED
378  
379 The following labels may be returned by Finance::Quote::ftfunds :
380  
381 name, currency, last, date, time, price, nav, source, method,
382 iso_date, net, p_change, success, errormsg.
383  
384  
385 =head1 SEE ALSO
386  
387 Financial Times websites, http://ft.com and http://funds.ft.com
388  
389  
390 =head1 AUTHOR
391  
392 Martin Sadler, E<lt>martinsadler@users.sourceforge.netE<gt>
393  
394 =head1 COPYRIGHT AND LICENSE
395  
396 Copyright (C) 2010 by Martin Sadler
397  
398 This library is free software; you can redistribute it and/or modify
399 it under the same terms as Perl itself, either Perl version 5.10.1 or,
400 at your option, any later version of Perl 5 you may have available.
401  
402  
403 =cut
404  
405 __END__
« Quote/Comdirect.pm ↑ Summary Quote/Consorsbank.pm »