Quote/YahooWeb.pm

filter: Statementstmt 34.3% 59 / 172 Branchbran 0.0% 0 / 48 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 89.4% 17 / 19 Podpod 0.0% 0 / 4 totaltotal 31.2% 76 / 243 CC 28 SCAR 59.9
CC
28
Cov
27%
yahooweb
702.0
__ANON__
12.0
BEGIN
1.0
CRAP 397.9
« Quote/CurrencyRates/AllRatesToday.pm ↑ Summary Quote/OnVista.pm »
1 # vi: set noai ic ts=4 sw=4 showmode showmatch:
2 # This program is free software: you can redistribute it and/or modify
3 # it under the terms of the GNU General Public License as published by
4 # the Free Software Foundation, either version 3 of the License, or
5 # (at your option) any later version.
6 #
7 # This program is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # GNU General Public License for more details.
11 #
12 # You should have received a copy of the GNU General Public License
13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
14  
15 package Finance::Quote::YahooWeb;
16  
17 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 5 ▶use Encode qw( encode_utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use HTML::TreeBuilder::XPath;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 ▶use JSON qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 5 ▶use LWP::Protocol::http;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶use Text::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 ▶use HTTP::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 5 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 5 ▶use constant DEBUG => $ENV{DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
30 5 ▶use if DEBUG, 'Smart::Comments';
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 5 ▶use constant URLTAG => "data-url";
Subroutine BEGIN: called CC=1 SCAR=0.0
33 5 ▶use constant JSONBODY => "body";
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 5 our $VERSION = '1.72_01'; # TRIAL VERSION
36  
37 # Fix for 500 Header line too long message
38 5 push(@LWP::Protocol::http::EXTRA_SOCK_OPTS, MaxLineLength => 0);
39  
40 5 my $URL = Text::Template->new(TYPE => 'STRING', SOURCE => 'https://finance.yahoo.com/quote/{$symbol}/history?p={$symbol}&crumb={$crumb}');
41 # my $AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36';
42 5 my $AGENT = 'Mozilla/5.0';
43 5 my $XPATH = Text::Template->new(TYPE => 'STRING', SOURCE => '//*[@data-symbol=~"^{$symbol}$"][@data-field=~"regularMarketPrice"]');
44  
45 5 our $DISPLAY = '<Module Name + Brief Info>';
46 5 our @LABELS = qw/symbol name exchange currency isodate last open high low volume/;
47 5 our $METHODHASH = {subroutine => \&yahooweb,
48 display => $DISPLAY,
49 labels => \@LABELS};
50  
51 10 ▶sub methodinfo {
Subroutine methodinfo: called CC=1 SCAR=0.0
Pod undocumented
52 return (
53 10 yahooweb => $METHODHASH,
54 );
55 }
56  
57 5 ▶sub labels {
Subroutine labels: called CC=1 SCAR=0.0
Pod undocumented
58 5 my %m = methodinfo();
59 5 return map {$_ => [@{$m{$_}{labels}}] } keys %m;
60 }
61  
62 5 ▶sub methods {
Subroutine methods: called CC=1 SCAR=0.0
Pod undocumented
63 5 my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m;
64 }
65  
66 0 ▶sub yahooweb {
Subroutine yahooweb: not called CC=26 SCAR=65.5
Pod undocumented
67 0 my $quoter = shift;
68  
69 0 my @stocks = @_;
70 0 my ( %info, $url, $reply, $script_tag );
71 0 my $ua = $quoter->user_agent();
72 0 my $agent = $ua->agent();
73  
74 0 my $cookie_jar = HTTP::Cookies->new;
75  
76 # Redirect handler deals with cookie consent workflow applicable to EU countries
77 # credit to John Weber from Germany for injecting redirect handler
78 0 my $gcrumb = "";
79 0 ▶ $ua->add_handler("response_redirect", sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
80 0 my($response, $ua, $h) = @_;
81  
82 # Check where we've been redirected and act accordingly
83 0 my $redirect_uri = URI->new($response->header("Location"));
84 0 ▶ if ($redirect_uri->path eq "/consent") {
Branch: 2 branches
BranchTrueFalseTotal
if ($redirect_uri->path eq '/consent') { } 0 0 0
elsif ($redirect_uri->path eq '/v2/collectConsent') { } 0 0 0
85  
86 # Remember gcrumb value for collectConsent request later
87 0 my %params = $redirect_uri->query_form;
88 0 $gcrumb = $params{'gcrumb'};
89  
90 } elsif ($redirect_uri->path eq "/v2/collectConsent") {
91  
92 0 my %params = $redirect_uri->query_form;
93 0 my $sessionId = $params{'sessionId'};
94  
95 # Turn this request into a POST with form data to confoo accept cookies
96 0 my $request = POST($redirect_uri, [
97 'csrfToken' => $gcrumb,
98 'sessionId' => $sessionId,
99 'originalDoneUrl' => 'https://www.yahoo.com/?guccounter=1',
100 'namespace' => 'yahoo',
101 # For the EU consent, either can :
102 # 'agree' => 'agree'
103 # to it or
104 'reject' => 'reject'
105 ]);
106 0 return $request;
107 }
108 0 return;
109 0 });
110  
111 0 $ua->cookie_jar($cookie_jar);
112 0 $ua->agent($AGENT);
113  
114 # Tell user agent to redirect POSTs in additional to GET AND HEAD
115 0 $ua->requests_redirectable(['GET', 'HEAD', 'POST']);
116  
117 # get necessary cookies
118 0 $reply = $ua->get('https://www.yahoo.com/', "Accept" => "text/html");
119  
120 0 ▶ if ($reply->code != 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reply->code != 200) 0 0 0
121 0 foreach my $symbol (@stocks) {
122 0 $info{$symbol, "success"} = 0;
123 0 $info{$symbol, "errormsg"} = "Error accessing www.yahoo.com: $@";
124 }
125 0 ▶ return wantarray() ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
126 }
127  
128 # get the crumb that corrosponds to cookies retrieved
129 0 $reply = $ua->request(GET 'https://query2.finance.yahoo.com/v1/test/getcrumb');
130 0 ▶ if ($reply->code != 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reply->code != 200) 0 0 0
131 0 foreach my $symbol (@stocks) {
132 0 $info{$symbol, "success"} = 0;
133 0 $info{$symbol, "errormsg"} = "Error accessing queary.finance.yahoo.com/v1/test/getcrumb: $@";
134 }
135 0 ▶ return wantarray() ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
136 }
137 0 my $crumb = uri_escape($reply->content);
138  
139 ### [<now>] cookie_jar : $cookie_jar
140 ### [<now>] crumb : $crumb
141  
142 0 foreach my $symbol (@stocks) {
143 0 $url = $URL->fill_in(HASH => {symbol => $symbol, crumb => $crumb});
144  
145 ### [<now>] YahooWeb: $url
146 0 $reply = $ua->request(GET $url);
147  
148 ### [<now>] Reply: $reply
149  
150 0 ▶ unless ($reply->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reply->is_success) 0 0 0
151 0 $info{ $symbol, "success" } = 0;
152 0 $info{ $symbol, "errormsg" } = join ' ', $reply->code, $reply->message;
153 0 next;
154 }
155
156 0 my $tree = HTML::TreeBuilder::XPath->new();
157 0 $tree->ignore_unknown(0);
158 0 $tree->parse($reply->decoded_content);
159  
160 0 $script_tag = $tree->look_down(_tag => 'script', type => 'application/json', URLTAG, qr!https://query1.finance.yahoo.com/v7/finance/quote\?fields=fiftyTwoWeekHigh.*! );
161  
162 ### [<now>] script_tag: $script_tag
163  
164 0 ▶ unless($script_tag) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($script_tag) 0 0 0
165 0 $info{ $symbol, "success" } = 0;
166 0 $info{ $symbol, "errormsg" } = 'Error - Symbol not found';
167 0 next;
168 }
169  
170 0 my @numfound = $script_tag->content_list();
171  
172 ### [<now>] numfound: @numfound
173  
174 0 my $json_data;
175 0 eval {$json_data = decode_json encode_utf8( $numfound[0] )};
176 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
177 0 $info{ $symbol, 'success' } = 0;
178 0 $info{ $symbol, 'errormsg' } = $@;
179 0 next;
180 }
181  
182 ### [<now>] json_data: $json_data
183  
184 my $json_body =
185 0 encode_utf8($json_data->{'body'});
186 ### [<now>] json_body: $json_body;
187  
188 0 eval {$json_data = decode_json $json_body};
189 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
190 0 $info{ $symbol, 'success' } = 0;
191 0 $info{ $symbol, 'errormsg' } = $@;
192 0 next;
193 }
194  
195 ### [<now>] json_data 2: $json_data
196  
197 my $yahoo_symbol =
198 0 $json_data->{'quoteResponse'}{'result'}[0]{'symbol'};
199  
200 0 my $name = $json_data->{'quoteResponse'}{'result'}[0]{'shortName'};
201
202 0 ▶ if (uc($symbol) ne uc($yahoo_symbol)) {
Branch: 1 branch
BranchTrueFalseTotal
if (uc $symbol ne uc $yahoo_symbol) 0 0 0
203 ### Error: $symbol, $yahoo_symbol
204 0 $info{ $symbol, "success" } = 0;
205 0 $info{ $symbol, "errormsg" } = 'Unexpected response from Yahoo site';
206 0 next;
207 }
208  
209 0 ▶ $info{ $symbol, 'name' } = $name if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 0 0
210 0 my $currency = $json_data->{'quoteResponse'}{'result'}[0]{'currency'};
211 0 ▶ $info{ $symbol, 'currency' } = $currency if $currency;
Branch: 1 branch
BranchTrueFalseTotal
if $currency 0 0 0
212 $info{ $symbol, 'exchange' } =
213 0 $json_data->{'quoteResponse'}{'result'}[0]{'fullExchangeName'};
214  
215 0 ▶ if ($currency =~ /^GBp/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($currency =~ /^GBp/) { } 0 0 0
216 0 $info{ $symbol, 'currency' } = 'GBP';
217 } else {
218 0 $info{ $symbol, 'currency' } = $currency;
219 }
220  
221 0 my $last = $json_data->{'quoteResponse'}{'result'}[0]{'regularMarketPrice'}{'fmt'};
222 0 $last =~ s/,//g;
223 0 ▶ if ($currency =~ /^GBp/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($currency =~ /^GBp/) 0 0 0
224 0 $last = $last / 100;
225 }
226  
227 0 my $open = $json_data->{'quoteResponse'}{'result'}[0]{'regularMarketOpen'}{'fmt'};
228 0 ▶ if ($open) {
Branch: 1 branch
BranchTrueFalseTotal
if ($open) 0 0 0
229 0 $open =~ s/,//g;
230 0 ▶ if ($currency =~ /^GBp/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($currency =~ /^GBp/) 0 0 0
231 0 $open = $open / 100;
232 }
233 }
234  
235 0 my $high = $json_data->{'quoteResponse'}{'result'}[0]{'regularMarketDayHigh'}{'fmt'};
236 0 ▶ if ($high) {
Branch: 1 branch
BranchTrueFalseTotal
if ($high) 0 0 0
237 0 $high =~ s/,//g;
238 0 ▶ if ($currency =~ /^GBp/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($currency =~ /^GBp/) 0 0 0
239 0 $high = $high / 100;
240 }
241 }
242  
243 0 my $low = $json_data->{'quoteResponse'}{'result'}[0]{'regularMarketDayLow'}{'fmt'};
244 0 ▶ if ($low) {
Branch: 1 branch
BranchTrueFalseTotal
if ($low) 0 0 0
245 0 $low =~ s/,//g;
246 0 ▶ if ($currency =~ /^GBp/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($currency =~ /^GBp/) 0 0 0
247 0 $low = $low / 100;
248 }
249 }
250  
251 0 my $volume = $json_data->{'quoteResponse'}{'result'}[0]{'regularMarketVolume'}{'raw'};
252 0 ▶ $volume =~ s/,//g if $volume;
Branch: 1 branch
BranchTrueFalseTotal
if $volume 0 0 0
253  
254 # regularMarketTime in JSON is seconds since epoch
255 0 my $tradedate = $json_data->{'quoteResponse'}{'result'}[0]{'regularMarketTime'}{'raw'};
256 0 my (undef,undef,undef,$day,$month,$year,undef,undef,undef) = localtime($tradedate);
257 0 $month += 1;
258 0 $year += 1900;
259  
260 ### YahooWeb Result: $last
261 0 $info{ $symbol, 'last'} = $last;
262 0 $info{ $symbol, 'open'} = $open;
263 0 $info{ $symbol, 'high'} = $high;
264 0 $info{ $symbol, 'low'} = $low;
265 0 ▶ $info{ $symbol, 'volume'} = $volume unless $volume eq "-";
Branch: 1 branch
BranchTrueFalseTotal
unless $volume eq "-" 0 0 0
266  
267 0 $quoter->store_date(\%info, $symbol, {month => $month, day => $day, year => $year});
268 0 $info{ $symbol, 'symbol' } = $symbol;
269 0 $info{ $symbol, 'method' } = 'yahooweb';
270 0 $info{ $symbol, 'success' } = 1;
271 }
272 0 $ua->agent($agent);
273 0 ▶ return wantarray ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
274 }
275 5 1;
276  
277 =head1 NAME
278  
279 Finance::Quote::YahooWeb - Obtain quotes from https://finance.yahoo.com/quote
280  
281 =head1 SYNOPSIS
282  
283 use Finance::Quote;
284 $q = Finance::Quote->new('YahooWeb');
285 %info = $q->fetch('yahooweb', "IBM", "AAPL");
286  
287 =head1 DESCRIPTION
288  
289 This module fetches information from https://finance.yahoo.com/quote.
290  
291 This module is loaded by default on a Finance::Quote object. It's
292 also possible to load it explicitly by placing "YahooWeb" in the argument
293 list to Finance::Quote->new().
294  
295 This module provides the "yahooweb" fetch method.
296  
297 =head1 LABELS RETURNED
298  
299 The following labels may be returned by Finance::Quote::YahooWeb :
300 symbol name exchange currency isodate last open high low volume
301  
302 =cut
« Quote/CurrencyRates/AllRatesToday.pm ↑ Summary Quote/OnVista.pm »