blib/lib/Finance/YahooProfile.pm

filter: Statementstmt 10.3% 14 / 135 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 50.0% 4 / 8 Podpod 50.0% 1 / 2 totaltotal 8.3% 19 / 229 CC 40 SCAR 60.4
CC
40
Cov
7%
profile
702.0
_expand
132.0
new
12.0
CRAP 420.9
↑ Summary
1 package Finance::YahooProfile;
2
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 1 $VERSION = '1.12';
7
8 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
12 0 my $type = shift;
13 0 ▶ my $class = ref $type || $type;
Condition0%0/3
ref $type || $type
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $type || $type
ref $type $type
Truth table0%0/3
A || B
A= ref $typeB= $type
ABresult
1X1
011
000
14 0 my $self = {};
15 0 bless $self, $class;
16 0 ▶ $self->_init(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
17 0 return $self;
18 }
19
20 0 ▶sub _init {
Subroutine _init: not called CC=3 SCAR=24.8
21 0 my ($self, %params) = @_;
22 0 for (keys %params) {
23 0 $self->{$_} = $params{$_};
24 0 ▶ $self->{'dollar_symbol'} = 1 unless defined $params{'dollar_symbol'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"dollar_symbol"} 0 0 0
25 }
26 }
27
28 0 ▶sub profile {
Subroutine profile: not called CC=26 SCAR=65.5
Pod undocumented
29 0 my ($self, %params) = @_;
30
31 0 my (@res, $ua);
32
33 0 $ua = LWP::UserAgent->new;
34 0 ▶ $ua->timeout($self->{'timeout'}) if defined $self->{'timeout'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"timeout"} 0 0 0
35 0 $ua->env_proxy();
36
37 0 ▶ if (not ref $params{'s'}) { $params{'s'} = [$params{'s'}]; }
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $params{"s"}) 0 0 0
38
39 0 my ($s, $firstletter, $url, $content, $res);
40 0 for $s ( @{$params{'s'}} ) {
41
42 0 $firstletter = substr($s, 0, 1);
43 0 $url = "http://biz.yahoo.com/p/$firstletter/$s.html";
44 0 $content = $ua->request(GET $url)->content();
45 0 $res = {}; ## to store the results
46 0 $res->{'requested_symbol'} = $s;
47
48 {
49 0 local $/;
50 0 $content =~ s/<[^>]*>//gs;
51 }
52
53 ## Prepare Content for extraction
54 0 $content =~ s/&nbsp;/ /g; ## insert spaces
55 0 $content =~ s/.*(Statistics at a Glance -- )//s; ## remove the beginning
56 0 $content =~ s/(See Profile).*//s; ## remove the end
57
58 ## Check for non-existent data
59 0 ▶ if ($content =~ /\nNo Such Data/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /\nNo Such Data/) 0 0 0
60 0 $res->{'success'} = 0;
61 0 push (@res, $res);
62 0 next;
63 }
64
65 ## Market
66 0 $content =~ s/(.+?)://s;
67 0 $res->{'market'} = $1;
68
69 ## Ticker Symbol
70 0 ▶ if ($content =~ s/(.+?)As of //s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ s/(.+?)As of //s) 0 0 0
71 0 $res->{'symbol'} = $1;
72 }
73
74 ## Last Updated
75 0 ▶ if ($content =~ s/(.+?)Price and Volume//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ s/(.+?)Price and Volume//s) 0 0 0
76 0 $res->{'last_updated'} = $1;
77 }
78
79 ## 52-Week Prices
80 0 ▶ if ($content =~ s/52-Week Lowon (.+?)\$(.+?) Recent Price(.+?) 52-Week Highon (.+?)\$(.+?)( )?Beta/Beta/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ s/52-Week Lowon (.+?)\$(.+?) Recent Price(.+?) 52-Week Highon (.+?)\$(.+?)( )?Beta/Beta/s) 0 0 0
81 0 $res->{'52_week_low_date'} = $1;
82 0 $res->{'52_week_low'} = $2;
83 0 $res->{'recent_price'} = $3;
84 0 $res->{'52_week_high_date'} = $4;
85 0 $res->{'52_week_high'} = $5;
86 }
87
88 ## Beta, and Volume Data
89 0 ▶ if ($content =~ s/Beta(.+?) Daily Volume \(3-month avg\)(.+?)Daily Volume \(10-day avg\)(.+?)Stock/Stock/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ s/Beta(.+?) Daily Volume \(3-month avg\)(.+?)Daily Volume \(10-day avg\)(.+?)Stock/Stock/s) 0 0 0
90 0 $res->{'beta'} = $1;
91 0 $res->{'daily_volume_3ma'} = $2;
92 0 $res->{'daily_volume_10da'} = $3;
93 }
94
95 ## 52-Week Price Change
96 0 ▶ if ($content =~ s/.+?52-Week Change(.+?)52-Week Changerelative to S&amp;P500(.+?)Share//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ s/.+?52-Week Change(.+?)52-Week Changerelative to S&amp;P500(.+?)Share//s) 0 0 0
97 0 $res->{'52_week_change'} = $1;
98 0 $res->{'52_week_change_sp500'} = $2;
99 }
100
101 ## Share related items
102 0 ▶ if ($content =~ s/.+?Market Capitalization(.+?)Shares Outstanding(.+?)Float(.+?)Dividends//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ s/.+?Market Capitalization(.+?)Shares Outstanding(.+?)Float(.+?)Dividends//s) 0 0 0
103 0 $res->{'market_capitalization'} = $1;
104 0 $res->{'shares_outstanding'} = $2;
105 0 $res->{'shares_float'} = $3;
106 }
107
108 0 ▶ if ($content =~ s/.*?Annual Dividend( \(indicated\))?(.+?)( Dividend Yield(.+?))?Last Split(.+?)Per//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ s/.*?Annual Dividend( \(indicated\))?(.+?)( Dividend Yield(.+?))?Last Split(.+?)Per//s) 0 0 0
109 0 $res->{'dividend'} = $2;
110 0 $res->{'dividend_yield'} = $4;
111
112 0 ▶ if ($5 ne 'none') {
Branch: 1 branch
BranchTrueFalseTotal
if ($5 ne "none") 0 0 0
113 0 ▶ if ($5 =~ m/factor (.+?) on (.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($5 =~ /factor (.+?) on (.+)/) 0 0 0
114 0 $res->{'last_split_factor'} = $1;
115 0 $res->{'last_split_date'} = $2;
116 }
117 }
118 }
119
120 ## Per-Share Data
121 0 ▶ if ($content =~ s/.+?Book Value \(mrq\*\)(.+?) Earnings \(ttm\)(.+?) (Earnings \(mrq\)(.+?) )?Sales \(ttm\)(.+?) Cash( \(mrq\*\)?)?(.+?) Valuation//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ s/.+?Book Value \(mrq\*\)(.+?) Earnings \(ttm\)(.+?) (Earnings \(mrq\)(.+?) )?Sales \(ttm\)(.+?) Cash( \(mrq\*\)?)?(.+?) Valuation//s) 0 0 0
122 0 $res->{'book_value'} = $1;
123 0 $res->{'earnings_ttm'} = $2;
124 0 $res->{'earnings_mrq'} = $4;
125 0 $res->{'sales_ttm'} = $5;
126 0 $res->{'cash'} = $7;
127 }
128
129 ## Valuation Ratios
130 0 ▶ if ($content =~ s!.+?Price/Book \(mrq\*\)(.+?) Price/Earnings( \(ttm\))?(.+?) Price/Sales \(ttm\)(.+?) Income!!s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ s[.+?Price/Book \(mrq\*\)(.+?) Price/Earnings( \(ttm\))?(.+?) Price/Sales \(ttm\)(.+?) Income][]s) 0 0 0
131 0 $res->{'price_book'} = $1;
132 0 $res->{'price_earnings'} = $3;
133 0 $res->{'price_sales'} = $4;
134 }
135
136 ## Income Statements
137 0 ▶ if ($content =~ s/.+?Sales \(ttm\)(.+?)EBITDA \(ttm\*\)(.+?)Income available to common \(ttm\)(.+?)Profitability//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ s/.+?Sales \(ttm\)(.+?)EBITDA \(ttm\*\)(.+?)Income available to common \(ttm\)(.+?)Profitability//s) 0 0 0
138 0 $res->{'sales'} = $1;
139 0 $res->{'ebitda'} = $2;
140 0 $res->{'income'} = $3;
141 }
142
143 ## Profitability
144 0 ▶ if ($content =~ s/.*?Profit Margin \(ttm\)(.+?)Operating Margin \(ttm\)(.+?)Fiscal//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ s/.*?Profit Margin \(ttm\)(.+?)Operating Margin \(ttm\)(.+?)Fiscal//s) 0 0 0
145 0 $res->{'profit_margin'} = $1;
146 0 $res->{'operating_margin'} = $2;
147 }
148
149 ## Fiscal Year
150 0 ▶ if ($content =~ s/.*?Fiscal Year Ends(.+?)Most recent quarter(\(fully\nupdated\))?(.+?)(Most recent quarter\(flash earnings\)(.+?))?Management//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ s/.*?Fiscal Year Ends(.+?)Most recent quarter(\(fully\nupdated\))?(.+?)(Most recent quarter\(flash earnings\)(.+?))?Management//s) 0 0 0
151 0 $res->{'fiscal_year_ends'} = $1;
152 0 $res->{'most_recent_quarter'} = $3;
153 0 $res->{'most_recent_quarter_fe'} = $5;
154 0 $res->{'fiscal_year_ends'} =~ s/\n|\r/ /s;
155
156 }
157
158 ## Management Effectiveness
159 0 ▶ if ($content =~ s/.*?Return on Assets \(ttm\)(.+?)Return on Equity \(ttm\)(.+?)Financial//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ s/.*?Return on Assets \(ttm\)(.+?)Return on Equity \(ttm\)(.+?)Financial//s) 0 0 0
160 0 $res->{'return_on_assets'} = $1;
161 0 $res->{'return_on_equity'} = $2;
162 }
163
164 ## Financial Strength
165 0 ▶ if ($content =~ s/.*?Current Ratio( \(mrq\*?\))?(.+?) Debt\/Equity( \(mrq\*?\))?(.+?) Total Cash( \(mrq\*?\))?(.+?)Short//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ s[.*?Current Ratio( \(mrq\*?\))?(.+?) Debt/Equity( \(mrq\*?\))?(.+?) Total Cash( \(mrq\*?\))?(.+?)Short][]s) 0 0 0
166 0 $res->{'current_ratio'} = $2;
167 0 $res->{'debt_equity'} = $4;
168 0 $res->{'total_cash'} = $6;
169 }
170
171 ## Short Interest
172 0 ▶ if ($content =~ s/.*?InterestAs\nof (.+?)Shares\nShort(.+?)Percent of Float(.+?)Shares Short\(Prior Month\)(.+?)Short Ratio(.+?) Daily Volume([\d\.KMB]+)//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ s/.*?InterestAs\nof (.+?)Shares\nShort(.+?)Percent of Float(.+?)Shares Short\(Prior Month\)(.+?)Short Ratio(.+?) Daily Volume([\d\.KMB]+)//s) 0 0 0
173 0 $res->{'short_interest_date'} = $1;
174 0 $res->{'short_interest'} = $2;
175 0 $res->{'short_percent'} = $3;
176 0 $res->{'short_previous_month'} = $4;
177 0 $res->{'short_ratio'} = $5;
178 0 $res->{'short_daily_volume'} = $6;
179 }
180
181 0 ▶ $self->_expand($res) if $self->{'expand_numbers'} || $self->{'expand_percent'} || $self->{'expand'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"expand_numbers"} or $self->{"expand_percent"} or $self->{"expand"} 0 0 0
Condition0%0/6
$self->{"expand_numbers"} or $self->{"expand_percent"} or $self->{"expand"}
l!l&&r!l&&!r
000
$self->{"expand_numbers"} or $self->{"expand_percent"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->{"expand_numbers"} or $self->{"expand_percent"} or $self->{"expand"}
$self->{"expand_numbers"} $self->{"expand_percent"} $self->{"expand"}
Truth table0%0/4
A or B or C
A= $self->{"expand_numbers"}B= $self->{"expand_percent"}C= $self->{"expand"}
ABCresult
1XX1
01X1
0011
0000
182 0 $res->{'success'} = 1;
183
184 0 push(@res, $res);
185 }
186
187 0 ▶ return wantarray ? @res : \@res;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
188
189 }
190
191 0 ▶sub _expand {
Subroutine _expand: not called CC=11 SCAR=48.8
192 0 my ($self, $res) = @_;
193 0 my (%factors, $key, $dollar, $factor, @number_keys, @percent_keys);
194
195 0 %factors = ( 'K' => 1_000,
196 'M' => 1_000_000,
197 'B' => 1_000_000_000,
198 'T' => 1_000_000_000_000 ## for when we have the first trillion dollar company.
199 );
200
201 0 @number_keys = qw(
202 daily_volume_10da
203 daily_volume_3ma
204 ebitda
205 income
206 market_capitalization
207 sales
208 shares_float
209 shares_outstanding
210 short_interest
211 short_daily_volume
212 short_previous_month
213 total_cash
214 );
215
216 0 @percent_keys = qw(
217 52_week_change
218 52_week_change_sp500
219 dividend_yield
220 operating_margin
221 profit_margin
222 return_on_assets
223 return_on_equity
224 short_percent
225 );
226
227 0 ▶ if ($self->{'expand_numbers'} || $self->{'expand'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"expand_numbers"} or $self->{"expand"}) 0 0 0
Condition0%0/3
$self->{"expand_numbers"} or $self->{"expand"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"expand_numbers"} or $self->{"expand"}
$self->{"expand_numbers"} $self->{"expand"}
Truth table0%0/3
A or B
A= $self->{"expand_numbers"}B= $self->{"expand"}
ABresult
1X1
011
000
228 0 for $key (@number_keys) {
229 0 ▶ $dollar = (($res->{$key} =~ s/^\$//) ? '$' : ''); ## save dollar sign'
Branch: 1 branch
BranchTrueFalseTotal
$res->{$key} =~ s/^\$// ? : 0 0 0
230
231 0 ▶ if ($res->{$key} =~ s/(K|M|B|T)$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{$key} =~ s/(K|M|B|T)$//) 0 0 0
232 0 $res->{$key} *= $factors{$1};
233 }
234
235 0 ▶ if ($self->{'dollar_symbol'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dollar_symbol"}) 0 0 0
236 0 $res->{$key} = $dollar . $res->{$key};
237 }
238 }
239 }
240
241 0 ▶ if ($self->{'expand_percent'} || $self->{'expand'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"expand_percent"} or $self->{"expand"}) 0 0 0
Condition0%0/3
$self->{"expand_percent"} or $self->{"expand"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"expand_percent"} or $self->{"expand"}
$self->{"expand_percent"} $self->{"expand"}
Truth table0%0/3
A or B
A= $self->{"expand_percent"}B= $self->{"expand"}
ABresult
1X1
011
000
242 0 for $key (@percent_keys) {
243 0 ▶ if ($res->{$key} =~ s/^([\+\-\d\.]+)%$/$1/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{$key} =~ s/^([\+\-\d\.]+)%$/$1/) 0 0 0
244 0 $res->{$key} /= 100;
245 }
246 }
247 }
248 }
249
250
251 1 1;
252
253 __END__
↑ Summary