Quote/GoogleWeb.pm

filter: Statementstmt 40.2% 33 / 82 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.0% 9 / 10 Podpod 0.0% 0 / 4 totaltotal 35.2% 42 / 119 CC 12 SCAR 45.0
CC
12
Cov
32%
googleweb
156.0
BEGIN
1.0
BEGIN
1.0
CRAP 89.6
« Quote/Bourso.pm ↑ Summary Quote/TesouroDireto.pm »
1 #!/usr/bin/perl -w
2 # vi: set ts=2 sw=2 noai ic showmode showmatch:
3 #
4 # Copyright (C) 2023, Bruce Schuck <bschuck@asgard-systems.com>
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 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 5 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 5 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 5 ▶use constant DEBUG => $ENV{DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
31 5 ▶use if DEBUG, 'Smart::Comments', '###';
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 5 our $VERSION = '1.72'; # VERSION
34  
35 5 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 5 my $SEARCH_URL = 'https://www.google.com/complete/search?client=finance-immersive&q=';
41  
42 5 our $DISPLAY = 'GoogleWeb - Scrapes www.google.com/finance/';
43 5 our @LABELS = qw/symbol name last date currency method/;
44 5 our $METHODHASH = {subroutine => \&googleweb,
45 display => $DISPLAY,
46 labels => \@LABELS};
47  
48 10 ▶sub methodinfo {
Subroutine methodinfo: called CC=1 SCAR=0.0
Pod undocumented
49 return (
50 10 googleweb => $METHODHASH,
51 bats => $METHODHASH,
52 nyse => $METHODHASH,
53 nasdaq => $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 googleweb {
Subroutine googleweb: not called CC=12 SCAR=50.5
Pod undocumented
67  
68 0 my $quoter = shift;
69 0 my @stocks = @_;
70 0 my (%info, $url, $reply);
71 0 my $ua = $quoter->user_agent();
72  
73 0 foreach my $stock (@stocks) {
74  
75 0 my $ucstock = uc($stock);
76  
77 0 $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 0 $url = $SEARCH_URL . $ucstock;
89 0 $reply = $ua->request( GET $url );
90  
91 0 my $code = $reply->code;
92 0 my $desc = HTTP::Status::status_message($code);
93  
94 0 ▶ unless ( $code == 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code == 200) 0 0 0
95 0 $info{ $stock, "success" } = 0;
96 0 $info{ $stock, "errormsg" } =
97 "Error retrieving quote for $stock. Attempt to fetch the URL $url resulted in HTTP response $code ($desc)";
98 0 next;
99 }
100  
101 0 my $body = decode('UTF-8', $reply->content);
102  
103 ### Search Body: $body
104  
105 0 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 0 ▶ while ( $body =~ /\{([^{}]*)\}/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /\{([^{}]*)\}/g) 0 0 0
111 0 my $obj = $1;
112 0 ▶ next unless $obj =~ /"t":"([^"]*)"/ && uc($1) eq $ucstock;
Branch: 1 branch
BranchTrueFalseTotal
unless $obj =~ /"t":"([^"]*)"/ and uc $1 eq $ucstock 0 0 0
Condition0%0/3
$obj =~ /"t":"([^"]*)"/ and uc $1 eq $ucstock
!ll&&!rl&&r
000
MC/DC0%0/2
$obj =~ /"t":"([^"]*)"/ and uc $1 eq $ucstock
$obj =~ /"t":"([^"]*)"/ uc $1 eq $ucstock
Truth table0%0/3
A and B
A= $obj =~ /"t":"([^"]*)"/B= uc $1 eq $ucstock
ABresult
0X0
100
111
113 0 ($exchange) = $obj =~ /"x":"([^"]*)"/;
114 0 ($name) = $obj =~ /"c":"([^"]*)"/;
115 0 ▶ last if $exchange;
Branch: 1 branch
BranchTrueFalseTotal
if $exchange 0 0 0
116 }
117  
118 0 ▶ unless ($exchange) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exchange) 0 0 0
119 0 $info{ $stock, "success" } = 0;
120 0 $info{ $stock, "errormsg" } = "$stock not found on Google Finance";
121 0 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 0 $url = $GOOGLE_URL . $ucstock . ':' . $exchange;
136 0 $reply = $ua->request( GET $url );
137  
138 0 ▶ if ( $reply->code != 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reply->code != 200) 0 0 0
139 0 $info{ $stock, "success" } = 0;
140 0 $info{ $stock, "errormsg" } =
141 "Error retrieving quote for $stock from $url";
142 0 next;
143 }
144  
145 0 $body = decode('UTF-8', $reply->content);
146  
147 ### Quote Body: $body
148  
149 0 ▶ if ( $body =~ /\["\Q$ucstock\E","\Q$exchange\E"\],"(?:[^"\\]|\\.)*",\d+,"([A-Z]{2,4})",\[(-?[\d.]+),.{0,300}?,\[(\d{6,})\]/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /\["\Q$ucstock\E","\Q$exchange\E"\],"(?:[^"\\]|\\.)*",\d+,"([A-Z]{2,4})",\[(-?[\d.]+),.{0,300}?,\[(\d{6,})\]/s) { } 0 0 0
150 0 ( $currency, $last, $time ) = ( $1, $2, $3 );
151 } else {
152 0 $info{ $stock, "success" } = 0;
153 0 $info{ $stock, "errormsg" } = "Cannot find price data in $url";
154 0 next;
155 }
156  
157 0 my ( undef, undef, undef, $mday, $mon, $year, undef, undef, undef ) =
158 localtime($time);
159 0 $date = sprintf("%d/%02d/%02d", $year + 1900, $mon + 1, $mday);
160  
161 0 $info{ $stock, 'method' } = 'googleweb';
162 0 ▶ $info{ $stock, 'name' } = $name if defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 0 0 0
163 0 $info{ $stock, 'last' } = $last;
164 0 $info{ $stock, 'currency' } = $currency;
165 0 $info{ $stock, 'exchange' } = $exchange;
166 0 $quoter->store_date(\%info, $stock, { isodate => $date });
167 0 $info{ $stock, 'success' } = 1;
168  
169 }
170  
171 0 ▶ return wantarray() ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
172  
173 }
174  
175 5 1;
176  
177 __END__
« Quote/Bourso.pm ↑ Summary Quote/TesouroDireto.pm »