QuoteHist/Yahoo.pm

filter: Statementstmt 93.1% 122 / 131 Branchbran 63.8% 23 / 36 Conditioncond 49.0% 25 / 51 MC/DCmcdc 6.9% 2 / 29 Subroutinesub 93.3% 14 / 15 Podpod 50.0% 2 / 4 totaltotal 70.6% 188 / 266 SCAR 41.7
CC
44
Cov
78%
url_maker
26.6
new
14.4
__ANON__
5.2
CRAP 64.7
« QuoteHist/Generic.pm ↑ Summary QuoteHist.pm »
1 package Finance::QuoteHist::Yahoo;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use vars qw(@ISA $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 $VERSION = "1.26";
8  
9 4 ▶use Finance::QuoteHist::Generic;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 @ISA = qw(Finance::QuoteHist::Generic);
11  
12 4 ▶use Date::Manip;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # curl 'https://query2.finance.yahoo.com/v8/finance/chart/TLSA?formatted=true&crumb=l92p7dftYe%2F&lang=en-US&region=US&includeAdjustedClose=true&interval=1d&period1=1455840000&period2=1613692800&events=div%7Csplit&useYfid=true&corsDomain=finance.yahoo.com' \
16 # -H 'authority: query2.finance.yahoo.com' \
17 # -H 'pragma: no-cache' \
18 # -H 'cache-control: no-cache' \
19 # -H 'user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36' \
20 # -H 'dnt: 1' \
21 # -H 'accept: */*' \
22 # -H 'origin: https://finance.yahoo.com' \
23 # -H 'sec-fetch-site: same-site' \
24 # -H 'sec-fetch-mode: cors' \
25 # -H 'sec-fetch-dest: empty' \
26 # -H 'referer: https://finance.yahoo.com/quote/TLSA/history?period1=1455840000&period2=1613692800&interval=1d&filter=history&frequency=1d&includeAdjustedClose=true' \
27 # -H 'accept-language: en-US,en;q=0.9' \
28 # -H 'cookie: B=a912p61g2vk8r&b=3&s=tt; GUC=AQEBAQFgMSJgOUIaCwP5; A1=d=AQABBBvRL2ACEAlRbREoQmTRqOBpeDBZhKQFEgEBAQEiMWA5YAAAAAAA_SMAAAcIG9EvYDBZhKQ&S=AQAAApHY37COo3qJCJvBwk9D-TA; A3=d=AQABBBvRL2ACEAlRbREoQmTRqOBpeDBZhKQFEgEBAQEiMWA5YAAAAAAA_SMAAAcIG9EvYDBZhKQ&S=AQAAApHY37COo3qJCJvBwk9D-TA; A1S=d=AQABBBvRL2ACEAlRbREoQmTRqOBpeDBZhKQFEgEBAQEiMWA5YAAAAAAA_SMAAAcIG9EvYDBZhKQ&S=AQAAApHY37COo3qJCJvBwk9D-TA&j=US; cmp=t=1613746462&j=0; PRF=t%3DTLSA%252BIBM%252B%255EYHQ' \
29 # -H 'sec-gpc: 1' \
30 # --compressed
31  
32  
33 # https://query1.finance.yahoo.com/v7/finance/download/IBM?period1=1495391410&period2=1498069810&interval=1d&events=history&crumb=bB6k340lPXt
34 # https://query1.finance.yahoo.com/v7/finance/download/IBM?period1=993096000&period2=1498017600&interval=1wk&events=history&crumb=bB6k340lPXt
35 # https://query1.finance.yahoo.com/v7/finance/download/IBM?period1=993096000&period2=1498017600&interval=1mo&events=history&crumb=bB6k340lPXt
36 #
37 # Dividends:
38 # https://query1.finance.yahoo.com/v7/finance/download/IBM?period1=993096000&period2=1498017600&interval=1d&events=div&crumb=bB6k340lPXt
39 #
40 # Splits:
41 # https://query1.finance.yahoo.com/v7/finance/download/NKE?period1=993096000&period2=1498017600&interval=1d&events=split&crumb=bB6k340lPXt
42  
43 sub new {
44 5 ▶ my $that = shift;
Subroutine new: called CC=9 SCAR=26.7
Pod documented
45 5 ▶ my $class = ref($that) || $that;
Condition33%1/3
ref $that || $that
l!l&&r!l&&!r
050
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table33%1/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
46 5 my %parms = @_;
47  
48 5 $parms{parse_mode} = 'json';
49 5 ▶ $parms{ua_params} ||= {};
Condition50%1/2
$parms{"ua_params"} ||= {}
l!l
05
MC/DC0%0/1
$parms{"ua_params"} ||= {}
$parms{"ua_params"}
Truth table50%1/2
A
A= $parms{"ua_params"}
Aresult
11
00
50 5 ▶ $parms{ua_params}{cookie_jar} ||= {};
Condition50%1/2
$parms{"ua_params"}{"cookie_jar"} ||= {}
l!l
05
MC/DC0%0/1
$parms{"ua_params"}{"cookie_jar"} ||= {}
$parms{"ua_params"}{"cookie_jar"}
Truth table50%1/2
A
A= $parms{"ua_params"}{"cookie_jar"}
Aresult
11
00
51  
52 5 my $self = __PACKAGE__->SUPER::new(%parms);
53 5 bless $self, $class;
54  
55 # set initial cookie (the cookie crumbs are hashed out of this)
56 # https://finance.yahoo.com/quote/IBM/history
57 5 my $ticker = $parms{symbols};
58 5 ▶ $ticker = $ticker->[0] if ref $ticker eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $ticker eq "ARRAY" 0 5 5
59 5 my $html = $self->fetch("https://finance.yahoo.com/quote/$ticker/history");
60  
61 # extract the cookie crumb
62 5 my %crumbs;
63 5 for my $c ($html =~ /"crumb"\s*:\s*"([^"]+)"/g) {
64 0 ▶ next if $c =~ /[{}]/;
Branch: 1 branch
BranchTrueFalseTotal
if $c =~ /[{}]/ 0 0 0
65 0 $c =~ s/\\u002F/\//;
66 0 ++$crumbs{$c};
67 }
68 5 my $crumb = '';
69 5 my $max = 0;
70 5 for my $c (keys %crumbs) {
71 0 ▶ if ($crumbs{$c} >= $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($crumbs{$c} >= $max) 0 0 0
72 0 $crumb = $c;
73 0 $max = $crumbs{$c};
74 }
75 }
76  
77 5 $self->{crumb} = $crumb;
78  
79 5 $self;
80 }
81  
82 0 ▶sub granularities { qw( daily weekly monthly ) }
Subroutine granularities: not called CC=1 SCAR=6.9
Pod undocumented
83  
84 sub url_maker {
85 10 ▶ my($self, %parms) = @_;
Subroutine url_maker: called CC=21 SCAR=32.8
Pod documented
86 10 ▶ my $target_mode = $parms{target_mode} || $self->target_mode;
Condition33%1/3
$parms{'target_mode'} || $self->target_mode
l!l&&r!l&&!r
1000
MC/DC0%0/2
$parms{'target_mode'} || $self->target_mode
$parms{'target_mode'} $self->target_mode
Truth table33%1/3
A || B
A= $parms{'target_mode'}B= $self->target_mode
ABresult
1X1
011
000
87 10 ▶ my $parse_mode = $parms{parse_mode} || $self->parse_mode;
Condition33%1/3
$parms{'parse_mode'} || $self->parse_mode
l!l&&r!l&&!r
1000
MC/DC0%0/2
$parms{'parse_mode'} || $self->parse_mode
$parms{'parse_mode'} $self->parse_mode
Truth table33%1/3
A || B
A= $parms{'parse_mode'}B= $self->parse_mode
ABresult
1X1
011
000
88  
89 # *always* block unknown target mode and parse mode combinations for
90 # cascade to work properly!
91 10 ▶ return undef unless $target_mode eq 'quote' ||
Branch: 1 branch
BranchTrueFalseTotal
unless $target_mode eq "quote" or $target_mode eq "split" or $target_mode eq "dividend" 0 10 10
Condition83%5/6
$target_mode eq "quote" or $target_mode eq "split" or $target_mode eq "dividend"
l!l&&r!l&&!r
820
$target_mode eq "quote" or $target_mode eq "split"
l!l&&r!l&&!r
622
MC/DC0%0/3
$target_mode eq "quote" or $target_mode eq "split" or $target_mode eq "dividend"
$target_mode eq "quote" $target_mode eq "split" $target_mode eq "dividend"
Truth table75%3/4
A or B or C
A= $target_mode eq "quote"B= $target_mode eq "split"C= $target_mode eq "dividend"
ABCresult
1XX1
01X1
0011
0000
92 $target_mode eq 'split' ||
93 $target_mode eq 'dividend';
94  
95 10 $parse_mode = "json";
96  
97 10 ▶ my $granularity = lc($parms{granularity} || $self->granularity);
Condition33%1/3
$parms{'granularity'} || $self->granularity
l!l&&r!l&&!r
0100
MC/DC0%0/2
$parms{'granularity'} || $self->granularity
$parms{'granularity'} $self->granularity
Truth table33%1/3
A || B
A= $parms{'granularity'}B= $self->granularity
ABresult
1X1
011
000
98 10 my $grain = 'd';
99 10 $granularity =~ /^\s*(\w)/;
100 10 ▶ $grain = $1 if $1 eq 'w' || $1 eq 'm';
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq "w" or $1 eq "m" 4 6 10
Condition100%3/3
$1 eq "w" or $1 eq "m"
l!l&&r!l&&!r
226
MC/DC100%2/2
$1 eq "w" or $1 eq "m"
$1 eq "w" $1 eq "m"
Truth table100%3/3
A or B
A= $1 eq "w"B= $1 eq "m"
ABresult
1X1
011
000
101 my($ticker, $start_date, $end_date) =
102 10 @parms{qw(symbol start_date end_date)};
103 10 ▶ $start_date ||= $self->start_date;
Condition33%1/3
$start_date ||= $self->start_date
l!l&&r!l&&!r
0100
MC/DC0%0/2
$start_date ||= $self->start_date
$start_date $self->start_date
Truth table33%1/3
A ||= B
A= $start_dateB= $self->start_date
ABresult
1X1
011
000
104 10 ▶ $end_date ||= $self->end_date;
Condition33%1/3
$end_date ||= $self->end_date
l!l&&r!l&&!r
0100
MC/DC0%0/2
$end_date ||= $self->end_date
$end_date $self->end_date
Truth table33%1/3
A ||= B
A= $end_dateB= $self->end_date
ABresult
1X1
011
000
105 10 ▶ if ($start_date && $end_date && $start_date gt $end_date) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start_date and $end_date and $start_date gt $end_date) 0 10 10
Condition33%2/6
$start_date and $end_date and $start_date gt $end_date
!ll&&!rl&&r
0100
$start_date and $end_date
!ll&&!rl&&r
0010
MC/DC0%0/3
$start_date and $end_date and $start_date gt $end_date
$start_date $end_date $start_date gt $end_date
Truth table25%1/4
A and B and C
A= $start_dateB= $end_dateC= $start_date gt $end_date
ABCresult
0XX0
10X0
1100
1111
106 0 ($start_date, $end_date) = ($end_date, $start_date);
107 }
108  
109 #my $host = "query1.finance.yahoo.com";
110 #my $base_url = "https://$host/v7/finance/download/$ticker?";
111
112 # https://query2.finance.yahoo.com/v8/finance/chart/TLSA?
113 # formatted=true
114 # crumb=l92p7dftYe%2F
115 # lang=en-US
116 # region=US
117 # includeAdjustedClose=true
118 # interval=1d
119 # period1=1455840000
120 # period2=1613692800
121 # events=div%7Csplit
122 # useYfid=true
123 # corsDomain=finance.yahoo.com
124  
125 10 my $host = "query2.finance.yahoo.com";
126 10 my $base_url = "https://$host/v8/finance/chart/$ticker?";
127 10 my @base_parms;
128 10 ▶ if ($start_date) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start_date) 10 0 10
129 10 my($y, $m, $d) = $self->ymd($start_date);
130 10 my $ts = Date_SecsSince1970($m, $d, $y, 0, 0, 0);
131 10 push(@base_parms, "period1=$ts");
132 }
133 10 ▶ if ($end_date) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end_date) 10 0 10
134 10 my($y, $m, $d) = $self->ymd($end_date);
135 10 my $ts = Date_SecsSince1970($m, $d, $y, 0, 0, 0);
136 10 $ts += 24*60*60;
137 10 push(@base_parms, "period2=$ts");
138 }
139  
140 10 my $interval = "1d";
141 10 ▶ if ($grain eq 'w') {
Branch: 2 branches
BranchTrueFalseTotal
if ($grain eq 'w') { } 2 8 10
elsif ($grain eq 'm') { } 2 6 8
142 2 $interval = "1wk";
143 }
144 elsif ($grain eq 'm') {
145 2 $interval = "1mo";
146 }
147 10 push(@base_parms, "interval=$interval");
148  
149 10 ▶ if ($target_mode eq "quote") {
Branch: 3 branches
BranchTrueFalseTotal
if ($target_mode eq 'quote') { } 6 4 10
elsif ($target_mode eq 'dividend') { } 2 2 4
elsif ($target_mode eq 'split') { } 2 0 2
150 6 push(@base_parms, "events=history");
151 6 push(@base_parms, "includeAdjustedClose=true")
152 }
153 elsif ($target_mode eq "dividend") {
154 2 push(@base_parms, "events=div");
155 }
156 elsif ($target_mode eq "split") {
157 2 push(@base_parms, "events=split");
158 }
159  
160 10 push(@base_parms, "crumb=" . $self->{crumb});
161  
162 10 my @urls = $base_url . join('&', @base_parms);
163 10 ▶ return sub { pop @urls };
Subroutine __ANON__: called CC=1 SCAR=0.0
164 }
165  
166 sub json_parser {
167 5 ▶ my $self = shift;
Subroutine json_parser: called CC=1 SCAR=0.0
Pod undocumented
168 5 my $target_mode = $self->target_mode();
169 my $json_quote_parse = sub {
170 3 ▶ my $data_result = shift;
Subroutine __ANON__: called CC=5 SCAR=16.2
171 3 ▶ my $data_indicators = $data_result->{indicators} || {};
Condition50%1/2
$data_result->{'indicators'} || {}
l!l
30
MC/DC0%0/1
$data_result->{'indicators'} || {}
$data_result->{'indicators'}
Truth table50%1/2
A
A= $data_result->{'indicators'}
Aresult
11
00
172 3 ▶ my $data_quote = ($data_indicators->{quote} || [])->[0];
Condition50%1/2
$data_indicators->{'quote'} || []
l!l
30
MC/DC0%0/1
$data_indicators->{'quote'} || []
$data_indicators->{'quote'}
Truth table50%1/2
A
A= $data_indicators->{'quote'}
Aresult
11
00
173 3 my @rows = [];
174 3 ▶ if ($data_quote) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_quote) 3 0 3
175 3 my $data_high = $data_quote->{high};
176 3 my $data_close = $data_quote->{close};
177 3 my $data_open = $data_quote->{open};
178 3 my $data_low = $data_quote->{low};
179 3 my $data_volume = $data_quote->{volume};
180 3 my $data_adj_close = $data_indicators->{adjclose}[0]{adjclose};
181 3 my $data_timestamp = $data_result->{timestamp};
182 3 for my $i (0 .. $#{$data_timestamp}) {
183 317 push(@rows, [
184 $data_timestamp->[$i],
185 $data_open->[$i],
186 $data_high->[$i],
187 $data_low->[$i],
188 $data_close->[$i],
189 $data_volume->[$i],
190 ]);
191 }
192 }
193 3 \@rows;
194 5 };
195 my $json_split_parse = sub {
196 1 ▶ my $data_result = shift;
Subroutine __ANON__: called CC=5 SCAR=16.6
197 1 ▶ my $data_events = $data_result->{events} || {};
Condition50%1/2
$data_result->{'events'} || {}
l!l
10
MC/DC0%0/1
$data_result->{'events'} || {}
$data_result->{'events'}
Truth table50%1/2
A
A= $data_result->{'events'}
Aresult
11
00
198 1 ▶ my $data_splits = $data_events->{splits} || {};
Condition50%1/2
$data_events->{'splits'} || {}
l!l
10
MC/DC0%0/1
$data_events->{'splits'} || {}
$data_events->{'splits'}
Truth table50%1/2
A
A= $data_events->{'splits'}
Aresult
11
00
199 1 my @rows;
200 1 ▶ if ($data_splits) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_splits) 1 0 1
201 1 for my $rec (sort values %$data_splits) {
202 push(@rows, [
203 $rec->{date},
204 $rec->{numerator},
205 $rec->{denominator},
206 1 ]);
207 }
208 }
209 1 \@rows;
210 5 };
211 my $json_div_parse = sub {
212 # "date" "amount"
213 1 ▶ my $data_result = shift;
Subroutine __ANON__: called CC=4 SCAR=14.1
214 1 ▶ my $data_events = $data_result->{events} || {};
Condition50%1/2
$data_result->{'events'} || {}
l!l
10
MC/DC0%0/1
$data_result->{'events'} || {}
$data_result->{'events'}
Truth table50%1/2
A
A= $data_result->{'events'}
Aresult
11
00
215 1 ▶ my $data_dividends = $data_events->{dividends} || {};
Condition50%1/2
$data_events->{'dividends'} || {}
l!l
10
MC/DC0%0/1
$data_events->{'dividends'} || {}
$data_events->{'dividends'}
Truth table50%1/2
A
A= $data_events->{'dividends'}
Aresult
11
00
216 1 my @rows;
217 1 for my $rec (sort values %$data_dividends) {
218 push(@rows, [
219 $rec->{date},
220 $rec->{amount},
221 11 ]);
222 }
223 1 \@rows;
224 5 };
225 sub {
226 5 ▶ my $data = shift;
Subroutine __ANON__: called CC=5 SCAR=16.4
227 5 $data = decode_json($data);
228 5 ▶ my $data_result = $data->{chart}{result}[0] || {};
Condition50%1/2
$data->{'chart'}{'result'}[0] || {}
l!l
50
MC/DC0%0/1
$data->{'chart'}{'result'}[0] || {}
$data->{'chart'}{'result'}[0]
Truth table50%1/2
A
A= $data->{'chart'}{'result'}[0]
Aresult
11
00
229 5 ▶ if ($target_mode eq "quote") {
Branch: 3 branches
BranchTrueFalseTotal
if ($target_mode eq 'quote') { } 3 2 5
elsif ($target_mode eq 'split') { } 1 1 2
elsif ($target_mode eq 'dividend') { } 1 0 1
230 3 return $json_quote_parse->($data_result);
231 }
232 elsif ($target_mode eq "split") {
233 1 return $json_split_parse->($data_result);
234 }
235 elsif ($target_mode eq "dividend") {
236 1 return $json_div_parse->($data_result);
237 }
238 0 else { die "unknown mode: $target_mode" }
239 5 };
240 }
241  
242 4 1;
243  
244 __END__
« QuoteHist/Generic.pm ↑ Summary QuoteHist.pm »