-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoogle.pm
208 lines (177 loc) · 6.43 KB
/
Google.pm
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
207
208
#
# Copyright 2014 Rennie deGraaf <[email protected]>
#
# Finance::Quote module to retrieve stock and fund quotes from Google Finance.
# WARNING: The Google Finance API is deprecated and may be discontinued at any
# time.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# To get a fund quote:
# $ curl 'https://www.google.com/finance/info?q=TDB972,GOOG' -H 'Host: www.google.com' -H 'Accept: application/json'
# TDB972: TD Dividend Growth
# TDB162: TD Canadian Bond. Google uses TDB030
# TDB622: TD Monthly Income. Google uses CTI16
package Finance::Quote::Google;
require 5.005;
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request::Common;
use URI::Escape;
use JSON qw(decode_json);
use vars qw($VERSION $QUOTE_URI);
$VERSION = '1.20';
$QUOTE_URI='https://www.google.com/finance/info?q=';
sub methods { return (google => \&google); }
sub labels { return (google => [qw/exchange symbol last currency date time timezone/]); }
sub get_currency_by_exchange
{
my $exchange = shift;
# WARNING: These lists are extremely incomplete!
foreach (qw(MUTF NASDAQ NYSE))
{
return 'USD' if ($_ eq $exchange);
}
foreach (qw(MUTF_CA))
{
return 'CAD' if ($_ eq $exchange);
}
foreach (qw(LON))
{
return 'GBP' if ($_ eq $exchange);
}
}
sub google
{
my $quoter = shift;
my @symbols = @_;
return unless @symbols;
my %info;
my $ua = $quoter->user_agent;
# Request quotes for the given symbols.
# This is a really terrible API:
# * It sometimes translates one symbol to another, but since callers can
# request more than one symbol at once, they have no way to know which
# symbol in the request maps to which symbol in the response.
# * It doesn't unambiguously indicate the currency. The 'l_cur' field
# contains a currency indicator, but it uses '$' for both CAD and USD.
# * It provides conflicting information about the time zone. The
# timestamp in 'lt_dts' has a 'Z' suffix indicating UTC, but the
# correct time zone is actually the one in 'ltt' and 'lt'.
# It's disappointing that Google deprecated it instead of fixing it.
my $response = $ua->get($QUOTE_URI. uri_escape(join(',', @symbols)));
if ($response->is_success)
{
# Strip off the leading '// ' so that we have valid JSON
my $json = ($response->content =~ s/^[^[{]*//r);
# Decode the JSON
my @quotes;
eval {@quotes = @{decode_json($json)}};
if (!$@)
{
# Convert the array of quotes to a map.
my %quote_map;
foreach (@quotes)
{
if ($_->{'t'})
{
$quote_map{$_->{'t'}} = $_;
}
}
foreach (@symbols)
{
# Make sure that we have a quote with the required fields
unless (defined $quote_map{$_})
{
$info{$_, 'errormsg'} = 'No response received';
$info{$_, 'success'} = 0;
next;
}
my $quote = $quote_map{$_};
unless ($quote->{'l'} && '' ne $quote->{'l'} && $quote->{'lt_dts'} && $quote->{'e'})
{
$info{$_, 'errormsg'} = 'Missing or invalid fields in response';
$info{$_, 'success'} = 0;
next;
}
# Parse fields before we set anything in the response
# lt_dts is a timestamp of the form YYYY-mm-DDTHH:MM:SSZ
(my $date, my $time) = ($quote->{'lt_dts'} =~ /^(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}):\d{2}Z$/);
# l_cur may contain a currency symbol, but it's ambiguous.
# So we guess the currency based on the exchange.
my $currency = get_currency_by_exchange($quote->{'e'});
unless ($date && $time && $currency)
{
$info{$_, 'errormsg'} = 'Missing or invalid fields in response';
$info{$_, 'success'} = 0;
next;
}
$info{$_, 'last'} = $quote->{'l'};
$quoter->store_date(\%info, $_, {isodate => $date});
$info{$_, 'time'} = $time;
if ($quote->{'ltt'})
{
# ltt is a timestamp of the form H:MMPP ZZ
($info{$_, 'timezone'}) = ($quote->{'ltt'} =~ /^\d{1,2}:\d{2}[AP]M ([A-Z]+)$/);
}
$info{$_, 'currency'} = $currency;
$info{$_, 'symbol'} = $_;
$info{$_, 'exchange'} = $quote->{'e'};
$info{$_, 'method'} = 'Google';
$info{$_, 'source'} = 'Finance::Quote::Google';
$info{$_, 'success'} = 1;
}
}
else
{
foreach (@symbols)
{
$info{$_, 'errormsg'} = 'Error parsing response';
$info{$_, 'success'} = 0;
}
}
}
else
{
foreach (@symbols)
{
$info{$_, 'errormsg'} = 'Error retrieving quotes';
$info{$_, 'success'} = 0;
}
}
return wantarray() ? %info : \%info;
}
__END__
=head1 NAME
Finance::Quote::Google - Obtain quotes from Google Finance
=head1 SYNOPSIS
use Finance::Quote;
my $q = Finance::Quote->new;
my %quote = $q->fetch('google', 'GOOG');
=head1 DESCRIPTION
Finance::Quote module to retrieve stock quotes from Google Finance.
=head1 LABELS RETURNED
Information available from TD may include the following labels:
symbol
exchange
date
time
timezone
last
currency
=head1 SEE ALSO
Finance::Quote
Google Finance: https://www.google.com/finance
http://m.blog.csdn.net/blog/solaris_navi/6730464
=cut