/usr/share/perl5/Finance/Quote/FinanceCanada.pm is in libfinance-quote-perl 1.18-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | #!/usr/bin/perl -w
#
# FinanceCanada.pm
#
# Version 0.1 Initial version
#
# Version 0.2 Rewrite by David Hampton <hampton@employees.org> for
# changed web site.
#
package Finance::Quote::FinanceCanada;
require 5.004;
use strict;
use vars qw/ $VERSION / ;
use LWP::UserAgent;
use HTTP::Request::Common;
use HTML::TableExtract;
$VERSION = '1.18';
my $FINANCECANADA_MAINURL = ("http://finance.canada.com/");
my $FINANCECANADA_URL = "http://stockgroup.canada.com/sn_overview.asp?symbol=T.";
sub methods {
return (canada => \&financecanada,
financecanada => \&financecanada);
}
sub labels {
my @labels = qw/method source name symbol currency last date isodate nav price/;
return (canada => \@labels,
financecanada => \@labels);
}
sub financecanada {
my $quoter = shift;
my @symbols = @_;
my %info;
return unless @symbols;
my $ua = $quoter->user_agent;
foreach my $symbol (@symbols) {
my ($day_high, $day_low, $year_high, $year_low);
$info{$symbol, "success"} = 0;
$info{$symbol, "symbol"} = $symbol;
$info{$symbol, "method"} = "financecanada";
$info{$symbol, "source"} = $FINANCECANADA_MAINURL;
# Pull the data from the web site
my $url = $FINANCECANADA_URL.$symbol;
# print $url;
my $response = $ua->request(GET $url);
# print $response->content;
if (!$response->is_success) {
$info{$symbol, "errormsg"} = "Error contacting URL";
next;
}
# Parse the page looking for the table containing the full
# name of the stock
my $te = new HTML::TableExtract( depth => 2, count => 0);
$te->parse($response->content);
# debug
# foreach my $ts ($te->table_states) {
# print "\n***\n*** Table (", join(',', $ts->coords), "):\n***\n";
# foreach my $row ($ts->rows) {
# print join(',', @$row), "\n";
# }
# }
foreach my $ts ($te->table_states) {
my $row = $ts->row(0);
$info{$symbol, "name"} = $row->[0]
if ($row->[0] =~ s/^.([\w\s]+).*/$1/);
}
if (!defined($info{$symbol, "name"})) {
$info{$symbol, "errormsg"} = "Invalid symbol";
next;
}
# Parse the page looking for the table containing the quote
# details
$te = new HTML::TableExtract(headers => [qw(Quote)],
slice_columns => 0);
$te->parse($response->content);
# debug
# foreach my $ts ($te->table_states) {
# print "\n***\n*** Table (", join(',', $ts->coords), "):\n***\n";
# foreach my $row ($ts->rows) {
# print join(',', @$row), "\n";
# }
# }
# Now parse the quote details. This method of parsing is
# independent of which row contains which data item, so if the
# web site reorders these it won't impact this code.
foreach my $ts ($te->table_states) {
foreach my $row ($ts->rows) {
# Remove leading and trailing white space
$row->[0] =~ s/^\s*(.+?)\s*$/$1/ if defined($row->[0]);
$row->[1] =~ s/^\s*(.+?)\s*$/$1/ if defined($row->[1]);
# Map the row into our data array
for ($row->[0]) {
/^Last Traded/ && do { s/Last Traded: (.*) ../$1/;
$quoter->store_date(\%info, $symbol, { usdate => $_}); };
/^Last$/ && do { $info{$symbol, "last"} = $row->[1];
$info{$symbol, "price"} = $row->[1];
$info{$symbol, "nav"} = $row->[1];
last; };
/^Open$/ && do { $info{$symbol, "open"} = $row->[1]; last; };
/^Bid$/ && do { $info{$symbol, "bid"} = $row->[1]; last; };
/^Ask$/ && do { $info{$symbol, "ask"} = $row->[1]; last; };
/^% Change/ && do { $info{$symbol, "p_change"} = $row->[1];
$info{$symbol, "p_change"} =~ s/%//;
last; };
/^Volume/ && do { $info{$symbol, "volume"} = $row->[1]; last; };
/^Close/ && do { $info{$symbol, "close"} = $row->[1]; last; };
/^Day High$/ && do { $info{$symbol, "high"} = $row->[1]; last; };
/^Day Low$/ && do { $info{$symbol, "low"} = $row->[1]; last; };
/^Year High$/ && do { $year_high = $row->[1]; last; };
/^Year Low$/ && do { $year_low = $row->[1]; last; };
$info{$symbol, "success"} = 1;
};
}
}
if ($info{$symbol, "success"} == 1) {
$info{$symbol, "currency"} = "CAD";
foreach (keys %info) {
$info{$_} =~ s/\$//;
}
$info{$symbol, "day_range"} = $info{$symbol, "low"} . " - " . $info{$symbol, "high"}
if (defined($info{$symbol, "high"}) && defined($info{$symbol, "low"}));
if (defined($year_high) && defined($year_low)) {
$info{$symbol, "year_range"} = "$year_low - $year_high";
}
} else {
$info{$symbol, "errormsg"} = "Cannot parse quote data";
}
}
return wantarray() ? %info : \%info;
}
1;
=head1 NAME
Finance::Quote::FinanceCanada - Obtain stock and mutual fund prices from
finance.canada.com
=head1 SYNOPSIS
use Finance::Quote;
$q = Finance::Quote->new;
# Can failover to other methods
%quotes = $q->fetch("canada", "stock_fund-code");
# Use this module only
%quotes = $q->fetch("financecanada", "stock_fund-code");
=head1 DESCRIPTION
This module obtains information about Canadian Stock and Mutual Funds from
finanace.canada.com. The information source "canada" can be used if the
information source is unimportant, or "financecanada" to specifically use
finance.canada.com.
=head1 STOCK_FUND-CODE
Canadian stocks/mutual funds do not have a unique symbol identifier. This
module uses the symbols as used on finance.canada.com. The simplest way
to fetch the ID for a particular stock/fund is to go to finance.canada.com,
search for your particular stock or mutual fund, and note the symbol ID.
This is helpfully provided by the site in their returned HTML quote.
=head1 LABELS RETURNED
Information available from financecanada may include the following labels:
method source name symbol currency date nav last price
=head1 SEE ALSO
Finance Canada.com website - http://finance.canada.com/
Finance::Quote
=cut
|