| 1 |
#!/usr/bin/perl -w |
| 2 |
# vi: set ts=4 sw=4 noai ic showmode showmatch: |
| 3 |
# This module is based on the Finance::Quote::BSERO module |
| 4 |
# It was first called BOMSE but has been renamed to yahooJSON |
| 5 |
# since it gets a lot of quotes besides Indian |
| 6 |
# |
| 7 |
# The code has been modified by Abhijit K to |
| 8 |
# retrieve stock information from Yahoo Finance through json calls |
| 9 |
# |
| 10 |
# This program is free software; you can redistribute it and/or modify |
| 11 |
# it under the terms of the GNU General Public License as published by |
| 12 |
# the Free Software Foundation; either version 2 of the License, or |
| 13 |
# (at your option) any later version. |
| 14 |
# |
| 15 |
# This program is distributed in the hope that it will be useful, |
| 16 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
# GNU General Public License for more details. |
| 19 |
# |
| 20 |
# You should have received a copy of the GNU General Public License |
| 21 |
# along with this program; if not, write to the Free Software |
| 22 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 23 |
# 02110-1301, USA |
| 24 |
|
| 25 |
package Finance::Quote::YahooJSON; |
| 26 |
|
| 27 |
use strict; |
| 28 |
|
| 29 |
use JSON qw( decode_json ); |
| 30 |
use vars qw($VERSION $YIND_URL_HEAD $YIND_URL_TAIL); |
| 31 |
use HTTP::Request::Common; |
| 32 |
use Time::Piece; |
| 33 |
use HTTP::Cookies; |
| 34 |
use URI::Escape; |
| 35 |
|
| 36 |
use constant DEBUG => $ENV{DEBUG}; |
| 37 |
use if DEBUG, 'Smart::Comments'; |
| 38 |
|
| 39 |
our $VERSION = '1.71'; # VERSION |
| 40 |
|
| 41 |
# Required to successfully read extra long headers returned from yahoo |
| 42 |
my %OPTS = @LWP::Protocol::http::EXTRA_SOCK_OPTS; |
| 43 |
$OPTS{MaxLineLength} = 16384; |
| 44 |
@LWP::Protocol::http::EXTRA_SOCK_OPTS = %OPTS; |
| 45 |
|
| 46 |
my $YIND_URL_HEAD = 'https://query2.finance.yahoo.com/v11/finance/quoteSummary/?symbols='; |
| 47 |
my $YIND_URL_TAIL = '&modules=price,summaryDetail,defaultKeyStatistics'; |
| 48 |
#my $browser = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36'; |
| 49 |
my $browser = 'Mozilla/5.0'; |
| 50 |
|
| 51 |
our $DISPLAY = 'YahooJSON'; |
| 52 |
our @LABELS = qw/name last date isodate volume currency method exchange type |
| 53 |
div_yield eps pe year_range open high low close market_cap volume_10_days |
| 54 |
trailing_pe book_value shares_outstanding shares_short shares_float/; |
| 55 |
our $METHODHASH = {subroutine => \&yahoo_json, |
| 56 |
display => $DISPLAY, |
| 57 |
labels => \@LABELS}; |
| 58 |
|
| 59 |
sub methodinfo { |
| 60 |
return ( |
| 61 |
yahoo_json => $METHODHASH, |
| 62 |
yahoojson => $METHODHASH, |
| 63 |
nyse => $METHODHASH, |
| 64 |
nasdaq => $METHODHASH, |
| 65 |
usa => $METHODHASH, |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
sub labels { |
| 70 |
my %m = methodinfo(); |
| 71 |
return map {$_ => [@{$m{$_}{labels}}] } keys %m; |
| 72 |
} |
| 73 |
|
| 74 |
sub methods { |
| 75 |
my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m; |
| 76 |
} |
| 77 |
|
| 78 |
sub yahoo_json { |
| 79 |
|
| 80 |
my $quoter = shift; |
| 81 |
my @stocks = @_; |
| 82 |
my ( %info, $reply, $url); |
| 83 |
my ( $my_date, $amp_stocks, $symbol ); |
| 84 |
my $ua = $quoter->user_agent(); |
| 85 |
|
| 86 |
my $cookie_jar = HTTP::Cookies->new; |
| 87 |
|
| 88 |
# Redirect handler deals with cookie consent workflow applicable to EU countries |
| 89 |
# credit to John Weber from Germany for injecting redirect handler |
| 90 |
my $gcrumb = ""; |
| 91 |
$ua->add_handler("response_redirect", sub { |
| 92 |
my($response, $ua, $h) = @_; |
| 93 |
|
| 94 |
# Check where we've been redirected and act accordingly |
| 95 |
my $redirect_uri = URI->new($response->header("Location")); |
| 96 |
if ($redirect_uri->path eq "/consent") { |
| 97 |
|
| 98 |
# Remember gcrumb value for collectConsent request later |
| 99 |
my %params = $redirect_uri->query_form; |
| 100 |
$gcrumb = $params{'gcrumb'}; |
| 101 |
|
| 102 |
} elsif ($redirect_uri->path eq "/v2/collectConsent") { |
| 103 |
|
| 104 |
my %params = $redirect_uri->query_form; |
| 105 |
my $sessionId = $params{'sessionId'}; |
| 106 |
|
| 107 |
# Turn this request into a POST with form data to confoo accept cookies |
| 108 |
my $request = POST($redirect_uri, [ |
| 109 |
'csrfToken' => $gcrumb, |
| 110 |
'sessionId' => $sessionId, |
| 111 |
'originalDoneUrl' => 'https://www.yahoo.com/?guccounter=1', |
| 112 |
'namespace' => 'yahoo', |
| 113 |
# For the EU consent, either can : |
| 114 |
# 'agree' => 'agree' |
| 115 |
# to it or |
| 116 |
'reject' => 'reject' |
| 117 |
]); |
| 118 |
return $request; |
| 119 |
} |
| 120 |
return; |
| 121 |
}); |
| 122 |
|
| 123 |
$ua->cookie_jar($cookie_jar); |
| 124 |
$ua->agent($browser); |
| 125 |
|
| 126 |
# Tell user agent to redirect POSTs in additional to GET AND HEAD |
| 127 |
$ua->requests_redirectable(['GET', 'HEAD', 'POST']); |
| 128 |
|
| 129 |
# get necessary cookies |
| 130 |
$reply = $ua->get('https://www.yahoo.com/', "Accept" => "text/html"); |
| 131 |
if ($reply->code != 200) { |
| 132 |
foreach my $symbol (@stocks) { |
| 133 |
$info{$symbol, "success"} = 0; |
| 134 |
$info{$symbol, "errormsg"} = "Error accessing www.yahoo.com: $@"; |
| 135 |
} |
| 136 |
return wantarray() ? %info : \%info; |
| 137 |
} |
| 138 |
|
| 139 |
# get the crumb that corrosponds to cookies retrieved |
| 140 |
$reply = $ua->request(GET 'https://query2.finance.yahoo.com/v1/test/getcrumb'); |
| 141 |
if ($reply->code != 200) { |
| 142 |
foreach my $symbol (@stocks) { |
| 143 |
$info{$symbol, "success"} = 0; |
| 144 |
$info{$symbol, "errormsg"} = "Error accessing query2.finance.yahoo.com/v1/test/getcrumb: $@"; |
| 145 |
} |
| 146 |
return wantarray() ? %info : \%info; |
| 147 |
} |
| 148 |
my $crumb = uri_escape($reply->content); |
| 149 |
|
| 150 |
### [<now>] cookie_jar : $cookie_jar |
| 151 |
### [<now>] crumb : $crumb |
| 152 |
|
| 153 |
foreach my $stocks (@stocks) { |
| 154 |
|
| 155 |
# Issue 202 - Fix symbols with Ampersand |
| 156 |
# Can also be written as |
| 157 |
# $amp_stocks = $stocks =~ s/&/%26/gr; |
| 158 |
($amp_stocks = $stocks) =~ s/&/%26/g; |
| 159 |
|
| 160 |
$url = $YIND_URL_HEAD . $amp_stocks . '&crumb=' . $crumb . $YIND_URL_TAIL; |
| 161 |
$reply = $ua->request(GET $url); |
| 162 |
|
| 163 |
### [<now>] url : $url |
| 164 |
### [<now>] reply : $reply |
| 165 |
|
| 166 |
my $code = $reply->code; |
| 167 |
my $desc = HTTP::Status::status_message($code); |
| 168 |
my $headers = $reply->headers_as_string; |
| 169 |
my $body = $reply->content; |
| 170 |
|
| 171 |
#Response variables available: |
| 172 |
#Response code: $code |
| 173 |
#Response description: $desc |
| 174 |
#HTTP Headers: $headers |
| 175 |
#Response body $body |
| 176 |
|
| 177 |
$info{ $stocks, "symbol" } = $stocks; |
| 178 |
|
| 179 |
if ( $code == 200 ) { |
| 180 |
|
| 181 |
#HTTP_Response succeeded - parse the data |
| 182 |
my $json_data = JSON::decode_json $body; |
| 183 |
|
| 184 |
# Requests for invalid symbols sometimes return 200 with an empty |
| 185 |
# JSON result array |
| 186 |
my $json_data_count |
| 187 |
= scalar @{ $json_data->{'quoteSummary'}{'result'} }; |
| 188 |
|
| 189 |
if ( $json_data_count < 1 ) { |
| 190 |
$info{ $stocks, "success" } = 0; |
| 191 |
$info{ $stocks, "errormsg" } = |
| 192 |
"Error retrieving quote for $stocks - no listing for this name found. Please check symbol and the two letter extension (if any)"; |
| 193 |
|
| 194 |
} |
| 195 |
else { |
| 196 |
|
| 197 |
my $json_resources_price = $json_data->{'quoteSummary'}{'result'}[0]{'price'}; |
| 198 |
my $json_resources_summaryDetail = $json_data->{'quoteSummary'}{'result'}[0]{'summaryDetail'}; |
| 199 |
my $json_resources_defaultKeyStatistics = $json_data->{'quoteSummary'}{'result'}[0]{'defaultKeyStatistics'}; |
| 200 |
|
| 201 |
# TODO: Check if $json_response_type is "Quote" |
| 202 |
# before attempting anything else |
| 203 |
my $json_symbol = $json_resources_price->{'symbol'}; |
| 204 |
# || $json_resources->{'resource'}{'fields'}{'symbol'}; |
| 205 |
my $json_volume = $json_resources_price->{'regularMarketVolume'}{'raw'}; |
| 206 |
my $json_timestamp = |
| 207 |
$json_resources_price->{'regularMarketTime'}; |
| 208 |
my $json_name = $json_resources_price->{'shortName'}; |
| 209 |
my $json_type = $json_resources_price->{'quoteType'}; |
| 210 |
my $json_price = |
| 211 |
$json_resources_price->{'regularMarketPrice'}{'raw'}; |
| 212 |
|
| 213 |
$info{ $stocks, "success" } = 1; |
| 214 |
$info{ $stocks, "exchange" } = |
| 215 |
$json_resources_price->{'exchangeName'}; |
| 216 |
$info{ $stocks, "method" } = "yahoo_json"; |
| 217 |
$info{ $stocks, "name" } = $stocks . ' (' . $json_name . ')'; |
| 218 |
$info{ $stocks, "type" } = $json_type; |
| 219 |
$info{ $stocks, "last" } = $json_price; |
| 220 |
$info{ $stocks, "currency"} = $json_resources_price->{'currency'}; |
| 221 |
$info{ $stocks, "volume" } = $json_volume; |
| 222 |
|
| 223 |
# Add extra fields using names as per yahoo to make it easier |
| 224 |
# to switch from yahoo to yahooJSON |
| 225 |
# Code added by goodvibes |
| 226 |
{ |
| 227 |
# turn off warnings in this block to fix bogus |
| 228 |
# 'Use of uninitialized value in multiplication' warning |
| 229 |
# in Strawberry perl 5.18.2 in Windows |
| 230 |
local $^W = 0; |
| 231 |
$info{ $stocks, "div_yield" } = |
| 232 |
$json_resources_summaryDetail->{'trailingAnnualDividendYield'}{'raw'} =~ m/^[\d,\.]+$/ ? |
| 233 |
$json_resources_summaryDetail->{'trailingAnnualDividendYield'}{'raw'} * 100 : |
| 234 |
0.0; |
| 235 |
} |
| 236 |
$info{ $stocks, "market_cap" } = |
| 237 |
$json_resources_price->{'marketCap'}{'raw'}; |
| 238 |
$info{ $stocks, "volume_10_days" } = |
| 239 |
$json_resources_price->{'averageDailyVolume10Day'}{'raw'}; |
| 240 |
$info{ $stocks, "trailing_pe" } = |
| 241 |
$json_resources_summaryDetail->{'trailingPE'}{'raw'}; |
| 242 |
$info{ $stocks, "book_value" } = |
| 243 |
$json_resources_defaultKeyStatistics->{'bookValue'}{'raw'}; |
| 244 |
$info{ $stocks, "shares_outstanding" } = |
| 245 |
$json_resources_defaultKeyStatistics->{'sharesOutstanding'}{'raw'}; |
| 246 |
$info{ $stocks, "shares_short" } = |
| 247 |
$json_resources_defaultKeyStatistics->{'sharesShort'}{'raw'}; |
| 248 |
$info{ $stocks, "shares_float" } = |
| 249 |
$json_resources_defaultKeyStatistics->{'floatShares'}{'raw'}; |
| 250 |
$info{ $stocks, "eps"} = |
| 251 |
$json_resources_defaultKeyStatistics->{'trailingEps'}{'raw'}; |
| 252 |
# $json_resources_summaryDetail->{'epsTrailingTwelveMonths'}; |
| 253 |
$info{ $stocks, "pe"} = $json_resources_summaryDetail->{'trailingPE'}{'raw'}; |
| 254 |
$info{ $stocks, "year_range"} = |
| 255 |
sprintf("%12s - %s", |
| 256 |
$json_resources_summaryDetail->{"fiftyTwoWeekLow"}{'raw'}, |
| 257 |
$json_resources_summaryDetail->{'fiftyTwoWeekHigh'}{'raw'}); |
| 258 |
$info{ $stocks, "open"} = |
| 259 |
$json_resources_price->{'regularMarketOpen'}{'raw'}; |
| 260 |
$info{ $stocks, "high"} = |
| 261 |
$json_resources_price->{'regularMarketDayHigh'}{'raw'}; |
| 262 |
$info{ $stocks, "low"} = |
| 263 |
$json_resources_price->{'regularMarketDayLow'}{'raw'}; |
| 264 |
$info{ $stocks, "close"} = |
| 265 |
$json_resources_summaryDetail->{'regularMarketPreviousClose'}{'raw'}; |
| 266 |
|
| 267 |
# The Yahoo JSON interface can London prices in |
| 268 |
# GBp (pence) instead of GBP (pounds) and the Yahoo Base |
| 269 |
# had a hack to convert them to GBP. In theory all the |
| 270 |
# callers would correctly handle GBp as not the same as GBP, |
| 271 |
# but they don't, and since we had the hack before, |
| 272 |
# let's add it back now. |
| 273 |
# Convert GBp or GBX to GBP (divide price by 100). |
| 274 |
|
| 275 |
if ( ($info{$stocks,"currency"} eq "GBp") || |
| 276 |
($info{$stocks,"currency"} eq "GBX")) { |
| 277 |
foreach my $field ($quoter->get_default_currency_fields) { |
| 278 |
next unless ( $info{ $stocks, $field } ); |
| 279 |
$info{$stocks, $field} = |
| 280 |
$quoter->scale_field($info{ $stocks, $field }, 0.01); |
| 281 |
} |
| 282 |
$info{ $stocks, "currency"} = "GBP"; |
| 283 |
} |
| 284 |
|
| 285 |
# Apply the same hack for Johannesburg Stock Exchange |
| 286 |
# (JSE) prices as they are returned in ZAc (cents) |
| 287 |
# instead of ZAR (rands). JSE symbols are suffixed |
| 288 |
# with ".JO" when querying Yahoo e.g. ANG.JO |
| 289 |
if ( $info{$stocks,"currency"} eq "ZAc") { |
| 290 |
foreach my $field ($quoter->get_default_currency_fields) { |
| 291 |
next unless ( $info{ $stocks, $field } ); |
| 292 |
$info{$stocks, $field} = |
| 293 |
$quoter->scale_field($info{ $stocks, $field }, 0.01); |
| 294 |
} |
| 295 |
$info{ $stocks, "currency"} = "ZAR"; |
| 296 |
} |
| 297 |
|
| 298 |
# Apply the same hack for Tel Aviv Stock Exchange |
| 299 |
# (TASE) prices as they are returned in ILA (Agorot) |
| 300 |
# instead of ILS (Shekels). TASE symbols are suffixed |
| 301 |
# with ".TA" when querying Yahoo e.g. POLI.TA |
| 302 |
if ( $info{$stocks,"currency"} eq "ILA") { |
| 303 |
foreach my $field ($quoter->get_default_currency_fields) { |
| 304 |
next unless ( $info{ $stocks, $field } ); |
| 305 |
$info{$stocks, $field} = |
| 306 |
$quoter->scale_field($info{ $stocks, $field }, 0.01); |
| 307 |
} |
| 308 |
$info{ $stocks, "currency"} = "ILS"; |
| 309 |
} |
| 310 |
|
| 311 |
# MS Windows strftime() does not support %T so use %H:%M:%S |
| 312 |
# instead. |
| 313 |
$my_date = |
| 314 |
localtime($json_timestamp)->strftime('%d.%m.%Y %H:%M:%S'); |
| 315 |
|
| 316 |
$quoter->store_date( \%info, $stocks, |
| 317 |
{ eurodate => $my_date } ); |
| 318 |
|
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
#HTTP request fail |
| 323 |
else { |
| 324 |
$info{ $stocks, "success" } = 0; |
| 325 |
$info{ $stocks, "errormsg" } = |
| 326 |
"Error retrieving quote for $stocks. Attempt to fetch the URL $url resulted in HTTP response $code ($desc)"; |
| 327 |
} |
| 328 |
|
| 329 |
} |
| 330 |
|
| 331 |
return wantarray() ? %info : \%info; |
| 332 |
return \%info; |
| 333 |
} |
| 334 |
|
| 335 |
1; |
| 336 |
|
| 337 |
=head1 NAME |
| 338 |
|
| 339 |
Finance::Quote::YahooJSON - Obtain quotes from Yahoo Finance through JSON call |
| 340 |
|
| 341 |
=head1 SYNOPSIS |
| 342 |
|
| 343 |
use Finance::Quote; |
| 344 |
|
| 345 |
$q = Finance::Quote->new; |
| 346 |
|
| 347 |
%info = $q->fetch('yahoo_json','SBIN.NS'); |
| 348 |
|
| 349 |
=head1 DESCRIPTION |
| 350 |
|
| 351 |
This module fetches information from Yahoo as JSON |
| 352 |
|
| 353 |
This module is loaded by default on a Finance::Quote object. It's |
| 354 |
also possible to load it explicitly by placing "YahooJSON" in the argument |
| 355 |
list to Finance::Quote->new(). |
| 356 |
|
| 357 |
This module provides the "yahoo_json" fetch method. |
| 358 |
|
| 359 |
=head1 LABELS RETURNED |
| 360 |
|
| 361 |
The following labels may be returned by Finance::Quote::YahooJSON : |
| 362 |
name, last, isodate, volume, currency, method, exchange, type, |
| 363 |
div_yield eps pe year_range open high low close. |
| 364 |
|
| 365 |
=head1 SEE ALSO |
| 366 |
|
| 367 |
=cut |