blib/lib/Finance/YahooQuote.pm

filter: Statementstmt 90.6% 68 / 75 Branchbran 55.5% 10 / 18 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 87.5% 14 / 16 Podpod 0.0% 0 / 7 totaltotal 71.3% 92 / 129 CC 14 SCAR 20.1
CC
14
Cov
76%
get_basic_credentials
30.0
readYahoo
6.1
getonequote
3.3
CRAP 7.5
↑ Summary
1 # perl -w
2 #
3 # Copyright (C) 1998-2002, Dj Padzensky <djpadz@padz.net>
4 # Copyright (C) 2002-2010 Dirk Eddelbuettel <edd@debian.org>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #
20 # $Id: YahooQuote.pm,v 1.11 2010/03/27 00:44:10 edd Exp $
21  
22 package Finance::YahooQuote;
23 5 require 5.005;
24  
25 5 require Exporter;
26 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 5 use vars qw($VERSION @EXPORT @ISA
28 $QURL $QURLbase $QURLformat $QURLextended $QURLrealtime $QURLend
29 5 ▶ $TIMEOUT $PROXY $PROXYUSER $PROXYPASSWD);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 5 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 5 ▶use Text::ParseWords;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 5 $VERSION = '0.24';
35  
36 ## these variables govern what type of quote the modules is retrieving
37 5 $QURLbase = "http://download.finance.yahoo.com/d/quotes.csvr?e=.csv&f=";
38 5 $QURLformat = "snl1d1t1c1p2va2bapomwerr1dyj1x"; # default up to 0.19
39 5 $QURLextended = "s7t8e7e8e9r6r7r5b4p6p5j4m3m4"; # new in 0.20
40 5 $QURLrealtime = "b2b3k2k1c6m2j3"; # also new in 0.20
41 5 $QURLend = "&s=";
42 5 $QURL = $QURLbase . $QURLformat . $QURLend; # define old format as default
43  
44 5 @ISA = qw(Exporter);
45 5 @EXPORT = qw(&getquote &getonequote &getcustomquote
46 &setQueryString &useExtendedQueryFormat
47 &useRealtimeQueryFormat);
48 5 undef $TIMEOUT;
49  
50 ## simple function to switch to extended format
51 sub useExtendedQueryFormat {
52 1 ▶ $QURLformat .= $QURLextended;
Subroutine useExtendedQueryFormat: called CC=1 SCAR=0.0
Pod undocumented
53 }
54  
55 ## simple function to append real-time format
56 sub useRealtimeQueryFormat {
57 1 ▶ $QURLformat .= $QURLrealtime;
Subroutine useRealtimeQueryFormat: called CC=1 SCAR=0.0
Pod undocumented
58 }
59  
60 ## allow module user to define a new query string
61 sub setQueryString {
62 0 ▶ my $format = shift;
Subroutine setQueryString: not called CC=1 SCAR=6.9
Pod undocumented
63 0 $QURLformat = $format;
64 }
65  
66 ## Common function for reading the yahoo site and retrieving and
67 ## parsing the data returned
68 ## The format used is supplied as a second argument, the default function
69 ## getquoye() below uses the default format $QURLformat, and either one of
70 ## the two preceding functions can be used to select the extended format
71 sub readYahoo {
72  
73 6 ▶ my @symbols = @{(shift)};
Subroutine readYahoo: called CC=6 SCAR=18.0
Pod undocumented
74 6 my $format = shift;
75  
76 6 my @qr;
77 6 my $ua = RequestAgent->new;
78 6 $ua->env_proxy; # proxy settings from *_proxy env. variables.
79 6 ▶ $ua->proxy('http', $PROXY) if defined $PROXY;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Finance::YahooQuote::PROXY 0 6 6
80 6 ▶ $ua->timeout($TIMEOUT) if defined $TIMEOUT;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Finance::YahooQuote::TIMEOUT 6 0 6
81  
82 # Loop over the list of symbols, grabbing 199 symbols at a
83 # time, since yahoo only lets you get 200 per request
84 6 ▶ while ($#symbols > -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#symbols > -1) 6 6 12
85 6 ▶ my @b = $#symbols >= 199 ? splice(@symbols,0,199)
Branch: 1 branch
BranchTrueFalseTotal
$#symbols >= 199 ? : 0 6 6
86 : splice(@symbols,0,$#symbols+1);
87  
88 6 my $url = "$QURLbase${format}$QURLend".join('+',@b);
89 6 foreach (split('\015?\012',$ua->request(GET $url)->content)) {
90 18 my @q = quotewords(',',0,$_);
91 18 push(@qr,[@q]);
92 }
93 }
94  
95 6 return \@qr;
96 }
97  
98 5 my %fields = (
99 'Symbol' => 's', # old default
100 'Name' => 'n', # old default
101 'Last Trade (With Time)' => 'l',
102 'Last Trade (Price Only)' => 'l1', # old default
103 'Last Trade Date' => 'd1', # old default
104 'Last Trade Time' => 't1', # old default
105 'Last Trade Size' => 'k3',
106 'Change and Percent Change' => 'c',
107 'Change' => 'c1', # old default
108 'Change in Percent' => 'p2', # old default
109 'Ticker Trend' => 't7',
110 'Volume' => 'v', # old default
111 'Average Daily Volume' => 'a2', # old default
112 'More Info' => 'i',
113 'Trade Links' => 't6',
114 'Bid' => 'b', # old default
115 'Bid Size' => 'b6',
116 'Ask' => 'a', # old default
117 'Ask Size' => 'a5',
118 'Previous Close' => 'p', # old default
119 'Open' => 'o', # old default
120 "Day's Range" => 'm', # old default
121 '52-week Range' => 'w', # old default
122 'Change From 52-wk Low' => 'j5',
123 'Pct Chg From 52-wk Low' => 'j6',
124 'Change From 52-wk High' => 'k4',
125 'Pct Chg From 52-wk High' => 'k5',
126 'Earnings/Share' => 'e', # old default
127 'P/E Ratio' => 'r', # old default
128 'Short Ratio' => 's7',
129 'Dividend Pay Date' => 'r1', # old default
130 'Ex-Dividend Date' => 'q',
131 'Dividend/Share' => 'd', # old default
132 'Dividend Yield' => 'y', # old default
133 'Float Shares' => 'f6',
134 'Market Capitalization' => 'j1', # old default
135 '1yr Target Price' => 't8',
136 'EPS Est. Current Yr' => 'e7',
137 'EPS Est. Next Year' => 'e8',
138 'EPS Est. Next Quarter' => 'e9',
139 'Price/EPS Est. Current Yr' => 'r6',
140 'Price/EPS Est. Next Yr' => 'r7',
141 'PEG Ratio' => 'r5',
142 'Book Value' => 'b4',
143 'Price/Book' => 'p6',
144 'Price/Sales' => 'p5',
145 'EBITDA' => 'j4',
146 '50-day Moving Avg' => 'm3',
147 'Change From 50-day Moving Avg' => 'm7',
148 'Pct Chg From 50-day Moving Avg' => 'm8',
149 '200-day Moving Avg' => 'm4',
150 'Change From 200-day Moving Avg' => 'm5',
151 'Pct Chg From 200-day Moving Avg' => 'm6',
152 'Shares Owned' => 's1',
153 'Price Paid' => 'p1',
154 'Commission' => 'c3',
155 'Holdings Value' => 'v1',
156 "Day's Value Change" => 'w1',
157 'Holdings Gain Percent' => 'g1',
158 'Holdings Gain' => 'g4',
159 'Trade Date' => 'd2',
160 'Annualized Gain' => 'g3',
161 'High Limit' => 'l2',
162 'Low Limit' => 'l3',
163 'Notes' => 'n4',
164 'Last Trade (Real-time) with Time'=> 'k1',
165 'Bid (Real-time)' => 'b3',
166 'Ask (Real-time)' => 'b2',
167 'Change Percent (Real-time)' => 'k2',
168 'Change (Real-time)' => 'c6',
169 'Holdings Value (Real-time)' => 'v7',
170 "Day's Value Change (Real-time)" => 'w4',
171 'Holdings Gain Pct (Real-time)' => 'g5',
172 'Holdings Gain (Real-time)' => 'g6',
173 "Day's Range (Real-time)" => 'm2',
174 'Market Cap (Real-time)' => 'j3',
175 'P/E (Real-time)' => 'r2',
176 'After Hours Change (Real-time)' => 'c8',
177 'Order Book (Real-time)' => 'i5',
178 'Stock Exchange' => 'x' # old default
179 );
180  
181 # Let the user define which colums to retrive from yahoo
182 #
183 sub getcustomquote {
184 2 ▶ my $symbols = shift;
Subroutine getcustomquote: called CC=2 SCAR=7.0
Pod undocumented
185 2 my $columns = shift;
186  
187 2 my $format = join('',map {$fields{$_}} @{$columns});
188  
189 2 my $qr = readYahoo($symbols,$format);
190 2 ▶ return wantarray() ? @$qr : $qr;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2 2
191 }
192  
193 # get quotes for all symbols in array
194  
195 sub getquote {
196 4 ▶ my @symbols = @_;
Subroutine getquote: called CC=2 SCAR=6.9
Pod undocumented
197  
198 4 my $format = $QURLformat; ## Old default from variable
199 4 my $qr = readYahoo(\@symbols,$format);
200 4 ▶ return wantarray() ? @$qr : $qr;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 3 4
201 }
202  
203 # Input: A single stock symbol
204 # Output: An array, containing the list elements mentioned above.
205  
206 sub getonequote {
207 1 ▶ my @x;
Subroutine getonequote: called CC=3 SCAR=12.0
Pod undocumented
208 1 @x = &getquote($_[0]);
209 1 ▶ return wantarray() ? @{$x[0]} : \@{$x[0]} if @x;
Branch: 2 branches
BranchTrueFalseTotal
if @x 1 0 1
wantarray ? : 1 0 1
210 }
211  
212 BEGIN { # Local variant of LWP::UserAgent that
213 5 ▶ use LWP; # checks for user/password if document
Subroutine BEGIN: called CC=1 SCAR=0.0
214 package RequestAgent; # this code taken from lwp-request, see
215 5 ▶ no strict 'vars'; # the various LWP manual pages
Subroutine BEGIN: called CC=1 SCAR=0.0
216 5 ▶ @ISA = qw(LWP::UserAgent);
Subroutine BEGIN: called CC=1 SCAR=0.0
217  
218 sub new {
219 6 ▶ my $self = LWP::UserAgent::new(@_);
Subroutine new: called CC=1 SCAR=0.0
220 6 $self->agent("Finance-YahooQuote/0.18");
221 6 $self;
222 }
223  
224 sub get_basic_credentials {
225 0 ▶ my $self = @_;
Subroutine get_basic_credentials: not called CC=5 SCAR=34.0
226 0 ▶ if (defined($PROXYUSER) and defined($PROXYPASSWD) and
Branch: 1 branch
BranchTrueFalseTotal
if (defined $PROXYUSER and defined $PROXYPASSWD and $PROXYUSER ne '' and $PROXYPASSWD ne '') { } 0 0 0
Condition0%0/9
defined $PROXYUSER and defined $PROXYPASSWD and $PROXYUSER ne "" and $PROXYPASSWD ne ""
!ll&&!rl&&r
000
defined $PROXYUSER and defined $PROXYPASSWD and $PROXYUSER ne ""
!ll&&!rl&&r
000
defined $PROXYUSER and defined $PROXYPASSWD
!ll&&!rl&&r
000
MC/DC0%0/4
defined $PROXYUSER and defined $PROXYPASSWD and $PROXYUSER ne "" and $PROXYPASSWD ne ""
defined $PROXYUSER defined $PROXYPASSWD $PROXYUSER ne "" $PROXYPASSWD ne ""
Truth table0%0/5
A and B and C and D
A= defined $PROXYUSERB= defined $PROXYPASSWDC= $PROXYUSER ne ""D= $PROXYPASSWD ne ""
ABCDresult
0XXX0
10XX0
110X0
11100
11111
227 $PROXYUSER ne "" and $PROXYPASSWD ne "") {
228 0 return ($PROXYUSER, $PROXYPASSWD);
229 } else {
230 return (undef, undef)
231 0 }
232 }
233 }
234  
235 5 1;
236  
237 __END__
↑ Summary