| 1 |
#!/usr/bin/perl -w |
| 2 |
# vi: set ts=2 sw=2 noai ic showmode showmatch: |
| 3 |
# |
| 4 |
# Copyright (C) 2023, Bruce Schuck <[email protected]> |
| 5 |
# |
| 6 |
# This program is free software; you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation; either version 2 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program; if not, write to the Free Software |
| 18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 19 |
# 02110-1301, USA |
| 20 |
# |
| 21 |
|
| 22 |
package Finance::Quote::GoogleWeb; |
| 23 |
|
| 24 |
use strict; |
| 25 |
use warnings; |
| 26 |
|
| 27 |
use Encode qw(decode); |
| 28 |
use HTTP::Request::Common; |
| 29 |
|
| 30 |
use constant DEBUG => $ENV{DEBUG}; |
| 31 |
use if DEBUG, 'Smart::Comments', '###'; |
| 32 |
|
| 33 |
our $VERSION = '1.71'; # VERSION |
| 34 |
|
| 35 |
my $GOOGLE_URL = 'https://www.google.com/finance/quote/'; |
| 36 |
|
| 37 |
# Endpoint used by the Google Finance search box to resolve a ticker |
| 38 |
# symbol to its primary listing (exchange). It returns the same |
| 39 |
# autocomplete suggestions a user sees while typing in the search box. |
| 40 |
my $SEARCH_URL = 'https://www.google.com/complete/search?client=finance-immersive&q='; |
| 41 |
|
| 42 |
our $DISPLAY = 'GoogleWeb - Scrapes www.google.com/finance/'; |
| 43 |
our @LABELS = qw/symbol name last date currency method/; |
| 44 |
our $METHODHASH = {subroutine => \&googleweb, |
| 45 |
display => $DISPLAY, |
| 46 |
labels => \@LABELS}; |
| 47 |
|
| 48 |
sub methodinfo { |
| 49 |
return ( |
| 50 |
googleweb => $METHODHASH, |
| 51 |
bats => $METHODHASH, |
| 52 |
nyse => $METHODHASH, |
| 53 |
nasdaq => $METHODHASH, |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
sub labels { |
| 58 |
my %m = methodinfo(); |
| 59 |
return map {$_ => [@{$m{$_}{labels}}] } keys %m; |
| 60 |
} |
| 61 |
|
| 62 |
sub methods { |
| 63 |
my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m; |
| 64 |
} |
| 65 |
|
| 66 |
sub googleweb { |
| 67 |
|
| 68 |
my $quoter = shift; |
| 69 |
my @stocks = @_; |
| 70 |
my (%info, $url, $reply); |
| 71 |
my $ua = $quoter->user_agent(); |
| 72 |
|
| 73 |
foreach my $stock (@stocks) { |
| 74 |
|
| 75 |
my $ucstock = uc($stock); |
| 76 |
|
| 77 |
$info{ $stock, "symbol" } = $stock; |
| 78 |
|
| 79 |
# --------------------------------------------------------------- |
| 80 |
# Step 1: resolve the symbol to its primary exchange. |
| 81 |
# |
| 82 |
# The Google Finance quote URL requires the exchange to be appended |
| 83 |
# (e.g. AAPL:NASDAQ). The plain quote/AAPL page no longer returns a |
| 84 |
# list of disambiguation links, so instead we query the same |
| 85 |
# autocomplete endpoint the search box uses. Its first suggestion |
| 86 |
# for an exact ticker match is the primary listing. |
| 87 |
# --------------------------------------------------------------- |
| 88 |
$url = $SEARCH_URL . $ucstock; |
| 89 |
$reply = $ua->request( GET $url ); |
| 90 |
|
| 91 |
my $code = $reply->code; |
| 92 |
my $desc = HTTP::Status::status_message($code); |
| 93 |
|
| 94 |
unless ( $code == 200 ) { |
| 95 |
$info{ $stock, "success" } = 0; |
| 96 |
$info{ $stock, "errormsg" } = |
| 97 |
"Error retrieving quote for $stock. Attempt to fetch the URL $url resulted in HTTP response $code ($desc)"; |
| 98 |
next; |
| 99 |
} |
| 100 |
|
| 101 |
my $body = decode('UTF-8', $reply->content); |
| 102 |
|
| 103 |
### Search Body: $body |
| 104 |
|
| 105 |
my ($name, $last, $date, $currency, $time, $exchange); |
| 106 |
|
| 107 |
# Each suggestion carries a small metadata object with the ticker |
| 108 |
# ("t"), exchange ("x") and company name ("c"). Pick the first one |
| 109 |
# whose ticker matches the requested symbol exactly. |
| 110 |
while ( $body =~ /\{([^{}]*)\}/g ) { |
| 111 |
my $obj = $1; |
| 112 |
next unless $obj =~ /"t":"([^"]*)"/ && uc($1) eq $ucstock; |
| 113 |
($exchange) = $obj =~ /"x":"([^"]*)"/; |
| 114 |
($name) = $obj =~ /"c":"([^"]*)"/; |
| 115 |
last if $exchange; |
| 116 |
} |
| 117 |
|
| 118 |
unless ($exchange) { |
| 119 |
$info{ $stock, "success" } = 0; |
| 120 |
$info{ $stock, "errormsg" } = "$stock not found on Google Finance"; |
| 121 |
next; |
| 122 |
} |
| 123 |
|
| 124 |
# --------------------------------------------------------------- |
| 125 |
# Step 2: fetch the quote page for SYMBOL:EXCHANGE and pull the |
| 126 |
# price, currency and trade time from the embedded data. |
| 127 |
# |
| 128 |
# The page no longer exposes data-last-price style attributes and |
| 129 |
# its CSS class names are obfuscated, but it still embeds a JSON |
| 130 |
# blob that includes, in a fixed order: |
| 131 |
# |
| 132 |
# ["AAPL","NASDAQ"],"Apple Inc",0,"USD",[291.13,...],...,[1781310600] |
| 133 |
# \_ symbol/exchange \_ name \_ currency \_ price \_ epoch |
| 134 |
# --------------------------------------------------------------- |
| 135 |
$url = $GOOGLE_URL . $ucstock . ':' . $exchange; |
| 136 |
$reply = $ua->request( GET $url ); |
| 137 |
|
| 138 |
if ( $reply->code != 200 ) { |
| 139 |
$info{ $stock, "success" } = 0; |
| 140 |
$info{ $stock, "errormsg" } = |
| 141 |
"Error retrieving quote for $stock from $url"; |
| 142 |
next; |
| 143 |
} |
| 144 |
|
| 145 |
$body = decode('UTF-8', $reply->content); |
| 146 |
|
| 147 |
### Quote Body: $body |
| 148 |
|
| 149 |
if ( $body =~ /\["\Q$ucstock\E","\Q$exchange\E"\],"(?:[^"\\]|\\.)*",\d+,"([A-Z]{2,4})",\[(-?[\d.]+),.{0,300}?,\[(\d{6,})\]/s ) { |
| 150 |
( $currency, $last, $time ) = ( $1, $2, $3 ); |
| 151 |
} else { |
| 152 |
$info{ $stock, "success" } = 0; |
| 153 |
$info{ $stock, "errormsg" } = "Cannot find price data in $url"; |
| 154 |
next; |
| 155 |
} |
| 156 |
|
| 157 |
my ( undef, undef, undef, $mday, $mon, $year, undef, undef, undef ) = |
| 158 |
localtime($time); |
| 159 |
$date = sprintf("%d/%02d/%02d", $year + 1900, $mon + 1, $mday); |
| 160 |
|
| 161 |
$info{ $stock, 'method' } = 'googleweb'; |
| 162 |
$info{ $stock, 'name' } = $name if defined $name; |
| 163 |
$info{ $stock, 'last' } = $last; |
| 164 |
$info{ $stock, 'currency' } = $currency; |
| 165 |
$info{ $stock, 'exchange' } = $exchange; |
| 166 |
$quoter->store_date(\%info, $stock, { isodate => $date }); |
| 167 |
$info{ $stock, 'success' } = 1; |
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
return wantarray() ? %info : \%info; |
| 172 |
|
| 173 |
} |
| 174 |
|
| 175 |
1; |
| 176 |
|
| 177 |
__END__ |
| 178 |
|
| 179 |
=head1 NAME |
| 180 |
|
| 181 |
Finance::Quote::GoogleWeb - Obtain quotes from Google Finance Web Pages |
| 182 |
|
| 183 |
=head1 SYNOPSIS |
| 184 |
|
| 185 |
use Finance::Quote; |
| 186 |
|
| 187 |
$q = Finance::Quote->new; |
| 188 |
|
| 189 |
%info = $q->fetch("googleweb", "aapl"); # Only query googleweb |
| 190 |
|
| 191 |
%info = $q->fetch("nyse", "ge"); # Failover to other sources OK. |
| 192 |
|
| 193 |
=head1 DESCRIPTION |
| 194 |
|
| 195 |
This module fetches information from L<https://www.google.com/finance/>. |
| 196 |
|
| 197 |
This module is loaded by default on a Finance::Quote object. It's also possible |
| 198 |
to load it explicitly by placing "googleweb" in the argument list to |
| 199 |
Finance::Quote->new(). |
| 200 |
|
| 201 |
This module provides "googleweb", "bats", "nyse", and "nasdaq" |
| 202 |
fetch methods. |
| 203 |
|
| 204 |
=head1 LABELS RETURNED |
| 205 |
|
| 206 |
The following labels are returned: |
| 207 |
|
| 208 |
=over |
| 209 |
|
| 210 |
=item name |
| 211 |
|
| 212 |
=item symbol |
| 213 |
|
| 214 |
=item last |
| 215 |
|
| 216 |
=item date |
| 217 |
|
| 218 |
=item currency |
| 219 |
|
| 220 |
=item method |
| 221 |
|
| 222 |
=back |
| 223 |
|
| 224 |
=head1 AVAILABLE EXCHANGES |
| 225 |
|
| 226 |
While the Google Finance web pages contain price information from other |
| 227 |
stock exchanges, this module currently retrieves last trade prices for |
| 228 |
securities listed on the NYSE, American, BATS, and NASDAQ stock exchanges. |
| 229 |
U.S. Mutual Funds quotes can also be retrieved with this module. |