| 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 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 5 | ▶ | use JSON qw( decode_json ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 5 | ▶ | use vars qw($VERSION $YIND_URL_HEAD $YIND_URL_TAIL); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 5 | ▶ | use HTTP::Request::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 5 | ▶ | use Time::Piece; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 5 | ▶ | use HTTP::Cookies; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 5 | ▶ | use URI::Escape; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 5 | ▶ | use constant DEBUG => $ENV{DEBUG}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 5 | ▶ | use if DEBUG, 'Smart::Comments'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 5 | our $VERSION = '1.72_01'; # TRIAL VERSION | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # Required to successfully read extra long headers returned from yahoo | ||||||||||||||||||||||||||||
| 42 | 5 | my %OPTS = @LWP::Protocol::http::EXTRA_SOCK_OPTS; | |||||||||||||||||||||||||||
| 43 | 5 | $OPTS{MaxLineLength} = 16384; | |||||||||||||||||||||||||||
| 44 | 5 | @LWP::Protocol::http::EXTRA_SOCK_OPTS = %OPTS; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 5 | my $YIND_URL_HEAD = 'https://query2.finance.yahoo.com/v11/finance/quoteSummary/?symbols='; | |||||||||||||||||||||||||||
| 47 | 5 | 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 | 5 | my $browser = 'Mozilla/5.0'; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 5 | our $DISPLAY = 'YahooJSON'; | |||||||||||||||||||||||||||
| 52 | 5 | 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 | 5 | our $METHODHASH = {subroutine => \&yahoo_json, | |||||||||||||||||||||||||||
| 56 | display => $DISPLAY, | ||||||||||||||||||||||||||||
| 57 | labels => \@LABELS}; | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 10 | ▶ | sub methodinfo { | ||||||||||||||||||||||||||
|
Subroutine
methodinfo: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 60 | return ( | ||||||||||||||||||||||||||||
| 61 | 10 | yahoo_json => $METHODHASH, | |||||||||||||||||||||||||||
| 62 | yahoojson => $METHODHASH, | ||||||||||||||||||||||||||||
| 63 | nyse => $METHODHASH, | ||||||||||||||||||||||||||||
| 64 | nasdaq => $METHODHASH, | ||||||||||||||||||||||||||||
| 65 | usa => $METHODHASH, | ||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 5 | ▶ | sub labels { | ||||||||||||||||||||||||||
|
Subroutine
labels: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 70 | 5 | my %m = methodinfo(); | |||||||||||||||||||||||||||
| 71 | 5 | return map {$_ => [@{$m{$_}{labels}}] } keys %m; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 5 | ▶ | sub methods { | ||||||||||||||||||||||||||
|
Subroutine
methods: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 75 | 5 | my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | sub yahoo_json { | ||||||||||||||||||||||||||
|
Subroutine
yahoo_json: not called CC=22 SCAR=62.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | my $quoter = shift; | |||||||||||||||||||||||||||
| 81 | 0 | my @stocks = @_; | |||||||||||||||||||||||||||
| 82 | 0 | my ( %info, $reply, $url); | |||||||||||||||||||||||||||
| 83 | 0 | my ( $my_date, $amp_stocks, $symbol ); | |||||||||||||||||||||||||||
| 84 | 0 | my $ua = $quoter->user_agent(); | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | 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 | 0 | my $gcrumb = ""; | |||||||||||||||||||||||||||
| 91 | 0 | ▶ | $ua->add_handler("response_redirect", sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 92 | 0 | my($response, $ua, $h) = @_; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # Check where we've been redirected and act accordingly | ||||||||||||||||||||||||||||
| 95 | 0 | my $redirect_uri = URI->new($response->header("Location")); | |||||||||||||||||||||||||||
| 96 | 0 | ▶ | if ($redirect_uri->path eq "/consent") { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # Remember gcrumb value for collectConsent request later | ||||||||||||||||||||||||||||
| 99 | 0 | my %params = $redirect_uri->query_form; | |||||||||||||||||||||||||||
| 100 | 0 | $gcrumb = $params{'gcrumb'}; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | } elsif ($redirect_uri->path eq "/v2/collectConsent") { | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | my %params = $redirect_uri->query_form; | |||||||||||||||||||||||||||
| 105 | 0 | my $sessionId = $params{'sessionId'}; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # Turn this request into a POST with form data to confoo accept cookies | ||||||||||||||||||||||||||||
| 108 | 0 | 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 | 0 | return $request; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | 0 | return; | |||||||||||||||||||||||||||
| 121 | 0 | }); | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | $ua->cookie_jar($cookie_jar); | |||||||||||||||||||||||||||
| 124 | 0 | $ua->agent($browser); | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # Tell user agent to redirect POSTs in additional to GET AND HEAD | ||||||||||||||||||||||||||||
| 127 | 0 | $ua->requests_redirectable(['GET', 'HEAD', 'POST']); | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | # get necessary cookies | ||||||||||||||||||||||||||||
| 130 | 0 | $reply = $ua->get('https://www.yahoo.com/', "Accept" => "text/html"); | |||||||||||||||||||||||||||
| 131 | 0 | ▶ | if ($reply->code != 200) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 0 | foreach my $symbol (@stocks) { | |||||||||||||||||||||||||||
| 133 | 0 | $info{$symbol, "success"} = 0; | |||||||||||||||||||||||||||
| 134 | 0 | $info{$symbol, "errormsg"} = "Error accessing www.yahoo.com: $@"; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 0 | ▶ | return wantarray() ? %info : \%info; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # get the crumb that corrosponds to cookies retrieved | ||||||||||||||||||||||||||||
| 140 | 0 | $reply = $ua->request(GET 'https://query2.finance.yahoo.com/v1/test/getcrumb'); | |||||||||||||||||||||||||||
| 141 | 0 | ▶ | if ($reply->code != 200) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 0 | foreach my $symbol (@stocks) { | |||||||||||||||||||||||||||
| 143 | 0 | $info{$symbol, "success"} = 0; | |||||||||||||||||||||||||||
| 144 | 0 | $info{$symbol, "errormsg"} = "Error accessing query2.finance.yahoo.com/v1/test/getcrumb: $@"; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | 0 | ▶ | return wantarray() ? %info : \%info; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 0 | my $crumb = uri_escape($reply->content); | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | ### [<now>] cookie_jar : $cookie_jar | ||||||||||||||||||||||||||||
| 151 | ### [<now>] crumb : $crumb | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | 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 | 0 | ($amp_stocks = $stocks) =~ s/&/%26/g; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 0 | $url = $YIND_URL_HEAD . $amp_stocks . '&crumb=' . $crumb . $YIND_URL_TAIL; | |||||||||||||||||||||||||||
| 161 | 0 | $reply = $ua->request(GET $url); | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | ### [<now>] url : $url | ||||||||||||||||||||||||||||
| 164 | ### [<now>] reply : $reply | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 0 | my $code = $reply->code; | |||||||||||||||||||||||||||
| 167 | 0 | my $desc = HTTP::Status::status_message($code); | |||||||||||||||||||||||||||
| 168 | 0 | my $headers = $reply->headers_as_string; | |||||||||||||||||||||||||||
| 169 | 0 | 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 | 0 | $info{ $stocks, "symbol" } = $stocks; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 0 | ▶ | if ( $code == 200 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | #HTTP_Response succeeded - parse the data | ||||||||||||||||||||||||||||
| 182 | 0 | 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 | 0 | = scalar @{ $json_data->{'quoteSummary'}{'result'} }; | |||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 0 | ▶ | if ( $json_data_count < 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | 0 | $info{ $stocks, "success" } = 0; | |||||||||||||||||||||||||||
| 191 | 0 | $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 | 0 | my $json_resources_price = $json_data->{'quoteSummary'}{'result'}[0]{'price'}; | |||||||||||||||||||||||||||
| 198 | 0 | my $json_resources_summaryDetail = $json_data->{'quoteSummary'}{'result'}[0]{'summaryDetail'}; | |||||||||||||||||||||||||||
| 199 | 0 | 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 | 0 | my $json_symbol = $json_resources_price->{'symbol'}; | |||||||||||||||||||||||||||
| 204 | # || $json_resources->{'resource'}{'fields'}{'symbol'}; | ||||||||||||||||||||||||||||
| 205 | 0 | my $json_volume = $json_resources_price->{'regularMarketVolume'}{'raw'}; | |||||||||||||||||||||||||||
| 206 | my $json_timestamp = | ||||||||||||||||||||||||||||
| 207 | 0 | $json_resources_price->{'regularMarketTime'}; | |||||||||||||||||||||||||||
| 208 | 0 | my $json_name = $json_resources_price->{'shortName'}; | |||||||||||||||||||||||||||
| 209 | 0 | my $json_type = $json_resources_price->{'quoteType'}; | |||||||||||||||||||||||||||
| 210 | my $json_price = | ||||||||||||||||||||||||||||
| 211 | 0 | $json_resources_price->{'regularMarketPrice'}{'raw'}; | |||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 0 | $info{ $stocks, "success" } = 1; | |||||||||||||||||||||||||||
| 214 | $info{ $stocks, "exchange" } = | ||||||||||||||||||||||||||||
| 215 | 0 | $json_resources_price->{'exchangeName'}; | |||||||||||||||||||||||||||
| 216 | 0 | $info{ $stocks, "method" } = "yahoo_json"; | |||||||||||||||||||||||||||
| 217 | 0 | $info{ $stocks, "name" } = $stocks . ' (' . $json_name . ')'; | |||||||||||||||||||||||||||
| 218 | 0 | $info{ $stocks, "type" } = $json_type; | |||||||||||||||||||||||||||
| 219 | 0 | $info{ $stocks, "last" } = $json_price; | |||||||||||||||||||||||||||
| 220 | 0 | $info{ $stocks, "currency"} = $json_resources_price->{'currency'}; | |||||||||||||||||||||||||||
| 221 | 0 | $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 | 0 | local $^W = 0; | |||||||||||||||||||||||||||
| 231 | $info{ $stocks, "div_yield" } = | ||||||||||||||||||||||||||||
| 232 | $json_resources_summaryDetail->{'trailingAnnualDividendYield'}{'raw'} =~ m/^[\d,\.]+$/ ? | ||||||||||||||||||||||||||||
| 233 | 0 | ▶ | $json_resources_summaryDetail->{'trailingAnnualDividendYield'}{'raw'} * 100 : | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 234 | 0.0; | ||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | $info{ $stocks, "market_cap" } = | ||||||||||||||||||||||||||||
| 237 | 0 | $json_resources_price->{'marketCap'}{'raw'}; | |||||||||||||||||||||||||||
| 238 | $info{ $stocks, "volume_10_days" } = | ||||||||||||||||||||||||||||
| 239 | 0 | $json_resources_price->{'averageDailyVolume10Day'}{'raw'}; | |||||||||||||||||||||||||||
| 240 | $info{ $stocks, "trailing_pe" } = | ||||||||||||||||||||||||||||
| 241 | 0 | $json_resources_summaryDetail->{'trailingPE'}{'raw'}; | |||||||||||||||||||||||||||
| 242 | $info{ $stocks, "book_value" } = | ||||||||||||||||||||||||||||
| 243 | 0 | $json_resources_defaultKeyStatistics->{'bookValue'}{'raw'}; | |||||||||||||||||||||||||||
| 244 | $info{ $stocks, "shares_outstanding" } = | ||||||||||||||||||||||||||||
| 245 | 0 | $json_resources_defaultKeyStatistics->{'sharesOutstanding'}{'raw'}; | |||||||||||||||||||||||||||
| 246 | $info{ $stocks, "shares_short" } = | ||||||||||||||||||||||||||||
| 247 | 0 | $json_resources_defaultKeyStatistics->{'sharesShort'}{'raw'}; | |||||||||||||||||||||||||||
| 248 | $info{ $stocks, "shares_float" } = | ||||||||||||||||||||||||||||
| 249 | 0 | $json_resources_defaultKeyStatistics->{'floatShares'}{'raw'}; | |||||||||||||||||||||||||||
| 250 | $info{ $stocks, "eps"} = | ||||||||||||||||||||||||||||
| 251 | 0 | $json_resources_defaultKeyStatistics->{'trailingEps'}{'raw'}; | |||||||||||||||||||||||||||
| 252 | # $json_resources_summaryDetail->{'epsTrailingTwelveMonths'}; | ||||||||||||||||||||||||||||
| 253 | 0 | $info{ $stocks, "pe"} = $json_resources_summaryDetail->{'trailingPE'}{'raw'}; | |||||||||||||||||||||||||||
| 254 | $info{ $stocks, "year_range"} = | ||||||||||||||||||||||||||||
| 255 | sprintf("%12s - %s", | ||||||||||||||||||||||||||||
| 256 | $json_resources_summaryDetail->{"fiftyTwoWeekLow"}{'raw'}, | ||||||||||||||||||||||||||||
| 257 | 0 | $json_resources_summaryDetail->{'fiftyTwoWeekHigh'}{'raw'}); | |||||||||||||||||||||||||||
| 258 | $info{ $stocks, "open"} = | ||||||||||||||||||||||||||||
| 259 | 0 | $json_resources_price->{'regularMarketOpen'}{'raw'}; | |||||||||||||||||||||||||||
| 260 | $info{ $stocks, "high"} = | ||||||||||||||||||||||||||||
| 261 | 0 | $json_resources_price->{'regularMarketDayHigh'}{'raw'}; | |||||||||||||||||||||||||||
| 262 | $info{ $stocks, "low"} = | ||||||||||||||||||||||||||||
| 263 | 0 | $json_resources_price->{'regularMarketDayLow'}{'raw'}; | |||||||||||||||||||||||||||
| 264 | $info{ $stocks, "close"} = | ||||||||||||||||||||||||||||
| 265 | 0 | $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 | 0 | ▶ | if ( ($info{$stocks,"currency"} eq "GBp") || | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$info{join $;, $stocks, "currency"} eq "GBp" or $info{join $;, $stocks, "currency"} eq "GBX"
MC/DC0%0/2
$info{join $;, $stocks, "currency"} eq "GBp" or $info{join $;, $stocks, "currency"} eq "GBX"
$info{join $;, $stocks, "currency"} eq "GBp"
$info{join $;, $stocks, "currency"} eq "GBX"
Truth table0%0/3
A or B
A= $info{join $;, $stocks, "currency"} eq "GBp"B= $info{join $;, $stocks, "currency"} eq "GBX"
| |||||||||||||||||||||||||||||
| 276 | ($info{$stocks,"currency"} eq "GBX")) { | ||||||||||||||||||||||||||||
| 277 | 0 | foreach my $field ($quoter->get_default_currency_fields) { | |||||||||||||||||||||||||||
| 278 | 0 | ▶ | next unless ( $info{ $stocks, $field } ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 279 | $info{$stocks, $field} = | ||||||||||||||||||||||||||||
| 280 | 0 | $quoter->scale_field($info{ $stocks, $field }, 0.01); | |||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||
| 282 | 0 | $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 | 0 | ▶ | if ( $info{$stocks,"currency"} eq "ZAc") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 290 | 0 | foreach my $field ($quoter->get_default_currency_fields) { | |||||||||||||||||||||||||||
| 291 | 0 | ▶ | next unless ( $info{ $stocks, $field } ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | $info{$stocks, $field} = | ||||||||||||||||||||||||||||
| 293 | 0 | $quoter->scale_field($info{ $stocks, $field }, 0.01); | |||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||
| 295 | 0 | $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 | 0 | ▶ | if ( $info{$stocks,"currency"} eq "ILA") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 303 | 0 | foreach my $field ($quoter->get_default_currency_fields) { | |||||||||||||||||||||||||||
| 304 | 0 | ▶ | next unless ( $info{ $stocks, $field } ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 305 | $info{$stocks, $field} = | ||||||||||||||||||||||||||||
| 306 | 0 | $quoter->scale_field($info{ $stocks, $field }, 0.01); | |||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||
| 308 | 0 | $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 | 0 | localtime($json_timestamp)->strftime('%d.%m.%Y %H:%M:%S'); | |||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | 0 | $quoter->store_date( \%info, $stocks, | |||||||||||||||||||||||||||
| 317 | { eurodate => $my_date } ); | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | #HTTP request fail | ||||||||||||||||||||||||||||
| 323 | else { | ||||||||||||||||||||||||||||
| 324 | 0 | $info{ $stocks, "success" } = 0; | |||||||||||||||||||||||||||
| 325 | 0 | $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 | 0 | ▶ | return wantarray() ? %info : \%info; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 332 | 0 | return \%info; | |||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | 5 | 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 | ||||||||||||||||||||||||||||