Quote/Fool.pm

filter: Statementstmt 32.1% 37 / 115 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.9% 10 / 11 Podpod 0.0% 0 / 4 totaltotal 30.7% 47 / 153 CC 13 SCAR 46.4
CC
13
Cov
27%
fool
182.0
BEGIN
1.0
BEGIN
1.0
CRAP 103.3
« Quote/BVB.pm ↑ Summary Quote/SIX.pm »
1 #!/usr/bin/perl -w
2 # vi: set noai ts=2 sw=2 ic showmode showmatch:
3 # This module was rewritten in June 2019 based on the
4 # Finance::Quote::IEXCloud.pm module and prior versions of Fool.pm
5 # that carried the following copyrights:
6 #
7 # Copyright (C) 1998, Dj Padzensky <djpadz@padz.net>
8 # Copyright (C) 1998, 1999 Linas Vepstas <linas@linas.org>
9 # Copyright (C) 2000, Yannick LE NY <y-le-ny@ifrance.com>
10 # Copyright (C) 2000, Paul Fenwick <pjf@cpan.org>
11 # Copyright (C) 2000, Brent Neal <brentn@users.sourceforge.net>
12 # Copyright (C) 2001, Tobias Vancura <tvancura@altavista.net>
13 #
14 # This program is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 2 of the License, or
17 # (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 # 02110-1301, USA
28  
29 package Finance::Quote::Fool;
30  
31 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 5 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 5 ▶use JSON qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
34 5 ▶use Text::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 5 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 5 ▶use constant DEBUG => $ENV{DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
38 5 ▶use if DEBUG, 'Smart::Comments';
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 5 our $VERSION = '1.72_01'; # TRIAL VERSION
41  
42 5 my $SEARCHURL = Text::Template->new(TYPE => 'STRING', SOURCE => 'https://api.fool.com/quotes/v4/instruments/search/?maxResults=10&apikey=public&domain=fool.com&query={$symbol}');
43  
44 # my $QUOTEURL = Text::Template->new(TYPE => 'STRING', SOURCE => 'https://www.fool.com/quote/{$lcexchange}/{$lcsymbol}/');
45  
46 5 my $QUOTEURL = Text::Template->new(TYPE => 'STRING', SOURCE => 'https://api.fool.com/quotes/v4/historical/charts/{$instrumentID}?timeFrame=OneWeek&precision=Day&apikey=6cbf5f34-ba40-4108-a1ab-d951c608955e');
47  
48 # Fool returns JSON with a Currency stanza
49 # '1' -> USD
50 5 my %currencies_by_id = (
51 '1' => 'USD',
52 );
53  
54 5 our $DISPLAY = 'Fool - Motley Fool Website Scrape';
55 5 our @LABELS = qw/date isodate open high low close volume last currency method/;
56 5 our $METHODHASH = {subroutine => \&fool,
57 display => $DISPLAY,
58 labels => \@LABELS};
59  
60 10 ▶sub methodinfo {
Subroutine methodinfo: called CC=1 SCAR=0.0
Pod undocumented
61 return (
62 10 fool => $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 fool {
Subroutine fool: not called CC=13 SCAR=52.0
Pod undocumented
79 0 my $quoter = shift;
80 0 my @stocks = @_;
81
82 0 my (%info, $symbol, $url, $reply, $code, $desc, $body);
83 0 my ($json_data, $instrumentID, $exchange, $tree );
84 0 my %mnames = (jan => 1, feb => 2, mar => 3, apr => 4, may => 5, jun => 6,
85 jul => 7, aug => 8, sep => 9, oct =>10, nov =>11, dec =>12);
86 0 my $ua = $quoter->user_agent();
87 0 $ua->agent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36');
88
89 0 my $quantity = @stocks;
90  
91 ### Stocks: @stocks
92  
93 0 foreach my $symbol (@stocks) {
94  
95 # Get the JSON with possible matches
96 0 $url = $SEARCHURL->fill_in(HASH => {symbol => $symbol});
97 ### url: $url
98 0 $reply = $ua->request( GET $url);
99 0 $code = $reply->code;
100 0 $desc = HTTP::Status::status_message($code);
101 0 $body = decode('UTF-8', $reply->content);
102  
103 ### Reply: $reply
104
105 0 ▶ if ($code != 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code != 200) 0 0 0
106 0 $info{ $symbol, 'success' } = 0;
107 0 $info{ $symbol, 'errormsg' } = $desc;
108 0 next;
109 }
110  
111 ### Body: $body
112  
113 # Parse the JSON
114 0 eval {$json_data = JSON::decode_json $body};
115 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
116 0 $info{ $symbol, 'success' } = 0;
117 0 $info{ $symbol, 'errormsg' } = $@;
118 0 next;
119 }
120  
121 # The JSON returned may return information for multiple
122 # securities
123 # {
124 # "ResultsFound": true,
125 # "SearchResults": [
126 # {
127 # "IndexedDate": "2023-01-14T02:44:40+00:00",
128 # "InstrumentId": 203983,
129 # "Symbol": "IBM",
130 # "Exchange": "NYSE",
131 # "Name": "International Business Machines",
132 # "AssetClass": "stock",
133 # "Popularity": 0,
134 # "Country": "US",
135 # "Sector": "Information Technology",
136 # "Industry": "IT Services",
137 # "IgnoreInSearch": false,
138 # "Relevance": 86.37764
139 # },
140 # {
141 # "IndexedDate": "2023-01-14T02:44:45+00:00",
142 # "InstrumentId": 270916,
143 # "Symbol": "IBM",
144 # "Exchange": "LSE",
145 # "Name": "International Business Machines",
146 # "AssetClass": "stock",
147 # "Popularity": 0,
148 # "Country": "US",
149 # "Sector": "Information Technology",
150 # "Industry": "IT Services",
151 # "IgnoreInSearch": false,
152 # "Relevance": 80.79494
153 # }
154 # ]
155 # }
156  
157 0 my $searchresults = $json_data->{'SearchResults'};
158  
159 # Loop through the array looking for a match for Symbol and
160 # US Exchanges
161 # In the future, symbols supplied can be "LSE:IBM". Will match
162 # Symbol and Exchange.
163  
164 0 foreach my $item( @$searchresults ) {
165 0 ▶ if ( $item->{'Symbol'} eq $symbol &&
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{"Symbol"} eq $symbol and $item->{"Exchange"} =~ /NYSE|NASDAQ|OTC/) 0 0 0
Condition0%0/3
$item->{"Symbol"} eq $symbol and $item->{"Exchange"} =~ /NYSE|NASDAQ|OTC/
!ll&&!rl&&r
000
MC/DC0%0/2
$item->{"Symbol"} eq $symbol and $item->{"Exchange"} =~ /NYSE|NASDAQ|OTC/
$item->{"Symbol"} eq $symbol $item->{"Exchange"} =~ /NYSE|NASDAQ|OTC/
Truth table0%0/3
A and B
A= $item->{"Symbol"} eq $symbolB= $item->{"Exchange"} =~ /NYSE|NASDAQ|OTC/
ABresult
0X0
100
111
166 $item->{'Exchange'} =~ /NYSE|NASDAQ|OTC/ ) {
167 0 $instrumentID = $item->{'InstrumentId'};
168 0 $exchange = $item->{'Exchange'};
169 0 last;
170 }
171 }
172  
173 # If instrumentID is not set return error
174 0 ▶ unless ($instrumentID) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($instrumentID) 0 0 0
175 0 $info{ $symbol, "success" } = 0;
176 0 $info{ $symbol, "errormsg" } = "Stock symbol not found";
177 0 next;
178 }
179  
180 # Create QUOTE URL
181 0 $url = $QUOTEURL->fill_in(HASH => {instrumentID => $instrumentID});
182  
183 ### [<now>] Quote URL: $url
184 0 $reply = $ua->request( GET $url);
185 0 $code = $reply->code;
186 0 $desc = HTTP::Status::status_message($code);
187 0 $body = decode('UTF-8', $reply->content);
188  
189 ### [<now>] Reply: $reply
190
191 0 ▶ if ($code != 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code != 200) 0 0 0
192 0 $info{ $symbol, 'success' } = 0;
193 0 $info{ $symbol, 'errormsg' } = $desc;
194 0 next;
195 }
196  
197 ### [<now>] Body: $body
198  
199 # Parse the JSON
200 0 eval {$json_data = JSON::decode_json $body};
201 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
202 0 $info{ $symbol, 'success' } = 0;
203 0 $info{ $symbol, 'errormsg' } = $@;
204 0 next;
205 }
206  
207 0 my $numChartBars = scalar @{$json_data->{'ChartBars'}};
208 ### [<now>] Number of ChartBars: $numChartBars
209 0 my $cb = $numChartBars - 1;
210  
211 0 ▶ if ( $json_data->{'Symbol'} ne $symbol ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json_data->{'Symbol'} ne $symbol) { } 0 0 0
212 0 $info{ $symbol, 'success' } = 0;
213 0 $info{ $symbol, 'errormsg' } = "Unexpect Data in JSON";
214 0 next;
215 } else {
216 0 $info{ $symbol, 'symbol' } = $symbol;
217 }
218  
219 0 my $name = $json_data->{'Name'};
220 0 my $currencyid = $json_data->{'Currency'}{'Id'};
221 0 ▶ if ( $currencies_by_id{$currencyid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($currencies_by_id{$currencyid}) 0 0 0
222 0 $info{ $symbol, 'currency' } = $currencies_by_id{$currencyid};
223 }
224 0 my $date = $json_data->{'ChartBars'}[$cb]{'PricingDate'};
225 0 my $open = $json_data->{'ChartBars'}[$cb]{'Open'}{'Amount'};
226 0 my $last = $json_data->{'ChartBars'}[$cb]{'Close'}{'Amount'};
227 0 my $high = $json_data->{'ChartBars'}[$cb]{'High'}{'Amount'};
228 0 my $low = $json_data->{'ChartBars'}[$cb]{'Low'}{'Amount'};
229 0 my $volume = $json_data->{'ChartBars'}[$cb]{'Volume'};
230 # my $currency = $json_data->{'ChartBars'}[$cb]{'Close'}{'CurrencyCode'};
231  
232 0 $info{ $symbol, 'success' } = 1;
233 0 $info{ $symbol, 'name' } = $name;
234 # $info{ $symbol, 'currency' } = $currency;
235 0 $info{ $symbol, 'open' } = $open;
236 0 $info{ $symbol, 'last' } = $last;
237 0 $info{ $symbol, 'high' } = $high;
238 0 $info{ $symbol, 'low' } = $low;
239 0 $info{ $symbol, 'volume' } = $volume;
240 0 $info{ $symbol, 'method' } = 'fool';
241 0 $quoter->store_date(\%info, $symbol, {isodate => $date});
242  
243 0 sleep 1;
244  
245 }
246  
247 0 ▶ return wantarray() ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
248 }
249  
250 5 1;
251  
252 =head1 NAME
253  
254 Finance::Quote::Fool - Obtain quotes from the Motley Fool web site.
255  
256 =head1 SYNOPSIS
257  
258 use Finance::Quote;
259  
260 $q = Finance::Quote->new;
261  
262 %stockinfo = $q->fetch('fool','GE', 'INTC');
263  
264 =head1 DESCRIPTION
265  
266 This module obtains information from the Motley Fool website
267 (http://caps.fool.com). The site provides date from NASDAQ, NYSE and AMEX.
268  
269 This module is loaded by default on a Finance::Quote object. It's
270 also possible to load it explicitly by placing "Fool" in the argument
271 list to Finance::Quote->new().
272  
273 Information returned by this module is governed by the Motley Fool's terms and
274 conditions.
275  
276 =head1 LABELS RETURNED
277  
278 The following labels may be returned by Finance::Quote::Fool:
279 symbol, open, high, low, volume, last, currency, method.
280  
281 =head1 SEE ALSO
282  
283 Motley Fool, http://www.fool.com
284  
285 Finance::Quote.
286  
287 =cut
« Quote/BVB.pm ↑ Summary Quote/SIX.pm »