App/Chart/Finance/Yahoo/Exchanges.pm untested

filter: Statementstmt 0.0% 0 / 73 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 113 CC 8 SCAR 24.5
CC
8
Cov
0%
symbol_quote_delay
30.0
parse_html
20.0
exchanges_data
2.0
CRAP 11.6
1 # Copyright 2007, 2008, 2009, 2010, 2017 Kevin Ryde
2  
3 # This file is part of Chart.
4 #
5 # Chart is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3, or (at your option) any later version.
8 #
9 # Chart is distributed in the hope that it will be useful, but WITHOUT ANY
10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 # details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Chart. If not, see <http://www.gnu.org/licenses/>.
16  
17  
18 # Currently unused ...
19  
20  
21 package App::Chart::Finance::Yahoo::Exchanges;
22 use 5.006;
23 use strict;
24 use warnings;
25 use Locale::TextDomain ('App-Chart');
26  
27  
28 # This looks at the exchanges page
29 #
30 # use constant URL => 'http://finance.yahoo.com/exchanges';
31 use constant URL => 'https://help.yahoo.com/kb/SLN2310.html';
32  
33 # Refetch the exchanges page after this many days
34 #
35 use constant UPDATE_DAYS => 7;
36  
37 # return a hashref of exchange delay data like { '.AX' => 20, '.BI' => 15 }
38 sub exchanges_data {
39 require App::Chart::Pagebits;
40 return App::Chart::Pagebits::get
41 (name => __('Yahoo exchanges page'),
42 url => URL,
43 key => 'yahoo-quote-delays',
44 freq_days => UPDATE_DAYS,
45 parse => \&parse_html);
46 }
47  
48 sub parse_html {
49 my ($content) = @_;
50 my @entries;
51 my $h = { entries => \@entries,
52 timestamp => timestamp_now(),
53 };
54  
55 require HTML::TableExtract;
56 my $te = HTML::TableExtract->new
57 (headers => [ 'Country',
58 'Exchange',
59 'Suffix',
60 'Delay',
61 'Provider' ]);
62  
63 $te->parse($content);
64 if (! $te->tables) {
65 warn "Yahoo exchanges page unrecognised, assuming 15 min quote delay";
66 return $h;
67 }
68  
69 foreach my $row ($te->rows) {
70 my %entry;
71 @entry{'country','exchange','suffix','delay_minutes','provider'} = @$row;
72  
73 # wash two spaces in "Chicago Mercantile ..." to one
74 foreach (values %entry) { s/\s+/ /g; }
75  
76 # US suffix N/A means no suffix
77 if ($entry{'suffix'} eq 'N/A') { $entry{'suffix'} = ''; }
78  
79 # Delay like "15 min", or "15 min**" with footnote for NS India
80 #
81 if ($entry{'delay_minutes'} =~ /^(\d+) min/) {
82 $entry{'delay_minutes'} = $1;
83 } else {
84 warn "Yahoo exchanges page unrecognised delay: \"$entry{delay_minutes}\"\n";
85 $entry{'delay_minutes'} = 20; # assumed default
86 }
87  
88 push @entries, \%entry;
89 }
90 return $h;
91 }
92  
93 sub timestamp_now {
94 return timet_to_timestamp(time());
95 }
96 sub timet_to_timestamp {
97 my ($t) = @_;
98 require POSIX;
99 return POSIX::strftime ('%Y-%m-%d %H:%M:%S+00:00', gmtime($t));
100 }
101  
102  
103 #------------------------------------------------------------------------------
104 # containing arefs [$pred,'.XX']
105 my @quote_delay_aliases;
106  
107 sub setup_quote_delay_alias {
108 my ($pred, $suffix) = @_;
109 push @quote_delay_aliases, [ $pred, $suffix ];
110 }
111  
112 sub symbol_quote_delay {
113 my ($symbol) = @_;
114  
115 # indexes all in real time
116 if ($symbol =~ /^\^/) {
117 return 0;
118 }
119  
120 my $suffix = ($symbol =~ /(\.[^.]+)$/ && $1);
121 my $h = exchanges_data();
122 my $delay = $h->{$suffix};
123  
124 if (! defined $delay) {
125 if (my $elem = List::Util::first { $_->[0]->match ($symbol) }
126 @quote_delay_aliases) {
127 $suffix = $elem->[1];
128 $delay = $h->{$suffix};
129 }
130 }
131 if (! defined $delay) {
132 # guess default 20 minutes
133 $delay = 20;
134 }
135 return $delay;
136 }
137  
138 1;
139 __END__