| 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 <[email protected]> |
| 8 |
# Copyright (C) 1998, 1999 Linas Vepstas <[email protected]> |
| 9 |
# Copyright (C) 2000, Yannick LE NY <[email protected]> |
| 10 |
# Copyright (C) 2000, Paul Fenwick <[email protected]> |
| 11 |
# Copyright (C) 2000, Brent Neal <[email protected]> |
| 12 |
# Copyright (C) 2001, Tobias Vancura <[email protected]> |
| 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 |
use strict; |
| 32 |
use HTTP::Request::Common; |
| 33 |
use JSON qw( decode_json ); |
| 34 |
use Text::Template; |
| 35 |
use Encode qw(decode); |
| 36 |
|
| 37 |
use constant DEBUG => $ENV{DEBUG}; |
| 38 |
use if DEBUG, 'Smart::Comments'; |
| 39 |
|
| 40 |
our $VERSION = '1.71'; # VERSION |
| 41 |
|
| 42 |
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 |
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 |
my %currencies_by_id = ( |
| 51 |
'1' => 'USD', |
| 52 |
); |
| 53 |
|
| 54 |
our $DISPLAY = 'Fool - Motley Fool Website Scrape'; |
| 55 |
our @LABELS = qw/date isodate open high low close volume last currency method/; |
| 56 |
our $METHODHASH = {subroutine => \&fool, |
| 57 |
display => $DISPLAY, |
| 58 |
labels => \@LABELS}; |
| 59 |
|
| 60 |
sub methodinfo { |
| 61 |
return ( |
| 62 |
fool => $METHODHASH, |
| 63 |
nyse => $METHODHASH, |
| 64 |
nasdaq => $METHODHASH, |
| 65 |
usa => $METHODHASH, |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
sub labels { |
| 70 |
my %m = methodinfo(); |
| 71 |
return map {$_ => [@{$m{$_}{labels}}] } keys %m; |
| 72 |
} |
| 73 |
|
| 74 |
sub methods { |
| 75 |
my %m = methodinfo(); return map {$_ => $m{$_}{subroutine} } keys %m; |
| 76 |
} |
| 77 |
|
| 78 |
sub fool { |
| 79 |
my $quoter = shift; |
| 80 |
my @stocks = @_; |
| 81 |
|
| 82 |
my (%info, $symbol, $url, $reply, $code, $desc, $body); |
| 83 |
my ($json_data, $instrumentID, $exchange, $tree ); |
| 84 |
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 |
my $ua = $quoter->user_agent(); |
| 87 |
$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 |
my $quantity = @stocks; |
| 90 |
|
| 91 |
### Stocks: @stocks |
| 92 |
|
| 93 |
foreach my $symbol (@stocks) { |
| 94 |
|
| 95 |
# Get the JSON with possible matches |
| 96 |
$url = $SEARCHURL->fill_in(HASH => {symbol => $symbol}); |
| 97 |
### url: $url |
| 98 |
$reply = $ua->request( GET $url); |
| 99 |
$code = $reply->code; |
| 100 |
$desc = HTTP::Status::status_message($code); |
| 101 |
$body = decode('UTF-8', $reply->content); |
| 102 |
|
| 103 |
### Reply: $reply |
| 104 |
|
| 105 |
if ($code != 200) { |
| 106 |
$info{ $symbol, 'success' } = 0; |
| 107 |
$info{ $symbol, 'errormsg' } = $desc; |
| 108 |
next; |
| 109 |
} |
| 110 |
|
| 111 |
### Body: $body |
| 112 |
|
| 113 |
# Parse the JSON |
| 114 |
eval {$json_data = JSON::decode_json $body}; |
| 115 |
if ($@) { |
| 116 |
$info{ $symbol, 'success' } = 0; |
| 117 |
$info{ $symbol, 'errormsg' } = $@; |
| 118 |
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 |
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 |
foreach my $item( @$searchresults ) { |
| 165 |
if ( $item->{'Symbol'} eq $symbol && |
| 166 |
$item->{'Exchange'} =~ /NYSE|NASDAQ|OTC/ ) { |
| 167 |
$instrumentID = $item->{'InstrumentId'}; |
| 168 |
$exchange = $item->{'Exchange'}; |
| 169 |
last; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
# If instrumentID is not set return error |
| 174 |
unless ($instrumentID) { |
| 175 |
$info{ $symbol, "success" } = 0; |
| 176 |
$info{ $symbol, "errormsg" } = "Stock symbol not found"; |
| 177 |
next; |
| 178 |
} |
| 179 |
|
| 180 |
# Create QUOTE URL |
| 181 |
$url = $QUOTEURL->fill_in(HASH => {instrumentID => $instrumentID}); |
| 182 |
|
| 183 |
### [<now>] Quote URL: $url |
| 184 |
$reply = $ua->request( GET $url); |
| 185 |
$code = $reply->code; |
| 186 |
$desc = HTTP::Status::status_message($code); |
| 187 |
$body = decode('UTF-8', $reply->content); |
| 188 |
|
| 189 |
### [<now>] Reply: $reply |
| 190 |
|
| 191 |
if ($code != 200) { |
| 192 |
$info{ $symbol, 'success' } = 0; |
| 193 |
$info{ $symbol, 'errormsg' } = $desc; |
| 194 |
next; |
| 195 |
} |
| 196 |
|
| 197 |
### [<now>] Body: $body |
| 198 |
|
| 199 |
# Parse the JSON |
| 200 |
eval {$json_data = JSON::decode_json $body}; |
| 201 |
if ($@) { |
| 202 |
$info{ $symbol, 'success' } = 0; |
| 203 |
$info{ $symbol, 'errormsg' } = $@; |
| 204 |
next; |
| 205 |
} |
| 206 |
|
| 207 |
my $numChartBars = scalar @{$json_data->{'ChartBars'}}; |
| 208 |
### [<now>] Number of ChartBars: $numChartBars |
| 209 |
my $cb = $numChartBars - 1; |
| 210 |
|
| 211 |
if ( $json_data->{'Symbol'} ne $symbol ) { |
| 212 |
$info{ $symbol, 'success' } = 0; |
| 213 |
$info{ $symbol, 'errormsg' } = "Unexpect Data in JSON"; |
| 214 |
next; |
| 215 |
} else { |
| 216 |
$info{ $symbol, 'symbol' } = $symbol; |
| 217 |
} |
| 218 |
|
| 219 |
my $name = $json_data->{'Name'}; |
| 220 |
my $currencyid = $json_data->{'Currency'}{'Id'}; |
| 221 |
if ( $currencies_by_id{$currencyid} ) { |
| 222 |
$info{ $symbol, 'currency' } = $currencies_by_id{$currencyid}; |
| 223 |
} |
| 224 |
my $date = $json_data->{'ChartBars'}[$cb]{'PricingDate'}; |
| 225 |
my $open = $json_data->{'ChartBars'}[$cb]{'Open'}{'Amount'}; |
| 226 |
my $last = $json_data->{'ChartBars'}[$cb]{'Close'}{'Amount'}; |
| 227 |
my $high = $json_data->{'ChartBars'}[$cb]{'High'}{'Amount'}; |
| 228 |
my $low = $json_data->{'ChartBars'}[$cb]{'Low'}{'Amount'}; |
| 229 |
my $volume = $json_data->{'ChartBars'}[$cb]{'Volume'}; |
| 230 |
# my $currency = $json_data->{'ChartBars'}[$cb]{'Close'}{'CurrencyCode'}; |
| 231 |
|
| 232 |
$info{ $symbol, 'success' } = 1; |
| 233 |
$info{ $symbol, 'name' } = $name; |
| 234 |
# $info{ $symbol, 'currency' } = $currency; |
| 235 |
$info{ $symbol, 'open' } = $open; |
| 236 |
$info{ $symbol, 'last' } = $last; |
| 237 |
$info{ $symbol, 'high' } = $high; |
| 238 |
$info{ $symbol, 'low' } = $low; |
| 239 |
$info{ $symbol, 'volume' } = $volume; |
| 240 |
$info{ $symbol, 'method' } = 'fool'; |
| 241 |
$quoter->store_date(\%info, $symbol, {isodate => $date}); |
| 242 |
|
| 243 |
sleep 1; |
| 244 |
|
| 245 |
} |
| 246 |
|
| 247 |
return wantarray() ? %info : \%info; |
| 248 |
} |
| 249 |
|
| 250 |
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 |