Debian Codegraph @

lib/Finance/Quote/TMX.pm

raw
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 use strict;
22 use warnings;
23 use Readonly;
24 Readonly my $DEBUG => $ENV{DEBUG};
25 use if $DEBUG, 'Smart::Comments';
26
27 use HTTP::Request;
28 use LWP::UserAgent;
29 use JSON qw( decode_json encode_json );
30 use String::Util qw(trim);
31
32 our $VERSION = '1.71'; # VERSION
33
34 our $DISPLAY = 'TMX - Toronto Stock Exchange';
35 our @LABELS = qw/currency name exchange volume open high low cap close year_range last p_change symbol isodate date/;
36 our $METHODHASH = {subroutine => \&tmx,
37 display => $DISPLAY,
38 labels => \@LABELS};
39
40 sub methodinfo {
41 return (
42 tmx => $METHODHASH,
43 tsx => $METHODHASH,
44 canada => $METHODHASH,
45 );
46 }
47
48 sub labels {
49 my %m = methodinfo();
50 return map {$_ => [@{$m{$_}{labels}}] } keys %m;
51 }
52
53 sub methods {
54 my %m = methodinfo();
55 return map {$_ => $m{$_}{subroutine} } keys %m;
56 }
57
58 sub tmx {
59 my $quoter = shift;
60 my @symbols = @_;
61 my $ua = $quoter->user_agent();
62 my %info;
63
64 foreach my $symbol (@symbols) {
65 eval {
66 my $url = 'https://app-money.tmx.com/graphql';
67 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 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 my $request = HTTP::Request->new('POST', $url, $header, $body);
81 $request->header("referrer" => "https://money.tmx.com/",
82 "referrerPolicy" => "strict-origin-when-cross-origin",
83 "mode" => "cors");
84
85 my $reply = $ua->request($request);
86 if (! $reply->is_success) {
87 $info{$symbol, 'errormsg'} = 'Failed to connect with TMX website';
88 $info{$symbol, 'success'} = 0;
89 return;
90 }
91 ### Search : $url, $reply->code
92 ### reply : $reply->content
93
94 my $data = decode_json $reply->content;
95 if (exists $data->{errors}) {
96 $info{$symbol, 'errormsg'} = $data->{errors}[0]->{message};
97 $info{$symbol, 'success'} = 0;
98 return;
99 }
100
101 $data = $data->{data}->{getQuoteBySymbol};
102 if (lc($data->{symbol}) ne lc($symbol)) {
103 $info{$symbol, 'errormsg'} = "returned symbol was not correct for $symbol";
104 $info{$symbol, 'success'} = 0;
105 return
106 }
107
108 if ( $symbol =~ /:us/ix ) {
109 $info{$symbol, 'currency'} = 'USD'; }
110 else {$info{$symbol, 'currency'} = 'CAD'}
111
112 $info{$symbol, 'name'} = $data->{name};
113 $info{$symbol, 'exchange'} = $data->{exchangeName};
114 $info{$symbol, 'volume'} = $data->{volume};
115 $info{$symbol, 'open'} = $data->{openPrice};
116 $info{$symbol, 'high'} = $data->{dayHigh};
117 $info{$symbol, 'low'} = $data->{dayLow};
118 $info{$symbol, 'cap'} = $data->{MarketCap};
119 $info{$symbol, 'close'} = $data->{prevClose};
120 $info{$symbol, 'year_range'} = $data->{weeks52low} . ' - ' . $data->{weeks52high};
121 $info{$symbol, 'last'} = $data->{price};
122 $info{$symbol, 'symbol'} = $data->{symbol};
123 $info{$symbol, 'p_change'} = $data->{percentChange};
124 $quoter->store_date(\%info, $symbol, {today => 1});
125
126 $info{$symbol, 'success'} = 1;
127 };
128 if ($@) {
129 my $error = "TMX failed: $@";
130 $info{$symbol, 'success'} = 0;
131 $info{$symbol, 'errormsg'} = trim($error);
132 }
133 }
134
135 return wantarray() ? %info : \%info;
136 }
137
138 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