Quote/MarketWatch.pm

filter: Statementstmt 42.8% 36 / 84 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.9% 10 / 11 Podpod 0.0% 0 / 4 totaltotal 42.9% 46 / 107 CC 6 SCAR 28.0
CC
6
Cov
39%
marketwatch
42.0
BEGIN
1.0
BEGIN
1.0
CRAP 16.4
« Quote/CurrencyRates/FinanceAPI.pm ↑ Summary Quote/MorningstarJP.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::MarketWatch;
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 5 ▶use HTML::TreeBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 5 ▶use constant DEBUG => $ENV{DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
32 5 ▶use if DEBUG, 'Smart::Comments', '###';
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 5 our $VERSION = '1.72'; # VERSION
35  
36 5 our $DISPLAY = 'MarketWatch';
37 5 our $FEATURES = {};
38 5 our @LABELS = qw/symbol name last date currency method/;
39 5 our $METHODHASH = {subroutine => \&marketwatch,
40 display => $DISPLAY,
41 labels => \@LABELS,
42 features => $FEATURES};
43  
44 10 ▶sub methodinfo {
Subroutine methodinfo: called CC=1 SCAR=0.0
Pod undocumented
45 return (
46 10 marketwatch => $METHODHASH,
47 nyse => $METHODHASH,
48 nasdaq => $METHODHASH,
49 );
50 }
51  
52 5 ▶sub methods {
Subroutine methods: called CC=1 SCAR=0.0
Pod undocumented
53 5 my %m = methodinfo();
54 5 return map {$_ => $m{$_}{subroutine} } keys %m;
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 my $MW_URL = 'https://www.marketwatch.com/investing/stock/';
63  
64 0 ▶sub marketwatch {
Subroutine marketwatch: not called CC=6 SCAR=37.4
Pod undocumented
65  
66 0 my $quoter = shift;
67 0 my @stocks = @_;
68 0 my (%info, $tree, $url, $reply);
69 0 my $ua = $quoter->user_agent();
70  
71 0 $ua->agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36");
72  
73 0 my @ua_headers = (
74 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
75 'Accept-Language' => 'en-US,en;q=0.9',
76 'Accept-Encoding' => 'gzip, deflate, br',
77 'Connection' => 'keep-alive',
78 'Upgrade-Insecure-Requests' => '1',
79 'Sec-Fetch-Dest' => 'document',
80 'Sec-Fetch-Mode' => 'navigate',
81 'Sec-Fetch-Site' => 'none',
82 'Sec-Fetch-User' => '?1',
83 );
84  
85 # Enable cookie handling (websites often check if you can store cookies)
86 0 $ua->cookie_jar({});
87  
88 0 foreach my $stock (@stocks) {
89  
90 0 $url = $MW_URL . $stock;
91 0 $reply = $ua->get($url, @ua_headers);
92  
93 0 my $code = $reply->code;
94 0 my $desc = HTTP::Status::status_message($code);
95 0 my $headers = $reply->headers_as_string;
96 0 my $body = $reply->decoded_content;
97  
98 ### Body: $body
99  
100 0 my ($name, $last, $open, $date, $currency, $metatag);
101 0 my (%datehash, @timearray, $timestring);
102  
103 0 $info{ $stock, "symbol" } = $stock;
104  
105 0 ▶ if ( $code == 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 200) { } 0 0 0
106  
107 # Use HTML::TreeBuilder to parse HTML in $body
108 0 $tree = HTML::TreeBuilder->new;
109 0 ▶ if ($tree->parse($body)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree->parse($body)) { } 0 0 0
110  
111 0 $tree->eof;
112  
113 0 ▶ if ($metatag = $tree->look_down(_tag => 'meta', name => 'name')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($metatag = $tree->look_down('_tag', 'meta', 'name', 'name')) { } 0 0 0
114 0 $info{ $stock, 'name' } = $metatag->attr('content');
115 } else {
116 0 $info{ $stock, 'success'} = 0;
117 0 $info{ $stock, 'errormsg' } = 'Error retrieving quote for $stock.';
118 0 next;
119 }
120  
121 0 $metatag = $tree->look_down(_tag => 'meta', name => 'tickerSymbol');
122 0 $info{ $stock, 'symbol' } = $metatag->attr('content');
123  
124 0 $metatag = $tree->look_down(_tag => 'meta', name => 'price');
125 0 ($info{ $stock, 'last' } = $metatag->attr('content')) =~ s/[^0-9.]//g;
126  
127 0 $metatag = $tree->look_down(_tag => 'meta', name => 'priceCurrency');
128 0 $info{ $stock, 'currency' } = $metatag->attr('content');
129  
130 0 $metatag = $tree->look_down(_tag => 'meta', name => 'quoteTime');
131 0 $date = $metatag->attr('content');
132 0 @timearray = split / /, $date;
133 0 $timearray[1] =~ s/[^0-9]//g;
134 0 %datehash = (
135 year => $timearray[2],
136 month => $timearray[0],
137 day => $timearray[1] );
138 0 ($timestring = $timearray[4]) =~ s|\.||g;
139 0 $quoter->store_date(\%info, $stock, \%datehash);
140  
141 0 $info{ $stock, 'success' } = 1;
142  
143 0 $info{ $stock, 'method' } = 'marketwatch';
144  
145 } else {
146 0 $tree->eof;
147 0 $info{ $stock, "success" } = 0;
148 0 $info{ $stock, "errormsg" } =
149 "Error retrieving quote for $stock. Could not parse HTML returned from $url.";
150 }
151  
152 } else { # HTTP Request failed (code != 200)
153 0 $info{ $stock, "success" } = 0;
154 0 $info{ $stock, "errormsg" } =
155 "Error retrieving quote for $stock. Attempt to fetch the URL $url resulted in HTTP response $code ($desc)";
156 }
157  
158 }
159  
160 0 ▶ return wantarray() ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
161 0 return \%info;
162  
163 }
164  
165 5 1;
166  
167 __END__
« Quote/CurrencyRates/FinanceAPI.pm ↑ Summary Quote/MorningstarJP.pm »