Debian Codegraph @

lib/Finance/Quote/FTfunds.pm

raw
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 ([email protected])
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 require 5.005;
46
47 use strict;
48 use warnings;
49
50 # Set DEBUG => 0 for no debug messages, => 1 for first level, => 2 for 2nd level, etc.
51
52 use constant DEBUG => 0;
53
54 # URLs
55 use vars qw($VERSION $FTFUNDS_LOOK_UD $FTFUNDS_LOOK_LD $FTFUNDS_MAIN_URL);
56
57 use LWP::UserAgent;
58 use HTTP::Request::Common;
59 use HTTP::Cookies;
60 use HTML::TokeParser;
61 # use Data::Dumper;
62
63 our $VERSION = '1.71'; # VERSION
64
65 $FTFUNDS_MAIN_URL = "https://markets.ft.com";
66 $FTFUNDS_LOOK_LD = "https://markets.ft.com/data/funds/tearsheet/summary?s=";
67 $FTFUNDS_LOOK_UD = "http://funds.ft.com/UnlistedTearsheet/Summary?s=";
68
69 # this will work with ISIN codes only.
70
71 # FIXME -
72
73 our $DISPLAY = 'FTfunds - Financial Times, UK';
74 our @LABELS =
75 qw/name currency last date time price nav source iso_date method net p_change success errormsg/;
76 our $METHODHASH = {subroutine => \&ftfunds_fund,
77 display => $DISPLAY,
78 labels => \@LABELS};
79
80 sub methodinfo {
81 return (
82 ftfunds => $METHODHASH,
83 ukfunds => $METHODHASH,
84 );
85 }
86
87 sub labels {
88 my %m = methodinfo();
89 return map {$_ => [@{$m{$_}{labels}}] } keys %m;
90 }
91
92 sub methods {
93 my %m = methodinfo();
94 return map {$_ => $m{$_}{subroutine} } keys %m;
95 }
96
97 #
98 # =======================================================================
99
100 sub ftfunds_fund {
101 my $quoter = shift;
102 my @symbols = @_;
103
104 return unless @symbols;
105
106 my %fundquote;
107
108 my $ua = $quoter->user_agent;
109 my $cj = HTTP::Cookies->new();
110 $ua->cookie_jar( $cj );
111
112 foreach (@symbols)
113 {
114 my $code = $_;
115
116 my $code_type = "** Invalid **";
117 if ($code =~ m/^[a-zA-Z]{2}[a-zA-Z0-9]{9}\d(.*)/ && !$1) { $code_type = "ISIN"; }
118 elsif ($code =~ m/^[a-zA-Z0-9]{6}\d(.*)/ && !$1 ) { $code_type = "SEDOL"; }
119 elsif ($code =~ m/^[a-zA-Z0-9]{4,6}(.*)/ && !$1) { $code_type = "MEXID"; }
120
121 # current version can only use ISIN - report an error and exit if any other type
122
123 if ($code_type ne "ISIN")
124 {
125 $fundquote {$code,"success"} = 0;
126 $fundquote {$code,"errormsg"} = "Error - invalid symbol";
127 next;
128 }
129
130 $fundquote {$code,"success"} = 1; # ever the optimist....
131 $fundquote {$code,"errormsg"} = "Success";
132
133 # perform the look-up - if not found, return with error
134
135 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 my $webdoc = $ua->get($FTFUNDS_LOOK_LD.$code, @ua_headers);
143 if (!$webdoc->is_success)
144 {
145 # serious error, report it and give up
146 $fundquote {$code,"success"} = 0;
147 $fundquote {$code,"errormsg"} =
148 "Error - failed to retrieve fund data : HTTP status = ".$webdoc->status_line;
149 next;
150 }
151
152 DEBUG and print "\nTitle = ",$webdoc->title,"\n";
153 DEBUG and print "\nStatus = ",$webdoc->status_line, "\n";
154 # DEBUG > 1 and print "\nCookie Jar = : \n",Dumper($cj),"\n\n";
155
156 $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 if ($webdoc->content =~
162 m[<h2>(0 results)</h2>] )
163 {
164 DEBUG and print "\nTrying unlisted funds for ",$code," : ",$1,"\n";
165 $webdoc = $ua->get($FTFUNDS_LOOK_UD.$code, @ua_headers);
166 if (!$webdoc->is_success)
167 {
168 # serious error, report it and give up
169 $fundquote {$code,"success"} = 0;
170 $fundquote {$code,"errormsg"} =
171 "Error - failed to retrieve fund data : HTTP status = ".$webdoc->status_line;
172 next;
173 }
174
175 DEBUG and print "\nTitle = ",$webdoc->title,"\n";
176 DEBUG and print "\nStatus = ",$webdoc->status_line, "\n";
177 # DEBUG > 1 and print "\nCookie Jar = : \n",Dumper($cj),"\n\n";
178
179 $fundquote {$code, "source"} = $FTFUNDS_LOOK_UD.$code;
180 }
181
182 $fundquote {$code, "symbol"} = $code;
183
184 # Find name by simple regexp
185
186 my $name;
187 if ($webdoc->content =~
188 m[<title>(.*) [Ss]ummary - FT.com] )
189 {
190 $name = $1 ;
191 }
192 if (!defined($name)) {
193 # not a serious error - don't report it ....
194 # ... but set a useful message ....
195 $fundquote {$code,"errormsg"} = "Warning - failed to find fund name";
196 $name = "*** UNKNOWN ***";
197 # ... and continue
198 }
199 $fundquote {$code, "name"} = $name; # set name
200
201 # Find price and currency
202 my $currency;
203 my $price;
204 if ($webdoc->content =~
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 $currency = $1;
208 $price = $2;
209 }
210 if (!defined($currency)) {
211 # serious error, report it and give up
212 $fundquote {$code,"success"} = 0;
213 $fundquote {$code,"errormsg"} = "Error - failed to find a currency";
214 next;
215 }
216 if (!defined($price)) {
217 # serious error, report it and give up
218 $fundquote {$code,"success"} = 0;
219 $fundquote {$code,"errormsg"} = "Error - failed to find a price";
220 next;
221 }
222 if ($price =~ m[([0-9]*),([\.0-9]*)])
223 {
224 $price = $1 * 1000 + $2;
225 }
226
227 # Find net and percent-age change
228 my $net;
229 my $pchange;
230 if ($webdoc->content =~
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 $net = $1 ;
234 $pchange = $2;
235 }
236 if (!defined($net)) {
237 # not a serious error - don't report it ....
238 # $fundquote {$code,"success"} = 0;
239 # ... but set a useful message ....
240 $fundquote {$code,"errormsg"} = "Warning - failed to find a net change.";
241 $net = "-0.00"; # ???? is this OK ????
242 # ... and continue
243 }
244 if (!defined($pchange)) {
245 # not a serious error - don't report it ....
246 # $fundquote {$code,"success"} = 0;
247 # ... but set a useful message ....
248 $fundquote {$code,"errormsg"} = "Warning - failed to find a %-change";
249 $pchange = "-0.00"; # ???? is this OK ????
250 # ... and continue
251 }
252 if ($net =~ m[([0-9]*),([\.0-9]*)])
253 {
254 $net = $1 * 1000 + $2;
255 }
256 if ($pchange =~ m[([0-9]*),([\.0-9]*)])
257 {
258 $pchange = $1 * 1000 + $2;
259 }
260
261 # deal with GBX pricing of UK unit trusts
262 if ($currency eq "GBX")
263 {
264 $currency = "GBP" ;
265 $price = $price / 100 ;
266 $net = $net / 100 ;
267 }
268
269 # now set prices, net change and currency
270
271 $fundquote {$code, "price"} = $price;
272 $fundquote {$code, "last"} = $price;
273 $fundquote {$code, "nav"} = $price;
274 $fundquote {$code, "net"} = $net;
275 $fundquote {$code, "currency"} = $currency;
276 $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 my $time;
284 if ($webdoc->content =~ m[......... some string that will identify the time ............] )
285 {
286 if ($1 =~ m[(\d\d:\d\d)] ) # strip any trailing text (Timezone, etc.)
287 {
288 $time = $1;
289 }
290 }
291 if (!defined($time)) {
292 # not a serious error - don't report it ....
293 # $fundquote {$code,"success"} = 0;
294 # ... but set a useful message ....
295 $fundquote {$code,"errormsg"} = "Warning - failed to find a time";
296 $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 $fundquote {$code, "time"} = $time; # set time
302
303 # Find date
304
305 my $date;
306 if ($webdoc->content =~
307 m[([A-Za-z]{3}) ([0-9]{2}) ([0-9]{4})] )
308 {
309 $date = "$2/$1/$3" ;
310 }
311 if (!defined($date)) {
312 # not a serious error - don't report it ....
313 # $fundquote {$code,"success"} = 0;
314 # ... but set a useful message ....
315 $fundquote {$code,"errormsg"} = "Warning - failed to find a date";
316 # use today's date
317 $quoter->store_date(\%fundquote, $code, {today => 1});
318 # ... and continue
319 }
320 else
321 {
322 $quoter->store_date(\%fundquote, $code, {eurodate => $date});
323 }
324
325 $fundquote {$code, "method"} = "ftfunds"; # set method
326 sleep 1; # go to sleep for a while to give the web-site a breather
327
328 } # end of "foreach (@symbols)"
329
330 return wantarray ? %fundquote : \%fundquote;
331 }
332
333 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>[email protected]<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__