QuoteHist/BusinessWeek.pm

filter: Statementstmt 19.2% 27 / 140 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 31.8% 7 / 22 Podpod 33.3% 2 / 6 totaltotal 14.7% 36 / 244 CC 32 SCAR 45.8
CC
32
Cov
14%
__ANON__
182.0
url_maker
182.0
_array_unpack
56.0
CRAP 97.5
« QuoteHist/Generic.pm ↑ Summary QuoteHist/Google.pm »
1 package Finance::QuoteHist::BusinessWeek;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw(@ISA $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 $VERSION = '1.02';
8  
9 1 ▶use constant DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Finance::QuoteHist::Generic;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 @ISA = qw(Finance::QuoteHist::Generic);
13  
14 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Date::Manip;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 Date::Manip::Date_Init("TZ=GMT");
17  
18 # Example URL:
19 #
20 # /businessweek/research/common/charts/update_historical_chart.asp?duration=3653&frequency=1day&freq=undefined&scaling=linear&display=mountain&exportData=1&dMin=37431&dMax=39257&uppers=[]&lowers=[]&lastColorUsed=2&action=&action_value= H
21  
22 1 my %Label_Map = (
23 d => 'date',
24 h => 'high',
25 c => 'close',
26 v => 'volume',
27 l => 'low',
28 o => 'open',
29 );
30 1 my @Sorted = qw(date open high low close volume);
31  
32 sub new {
33 0 ▶ my $that = shift;
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
34 0 ▶ my $class = ref($that) || $that;
Condition0%0/3
ref $that || $that
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table0%0/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
35 0 my %parms = @_;
36 0 my $self = __PACKAGE__->SUPER::new(%parms);
37 0 bless $self, $class;
38 0 $self->parse_mode('businessweek_javascript');
39 0 $self->labels(
40 target_mode => 'quote',
41 labels => [ qw(d h c v l o) ],
42 );
43 0 $self;
44 }
45  
46 sub businessweek_javascript_parser {
47 0 ▶ my $self = shift;
Subroutine businessweek_javascript_parser: not called CC=1 SCAR=6.9
Pod undocumented
48 sub {
49 0 ▶ my $data = shift;
Subroutine __ANON__: not called CC=13 SCAR=52.0
50 0 ▶ return [] unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
51  
52 0 my %columns;
53 0 my @pats = sort keys %Label_Map;
54 0 foreach my $line (grep(/arrayDecodeUnpack/i, split(/\s*;\s*/, $data))) {
55 0 ▶ next unless $line =~ /(\w+)=arrayDecodeUnpack\s*\(\"([^\"]+)\"\s*\)/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /(\w+)=arrayDecodeUnpack\s*\(\"([^\"]+)\"\s*\)/i 0 0 0
56 0 my($varname, $value) = ($1, $2);
57 0 my $label;
58 0 foreach my $pi (0 .. $#pats) {
59 0 my $pat = $pats[$pi];
60 0 ▶ if ($varname =~ /^($pat)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($varname =~ /^($pat)/i) 0 0 0
61 0 $label = $Label_Map{$pat};
62 0 splice(@pats, $pi, 1);
63 0 last;
64 }
65 }
66 0 ▶ if (! $label) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($label) 0 0 0
67 0 print STDERR "skipping unknown label $varname\n" if DEBUG;
68 0 next;
69 }
70 0 ▶ if ($columns{$label}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($columns{$label}) 0 0 0
71 0 warn "oops, seen $label, skipping\n" if DEBUG;
72 0 next;
73 }
74 0 $columns{$label} = $value;
75 }
76 0 my $count = 0;
77 0 foreach my $label (keys %columns) {
78 0 my $decoded = _decode_unpack($columns{$label});
79 0 print STDERR "$label : ", scalar @$decoded, " items\n" if DEBUG;
80 0 ▶ $count = @$decoded if @$decoded > $count;
Branch: 1 branch
BranchTrueFalseTotal
if @$decoded > $count 0 0 0
81 0 $columns{$label} = $decoded;
82 }
83 0 my @rows;
84 0 foreach my $i (0 .. $count-1) {
85 0 my @row;
86 0 foreach my $label (@Sorted) {
87 0 my $value = $columns{$label}[$i];
88 0 ▶ $value = $self->excel_daynum_to_date($value) if $label eq 'date';
Branch: 1 branch
BranchTrueFalseTotal
if $label eq "date" 0 0 0
89 0 push(@row, $value);
90 }
91 0 push(@rows, \@row);
92 }
93 0 \@rows;
94 0 };
95 }
96  
97 0 ▶sub granularities { qw( daily weekly monthly ) }
Subroutine granularities: not called CC=1 SCAR=6.9
Pod undocumented
98  
99 sub url_maker {
100 0 ▶ my($self, %parms) = @_;
Subroutine url_maker: not called CC=13 SCAR=52.0
Pod documented
101 0 ▶ my $target_mode = $parms{target_mode} || $self->target_mode;
Condition0%0/3
$parms{'target_mode'} || $self->target_mode
l!l&&r!l&&!r
000
MC/DC0%0/2
$parms{'target_mode'} || $self->target_mode
$parms{'target_mode'} $self->target_mode
Truth table0%0/3
A || B
A= $parms{'target_mode'}B= $self->target_mode
ABresult
1X1
011
000
102 0 ▶ my $parse_mode = $parms{parse_mode} || $self->parse_mode;
Condition0%0/3
$parms{'parse_mode'} || $self->parse_mode
l!l&&r!l&&!r
000
MC/DC0%0/2
$parms{'parse_mode'} || $self->parse_mode
$parms{'parse_mode'} $self->parse_mode
Truth table0%0/3
A || B
A= $parms{'parse_mode'}B= $self->parse_mode
ABresult
1X1
011
000
103  
104 # *always* block uknown target mode and parse mode combinations in
105 # order for cascade to work!
106 return undef unless
107 0 ▶ ($target_mode eq 'quote') && $parse_mode eq 'businessweek_javascript';
Branch: 1 branch
BranchTrueFalseTotal
unless $target_mode eq "quote" and $parse_mode eq "businessweek_javascript" 0 0 0
Condition0%0/3
$target_mode eq "quote" and $parse_mode eq "businessweek_javascript"
!ll&&!rl&&r
000
MC/DC0%0/2
$target_mode eq "quote" and $parse_mode eq "businessweek_javascript"
$target_mode eq "quote" $parse_mode eq "businessweek_javascript"
Truth table0%0/3
A and B
A= $target_mode eq "quote"B= $parse_mode eq "businessweek_javascript"
ABresult
0X0
100
111
108  
109 0 ▶ my $granularity = lc($parms{granularity} || $self->granularity);
Condition0%0/3
$parms{'granularity'} || $self->granularity
l!l&&r!l&&!r
000
MC/DC0%0/2
$parms{'granularity'} || $self->granularity
$parms{'granularity'} $self->granularity
Truth table0%0/3
A || B
A= $parms{'granularity'}B= $self->granularity
ABresult
1X1
011
000
110 0 my $grain;
111 0 my ($g) = $granularity =~ /^\s*(\w)/;
112 0 ▶ if ($g eq 'd') { $grain = '1day' } # daily
Branch: 3 branches
BranchTrueFalseTotal
if ($g eq 'd') { } 0 0 0
elsif ($g eq 'w') { } 0 0 0
elsif ($g eq 'm') { } 0 0 0
113 0 elsif ($g eq 'w') { $grain = '1week' } # weekly
114 0 elsif ($g eq 'm') { $grain = '1month' } # monthly
115 0 else { croak "Unknown granularity '$granularity'\n" }
116  
117 my($ticker, $start_date, $end_date) =
118 0 @parms{qw(symbol start_date end_date)};
119 0 ▶ $start_date ||= $self->start_date;
Condition0%0/3
$start_date ||= $self->start_date
l!l&&r!l&&!r
000
MC/DC0%0/2
$start_date ||= $self->start_date
$start_date $self->start_date
Truth table0%0/3
A ||= B
A= $start_dateB= $self->start_date
ABresult
1X1
011
000
120 0 ▶ $end_date ||= $self->end_date;
Condition0%0/3
$end_date ||= $self->end_date
l!l&&r!l&&!r
000
MC/DC0%0/2
$end_date ||= $self->end_date
$end_date $self->end_date
Truth table0%0/3
A ||= B
A= $end_dateB= $self->end_date
ABresult
1X1
011
000
121  
122 # businessweek has a one-off error on the start date when using weekly
123 # or monthly mode
124 0 ▶ if ($self->granularity =~ /^(m|w)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->granularity =~ /^(m|w)/i) 0 0 0
125 0 ▶ if ($1 eq 'w') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq 'w') { } 0 0 0
126 0 $start_date = DateCalc($start_date, '- 1 week');
127 }
128 else {
129 0 $start_date = DateCalc($start_date, '- 1 month');
130 }
131 }
132  
133 0 my $host = 'investing.businessweek.com';
134 0 my $path = 'businessweek/research/common/charts/update_historical_chart.asp';
135  
136 # this also worked
137 # my $path = '/research/stocks/snapshot/historical.asp';
138  
139 0 my %query = (
140 symbol => $ticker,
141 freq => $grain,
142 dMin => $self->date_to_excel_daynum($start_date),
143 dMax => $self->date_to_excel_daynum($end_date),
144 # dunno what the rest of these do
145 duration => 1096,
146 frequency => '1day', # relic?
147 scaling => 'linear',
148 display => 'mountain',
149 exportData => '1',
150 uppers => '[]',
151 lowers => '[]',
152 lastColorUsed => 2,
153 action => '',
154 action_value => '',
155 );
156  
157  
158 my $url = "http://$host/$path?" .
159 0 join('&', map { "$_=$query{$_}" } sort keys %query);
160  
161 0 print STDERR "URL: $url\n" if DEBUG;
162  
163 0 my @urls = ($url);
164 0 ▶ sub { pop @urls };
Subroutine __ANON__: not called CC=1 SCAR=6.9
165 }
166  
167 ### subroutines to deal with businessweek's wonky data encoding
168  
169 0 ▶sub _decode_unpack { _array_unpack(MIME::Base64::decode_base64(shift)) }
Subroutine _decode_unpack: not called CC=1 SCAR=6.9
170  
171 sub _array_unpack {
172 # inspired by this (who comes up with this stuff?):
173 # http://investing.businessweek.com/includes/asplib/ArrayUnpack.js
174 0 ▶ my $str = shift;
Subroutine _array_unpack: not called CC=7 SCAR=40.3
175 0 my @e;
176 0 my @code = map { ord($_) } split(//, $str);
177 0 my $m = $code[1] | ($code[2] << 8);
178 0 ▶ if ($m > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m > 0) 0 0 0
179 0 my $b = $code[3];
180 0 my $f = 10 ** $code[4];
181 0 my $k = $code[5] | ($code[6] << 8) | ($code[7] << 16) | ($code[8] << 24);
182 0 my $l = (2 ** $b) - 1;
183 0 push(@e, $k/$f);
184 0 my($g, $h) = (0, 0);
185 0 ▶ for (my $i = 9; $i < @code; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @code) 0 0 0
186 0 $g |= $code[$i] << $h;
187 0 $h += 8;
188 0 ▶ while ($h >= $b && @e <= $m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h >= $b and @e <= $m) 0 0 0
Condition0%0/3
$h >= $b and @e <= $m
!ll&&!rl&&r
000
MC/DC0%0/2
$h >= $b and @e <= $m
$h >= $b @e <= $m
Truth table0%0/3
A and B
A= $h >= $bB= @e <= $m
ABresult
0X0
100
111
189 0 my $j = ($g & $l) >> 1;
190 0 ▶ $k += ($g & 1) ? -1 * $j : $j;
Branch: 1 branch
BranchTrueFalseTotal
$g & 1 ? : 0 0 0
191 0 push(@e, $k/$f);
192 0 $g >>= $b;
193 0 $h -= $b;
194 }
195 0 $g |= $code[$i] >> (8 - $h);
196 }
197 }
198 0 ▶ wantarray ? @e : \@e;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
199 }
200  
201 ### excel daynum conversions
202  
203 # using locale TZ
204 #sub excel_daynum_to_date {
205 # my $self = shift;
206 # my $excel_daynum = shift;
207 # my $skew = _excel_daynum_sub_skew($excel_daynum);
208 # my $date = _dm_from_epoch(_days_to_secs($skew));
209 # my $tzod = _tz_offset_in_days($date);
210 # _dm_from_epoch(_days_to_secs($skew - $tzod));
211 #}
212  
213 # using GMT
214 sub excel_daynum_to_date {
215 0 ▶ my $self = shift;
Subroutine excel_daynum_to_date: not called CC=1 SCAR=6.9
Pod undocumented
216 0 my $excel_daynum = shift;
217 0 my $skew = _excel_daynum_sub_skew($excel_daynum);
218 0 my $date = _dm_from_epoch(_days_to_secs($skew));
219 0 _dm_from_epoch(_days_to_secs($skew));
220 }
221  
222 # using locale TZ
223 #sub date_to_excel_daynum {
224 # my $self = shift;
225 # my $date = shift;
226 # _secs_to_days(Date::Manip::UnixDate($date, "%s")) +
227 # (_excel_daynum_add_skew(_tz_offset_in_days($date)));
228 #}
229  
230 # using GMT
231 sub date_to_excel_daynum {
232 0 ▶ my $self = shift;
Subroutine date_to_excel_daynum: not called CC=1 SCAR=6.9
Pod undocumented
233 0 my $date = shift;
234 0 _excel_daynum_add_skew(_secs_to_days(Date::Manip::UnixDate($date, "%s")));
235 }
236  
237 # subroutines
238  
239 sub _dm_from_epoch {
240 0 ▶ my $epoch = shift;
Subroutine _dm_from_epoch: not called CC=1 SCAR=6.9
241 0 Date::Manip::ParseDateString("epoch $epoch");
242 }
243  
244 # using locale TZ
245 #sub _tz_offset_in_minutes {
246 # my $date = shift;
247 # my $tz_offset = Date::Manip::UnixDate($date, "%z");
248 # my($sign, $hours, $minutes) = $tz_offset =~ /^(\D?)(\d\d)(\d\d)/;
249 # $tz_offset = ($hours*60 + $minutes);
250 # $tz_offset *= -1 if $sign eq '-';
251 # $tz_offset;
252 #}
253  
254 # using locale TZ
255 #sub _tz_offset_in_days { _tz_offset_in_minutes(@_) / (60 * 24) }
256  
257 0 ▶sub _excel_daynum_sub_skew { shift() - 25569 }
Subroutine _excel_daynum_sub_skew: not called CC=1 SCAR=6.9
258 0 ▶sub _excel_daynum_add_skew { shift() + 25569 }
Subroutine _excel_daynum_add_skew: not called CC=1 SCAR=6.9
259  
260 0 ▶sub _days_to_secs { shift() * 86400 }
Subroutine _days_to_secs: not called CC=1 SCAR=6.9
261 0 ▶sub _secs_to_days { shift() / 86400 }
Subroutine _secs_to_days: not called CC=1 SCAR=6.9
262  
263 1 1;
264  
265 __END__
« QuoteHist/Generic.pm ↑ Summary QuoteHist/Google.pm »