Quote/TwelveData.pm

filter: Statementstmt 40.6% 39 / 96 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 91.6% 11 / 12 Podpod 0.0% 0 / 5 totaltotal 31.8% 50 / 157 CC 21 SCAR 57.2
CC
21
Cov
29%
twelvedata
462.0
BEGIN
1.0
BEGIN
1.0
CRAP 303.5
« Quote/Stooq.pm ↑ Summary Quote/YahooJSON.pm »
1 #!/usr/bin/perl -w
2 # vi: set ts=2 sw=2 noai ic showmode showmatch:
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 # 02110-1301, USA
17  
18 package Finance::Quote::TwelveData;
19 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22 5 ▶use constant DEBUG => $ENV{DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 ▶use if DEBUG, 'Smart::Comments';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 5 ▶use JSON qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 5 ▶use Text::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 5 ▶use DateTime::Format::Strptime qw( strptime strftime );
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 5 our $VERSION = '1.72_01'; # TRIAL VERSION
31  
32 5 my $URL = Text::Template->new(TYPE => 'STRING', SOURCE => 'https://api.twelvedata.com/quote?symbol={$symbol}&apikey={$token}');
33 5 my $THROTTLE = 1.05 * 60.0/8.0; # 5% more than maximum seconds / request for free tier
34 5 my $AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36';
35  
36 5 our $DISPLAY = 'TwelveData';
37 5 our $FEATURES = {'API_KEY' => 'registered user API key'};
38 5 our @LABELS = qw/symbol name exchange currency isodate currency open high low close last/;
39 5 our $METHODHASH = {subroutine => \&twelvedata,
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 twelvedata => $METHODHASH,
47 usa => $METHODHASH,
48 nyse => $METHODHASH,
49 nasdaq => $METHODHASH,
50 );
51 }
52  
53 5 ▶sub methods {
Subroutine methods: called CC=1 SCAR=0.0
Pod undocumented
54 5 my %m = methodinfo(); 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(); return map {$_ => [@{$m{$_}{labels}}] } keys %m;
59 }
60  
61 1 ▶sub parameters {
Subroutine parameters: called CC=1 SCAR=0.0
Pod undocumented
62 1 return ('API_KEY');
63 }
64  
65  
66 0 ▶sub twelvedata {
Subroutine twelvedata: not called CC=21 SCAR=61.4
Pod undocumented
67 0 my $quoter = shift;
68  
69 0 my @stocks = @_;
70 0 my $quantity = @stocks;
71 0 my ( %info, $reply, $url, $code, $desc, $body, $mark );
72 0 my $ua = $quoter->user_agent();
73 0 my $agent = $ua->agent();
74 0 $ua->agent($AGENT);
75  
76 my $token = exists $quoter->{module_specific_data}->{twelvedata}->{API_KEY} ?
77 $quoter->{module_specific_data}->{twelvedata}->{API_KEY} :
78 0 ▶ $ENV{"TWELVEDATA_API_KEY"};
Branch: 1 branch
BranchTrueFalseTotal
exists $quoter->{'module_specific_data'}{'twelvedata'}{'API_KEY'} ? : 0 0 0
79  
80 0 foreach my $symbol (@stocks) {
81  
82 0 ▶ if ( !defined $token ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $token) 0 0 0
83 0 $info{ $symbol, 'success' } = 0;
84 0 $info{ $symbol, 'errormsg' } =
85 'TwelveData API_KEY not defined. Get an API key at https://twelvedata.com';
86 0 next;
87 }
88  
89 # Rate limit - first time through loop, mark is negative
90 0 $mark -= time();
91 ### TwelveData Mark: $mark
92 0 ▶ sleep($mark) if $mark > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $mark > 0 0 0 0
93 0 $mark = time() + $THROTTLE;
94  
95 0 $url = $URL->fill_in(HASH => {symbol => $symbol, token => $token});
96 0 $reply = $ua->request(GET $url);
97
98 ### url: $url
99 ### reply: $reply
100
101 0 $code = $reply->code;
102 0 $desc = HTTP::Status::status_message($code);
103 0 $body = $reply->content;
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 0 my $quote;
112 0 eval {$quote = JSON::decode_json $body};
113 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
114 0 $info{ $symbol, 'success' } = 0;
115 0 $info{ $symbol, 'errormsg' } = $@;
116 0 next;
117 }
118  
119 0 ▶ if (exists $quote->{'status'} and $quote->{'status'} eq 'error') {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $quote->{"status"} and $quote->{"status"} eq "error") 0 0 0
Condition0%0/3
exists $quote->{"status"} and $quote->{"status"} eq "error"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $quote->{"status"} and $quote->{"status"} eq "error"
exists $quote->{"status"} $quote->{"status"} eq "error"
Truth table0%0/3
A and B
A= exists $quote->{"status"}B= $quote->{"status"} eq "error"
ABresult
0X0
100
111
120 0 $info{ $symbol, 'success' } = 0;
121 0 $info{ $symbol, 'errormsg' } = $quote->{'message'};
122 0 next;
123 }
124  
125 0 ▶ if (not exists $quote->{'symbol'} or $quote->{'symbol'} ne $symbol) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $quote->{"symbol"} or $quote->{"symbol"} ne $symbol) 0 0 0
Condition0%0/3
not exists $quote->{"symbol"} or $quote->{"symbol"} ne $symbol
l!l&&r!l&&!r
000
MC/DC0%0/2
not exists $quote->{"symbol"} or $quote->{"symbol"} ne $symbol
not exists $quote->{"symbol"} $quote->{"symbol"} ne $symbol
Truth table0%0/3
not(A) or B
A= not exists $quote->{"symbol"}B= $quote->{"symbol"} ne $symbol
ABresult
1X1
011
000
126 0 $info{ $symbol, 'success' } = 0;
127 0 $info{ $symbol, 'errormsg' } = "TwevleData return and unexpected json result";
128 0 next;
129 }
130  
131 0 $info{ $symbol, 'success' } = 1;
132 0 $info{ $symbol, 'symbol' } = $symbol;
133 0 ▶ $info{ $symbol, 'name'} = $quote->{'name'} if $quote->{'name'};
Branch: 1 branch
BranchTrueFalseTotal
if $quote->{"name"} 0 0 0
134 0 ▶ $info{ $symbol, 'exchange'} = $quote->{'exchange'} if $quote->{'exchange'};
Branch: 1 branch
BranchTrueFalseTotal
if $quote->{"exchange"} 0 0 0
135 0 ▶ $info{ $symbol, 'currency'} = $quote->{'currency'} if $quote->{'currency'};
Branch: 1 branch
BranchTrueFalseTotal
if $quote->{"currency"} 0 0 0
136 0 ▶ $info{ $symbol, 'open' } = $quote->{'open'} if $quote->{'open'};
Branch: 1 branch
BranchTrueFalseTotal
if $quote->{"open"} 0 0 0
137 0 ▶ $info{ $symbol, 'high' } = $quote->{'high'} if $quote->{'high'};
Branch: 1 branch
BranchTrueFalseTotal
if $quote->{"high"} 0 0 0
138 0 ▶ $info{ $symbol, 'low' } = $quote->{'low'} if $quote->{'low'};
Branch: 1 branch
BranchTrueFalseTotal
if $quote->{"low"} 0 0 0
139 0 ▶ $info{ $symbol, 'close' } = $quote->{'close'} if $quote->{'close'};
Branch: 1 branch
BranchTrueFalseTotal
if $quote->{"close"} 0 0 0
140 0 ▶ $info{ $symbol, 'last' } = $quote->{'close'} if $quote->{'close'};
Branch: 1 branch
BranchTrueFalseTotal
if $quote->{"close"} 0 0 0
141 0 ▶ $info{ $symbol, 'volume' } = $quote->{'volume'} if $quote->{'volume'};
Branch: 1 branch
BranchTrueFalseTotal
if $quote->{"volume"} 0 0 0
142 0 $info{ $symbol, 'method' } = 'twelvedata';
143
144 0 my $time = strptime('%s', int($quote->{'timestamp'}));
145 0 my $isodate = strftime('%F', $time);
146 0 $quoter->store_date( \%info, $symbol, { isodate => $isodate } );
147 }
148  
149 0 $ua->agent($agent);
150  
151 0 ▶ return wantarray() ? %info : \%info;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
152 }
153 5 1;
154  
155 =head1 NAME
156  
157 Finance::Quote::TwelveData - Obtain quotes from https://twelvedata.com
158  
159 =head1 SYNOPSIS
160  
161 use Finance::Quote;
162
163 $q = Finance::Quote->new('TwelveData', twelvedata => {API_KEY => 'your-twelvedata-api-key'});
164  
165 %info = $q->fetch('twelvedata', 'IBM', 'AAPL');
166  
167 =head1 DESCRIPTION
168  
169 This module fetches information from https://twelvedata.com.
170  
171 This module is loaded by default on a Finance::Quote object. It's
172 also possible to load it explicitly by placing "TwelveData" in the argument
173 list to Finance::Quote->new().
174  
175 This module provides the "twelvedata" fetch method.
176  
177 =head1 API_KEY
178  
179 https://twelvedata.com requires users to register and obtain an API key, which
180 is a secret value written in hexidecimal.
181  
182 The API key may be set by either providing a module specific hash to
183 Finance::Quote->new as in the above example, or by setting the environment
184 variable TWELVEDATA_API_KEY.
185  
186 =head2 FREE KEY LIMITS
187  
188 The TwelveData free key limits usage to:
189  
190 =over
191  
192 =item 800 queries per day
193  
194 =item 8 queries per minute
195  
196 =back
197  
198 =head1 LABELS RETURNED
199  
200 The following labels may be returned by Finance::Quote::TwelveData :
201 symbol name exchange currency isodate currency open high low close last
202  
203 =cut
« Quote/Stooq.pm ↑ Summary Quote/YahooJSON.pm »