| 1 | #!/usr/bin/perl -w | ||||||||||
| 2 | # vi: set ts=2 sw=2 noai expandtab ic showmode showmatch: | ||||||||||
| 3 | |||||||||||
| 4 | # This program is free software; you can redistribute it and/or modify | ||||||||||
| 5 | # it under the terms of the GNU General Public License as published by | ||||||||||
| 6 | # the Free Software Foundation; either version 2 of the License, or | ||||||||||
| 7 | # (at your option) any later version. | ||||||||||
| 8 | # | ||||||||||
| 9 | # This program is distributed in the hope that it will be useful, | ||||||||||
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||
| 12 | # GNU General Public License for more details. | ||||||||||
| 13 | # | ||||||||||
| 14 | # You should have received a copy of the GNU General Public License | ||||||||||
| 15 | # along with this program; if not, write to the Free Software | ||||||||||
| 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||||||||||
| 17 | # 02110-1301, USA | ||||||||||
| 18 | |||||||||||
| 19 | package Finance::Quote::TMX; | ||||||||||
| 20 | |||||||||||
| 21 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 5 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 5 | ▶ | use Readonly; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 5 | Readonly my $DEBUG => $ENV{DEBUG}; | |||||||||
| 25 | 5 | ▶ | use if $DEBUG, 'Smart::Comments'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | |||||||||||
| 27 | 5 | ▶ | use HTTP::Request; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 5 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 5 | ▶ | use JSON qw( decode_json encode_json ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 5 | ▶ | use String::Util qw(trim); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | |||||||||||
| 32 | 5 | our $VERSION = '1.72'; # VERSION | |||||||||
| 33 | |||||||||||
| 34 | 5 | our $DISPLAY = 'TMX - Toronto Stock Exchange'; | |||||||||
| 35 | 5 | our @LABELS = qw/currency name exchange volume open high low cap close year_range last p_change symbol isodate date/; | |||||||||
| 36 | 5 | our $METHODHASH = {subroutine => \&tmx, | |||||||||
| 37 | display => $DISPLAY, | ||||||||||
| 38 | labels => \@LABELS}; | ||||||||||
| 39 | |||||||||||
| 40 | 10 | ▶ | sub methodinfo { | ||||||||
|
Subroutine
methodinfo: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 41 | return ( | ||||||||||
| 42 | 10 | tmx => $METHODHASH, | |||||||||
| 43 | tsx => $METHODHASH, | ||||||||||
| 44 | canada => $METHODHASH, | ||||||||||
| 45 | ); | ||||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 5 | ▶ | sub labels { | ||||||||
|
Subroutine
labels: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 49 | 5 | my %m = methodinfo(); | |||||||||
| 50 | 5 | return map {$_ => [@{$m{$_}{labels}}] } keys %m; | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | 5 | ▶ | sub methods { | ||||||||
|
Subroutine
methods: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 54 | 5 | my %m = methodinfo(); | |||||||||
| 55 | 5 | return map {$_ => $m{$_}{subroutine} } keys %m; | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | 0 | ▶ | sub tmx { | ||||||||
|
Subroutine
tmx: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||
| 59 | 0 | my $quoter = shift; | |||||||||
| 60 | 0 | my @symbols = @_; | |||||||||
| 61 | 0 | my $ua = $quoter->user_agent(); | |||||||||
| 62 | 0 | my %info; | |||||||||
| 63 | |||||||||||
| 64 | 0 | foreach my $symbol (@symbols) { | |||||||||
| 65 | 0 | eval { | |||||||||
| 66 | 0 | my $url = 'https://app-money.tmx.com/graphql'; | |||||||||
| 67 | 0 | my $header = ["accept" => "*/*", | |||||||||
| 68 | "accept-language" => "en-US,en;q=0.9", | ||||||||||
| 69 | "authorization" => "", | ||||||||||
| 70 | "content-type" => "application/json", | ||||||||||
| 71 | "locale" => "en", | ||||||||||
| 72 | "sec-ch-ua" => "\"Google Chrome\";v=\"87\", \" Not;A Brand\";v=\"99\", \"Chromium\";v=\"87\"", | ||||||||||
| 73 | "sec-ch-ua-mobile" => "?0", | ||||||||||
| 74 | "sec-fetch-dest" => "empty", | ||||||||||
| 75 | "sec-fetch-mode" => "cors", | ||||||||||
| 76 | "sec-fetch-site" => "same-site"]; | ||||||||||
| 77 | 0 | my $body = "{\"operationName\":\"getQuoteBySymbol\",\"variables\":{\"symbol\":\"$symbol\",\"locale\":\"en\"},\"query\":\"query getQuoteBySymbol(\$symbol: String, \$locale: String) {\\n getQuoteBySymbol(symbol: \$symbol, locale: \$locale) {\\n symbol\\n name\\n price\\n percentChange\\n exchangeName\\n volume\\n openPrice\\n dayHigh\\n dayLow\\n MarketCap\\n prevClose\\n weeks52high\\n weeks52low\\n }\\n}\\n\"}"; | |||||||||
| 78 | |||||||||||
| 79 | |||||||||||
| 80 | 0 | my $request = HTTP::Request->new('POST', $url, $header, $body); | |||||||||
| 81 | 0 | $request->header("referrer" => "https://money.tmx.com/", | |||||||||
| 82 | "referrerPolicy" => "strict-origin-when-cross-origin", | ||||||||||
| 83 | "mode" => "cors"); | ||||||||||
| 84 | |||||||||||
| 85 | 0 | my $reply = $ua->request($request); | |||||||||
| 86 | 0 | ▶ | if (! $reply->is_success) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 0 | $info{$symbol, 'errormsg'} = 'Failed to connect with TMX website'; | |||||||||
| 88 | 0 | $info{$symbol, 'success'} = 0; | |||||||||
| 89 | 0 | return; | |||||||||
| 90 | } | ||||||||||
| 91 | ### Search : $url, $reply->code | ||||||||||
| 92 | ### reply : $reply->content | ||||||||||
| 93 | |||||||||||
| 94 | 0 | my $data = decode_json $reply->content; | |||||||||
| 95 | 0 | ▶ | if (exists $data->{errors}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 0 | $info{$symbol, 'errormsg'} = $data->{errors}[0]->{message}; | |||||||||
| 97 | 0 | $info{$symbol, 'success'} = 0; | |||||||||
| 98 | 0 | return; | |||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | 0 | $data = $data->{data}->{getQuoteBySymbol}; | |||||||||
| 102 | 0 | ▶ | if (lc($data->{symbol}) ne lc($symbol)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 0 | $info{$symbol, 'errormsg'} = "returned symbol was not correct for $symbol"; | |||||||||
| 104 | 0 | $info{$symbol, 'success'} = 0; | |||||||||
| 105 | return | ||||||||||
| 106 | 0 | } | |||||||||
| 107 | |||||||||||
| 108 | 0 | ▶ | if ( $symbol =~ /:us/ix ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | 0 | $info{$symbol, 'currency'} = 'USD'; } | |||||||||
| 110 | 0 | else {$info{$symbol, 'currency'} = 'CAD'} | |||||||||
| 111 | |||||||||||
| 112 | 0 | $info{$symbol, 'name'} = $data->{name}; | |||||||||
| 113 | 0 | $info{$symbol, 'exchange'} = $data->{exchangeName}; | |||||||||
| 114 | 0 | $info{$symbol, 'volume'} = $data->{volume}; | |||||||||
| 115 | 0 | $info{$symbol, 'open'} = $data->{openPrice}; | |||||||||
| 116 | 0 | $info{$symbol, 'high'} = $data->{dayHigh}; | |||||||||
| 117 | 0 | $info{$symbol, 'low'} = $data->{dayLow}; | |||||||||
| 118 | 0 | $info{$symbol, 'cap'} = $data->{MarketCap}; | |||||||||
| 119 | 0 | $info{$symbol, 'close'} = $data->{prevClose}; | |||||||||
| 120 | 0 | $info{$symbol, 'year_range'} = $data->{weeks52low} . ' - ' . $data->{weeks52high}; | |||||||||
| 121 | 0 | $info{$symbol, 'last'} = $data->{price}; | |||||||||
| 122 | 0 | $info{$symbol, 'symbol'} = $data->{symbol}; | |||||||||
| 123 | 0 | $info{$symbol, 'p_change'} = $data->{percentChange}; | |||||||||
| 124 | 0 | $quoter->store_date(\%info, $symbol, {today => 1}); | |||||||||
| 125 | |||||||||||
| 126 | 0 | $info{$symbol, 'success'} = 1; | |||||||||
| 127 | }; | ||||||||||
| 128 | 0 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | 0 | my $error = "TMX failed: $@"; | |||||||||
| 130 | 0 | $info{$symbol, 'success'} = 0; | |||||||||
| 131 | 0 | $info{$symbol, 'errormsg'} = trim($error); | |||||||||
| 132 | } | ||||||||||
| 133 | } | ||||||||||
| 134 | |||||||||||
| 135 | 0 | ▶ | return wantarray() ? %info : \%info; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | } | ||||||||||
| 137 | |||||||||||
| 138 | 5 | 1; | |||||||||
| 139 | |||||||||||
| 140 | =head1 NAME | ||||||||||
| 141 | |||||||||||
| 142 | Finance::Quote::TMX - Obtain quotes from the Toronto Stock Exchange | ||||||||||
| 143 | (https://money.tmx.com) | ||||||||||
| 144 | |||||||||||
| 145 | =head1 SYNOPSIS | ||||||||||
| 146 | |||||||||||
| 147 | use Finance::Quote; | ||||||||||
| 148 | |||||||||||
| 149 | $q = Finance::Quote->new; | ||||||||||
| 150 | |||||||||||
| 151 | %stockinfo = $q->fetch('tmx','NT-T'); # Only query TMX | ||||||||||
| 152 | %stockinfo = $q->fetch('canada','NT'); # Failover to other Canadian sources | ||||||||||
| 153 | |||||||||||
| 154 | =head1 DESCRIPTION | ||||||||||
| 155 | |||||||||||
| 156 | This module obtains information from the Toronto Stock Exchange, | ||||||||||
| 157 | https://money.tmx.com. | ||||||||||
| 158 | |||||||||||
| 159 | This module is loaded by default on a Finance::Quote object. It's also | ||||||||||
| 160 | possible to load it explicitly by placing 'TMX' in the argument list to | ||||||||||
| 161 | Finance::Quote->new(). | ||||||||||
| 162 | |||||||||||
| 163 | =head1 LABELS RETURNED | ||||||||||
| 164 | |||||||||||
| 165 | The following labels are returned by Finance::Quote::TMX: name, | ||||||||||
| 166 | exchange, volume, open, high, low, cap, close, year_range, symbol, last, p_change | ||||||||||
| 167 | |||||||||||
| 168 | =head1 TERMS & CONDITIONS | ||||||||||
| 169 | |||||||||||
| 170 | Use of money.tmx.com is governed by any terms & conditions of that | ||||||||||
| 171 | site and its data provider quotemedia.com. | ||||||||||
| 172 | |||||||||||
| 173 | Finance::Quote is released under the GNU General Public License, version 2, | ||||||||||
| 174 | which explicitly carries a "No Warranty" clause. | ||||||||||
| 175 | |||||||||||
| 176 | =cut | ||||||||||