Grab.pm

filter: Statementstmt 77.9% 131 / 168 Branchbran 47.7% 43 / 90 Conditioncond 31.0% 18 / 58 MC/DCmcdc 5.7% 2 / 35 Subroutinesub 75.0% 12 / 16 Podpod 75.0% 6 / 8 totaltotal 56.5% 212 / 375 CC 65 SCAR 32.2
CC
65
Cov
61%
grab_new
110.0
grab
19.1
expand_url
19.0
CRAP 25.1
↑ Summary Grab/RequestAgent.pm »
1 package Image::Grab;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # $Id: Grab.pm,v 1.6 2002/01/19 21:14:01 mah Exp $
7 8 $VERSION = '1.4.2';
8  
9 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 require HTTP::Request;
12 8 require HTML::TreeBuilder;
13 8 require URI::URL;
14 8 require Image::Grab::RequestAgent;
15 8 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 8 require Exporter;
18  
19 8 @ISA = qw(Exporter);
20 8 @EXPORT_OK = qw(
21 &expand_url &grab
22 );
23  
24 # %fields, new, AUTOLOAD are from perltoot
25  
26 my %fields = (
27 cookiefile => undef,
28 cookiejar => undef,
29 date => undef,
30 image => undef,
31 "index" => undef,
32 md5 => undef,
33 refer => undef,
34 regexp => undef,
35 type => undef,
36 ua => undef,
37 url => undef,
38 search_url => undef,
39 debug => undef,
40 do_posix => ($Config{patchlevel} && $Config{patchlevel} >= 5 and
41 8 ▶ $Config{baserev} && $Config{baserev} >= 5) ? 1 : undef,
Branch: 1 branch
BranchTrueFalseTotal
$Image::Grab::Config{'patchlevel'} && $Image::Grab::Config{'patchlevel'} >= 5 && ($Image::Grab::Config{'baserev'} && $Image::Grab::Config{'baserev'} >= 5) ? : 8 0 8
Condition33%3/9
$Image::Grab::Config{"patchlevel"} and $Image::Grab::Config{"patchlevel"} >= 5 and ($Image::Grab::Config{"baserev"} and $Image::Grab::Config{"baserev"} >= 5)
!ll&&!rl&&r
008
$Image::Grab::Config{"patchlevel"} and $Image::Grab::Config{"patchlevel"} >= 5
!ll&&!rl&&r
008
$Image::Grab::Config{"baserev"} and $Image::Grab::Config{"baserev"} >= 5
!ll&&!rl&&r
008
MC/DC0%0/4
$Image::Grab::Config{"patchlevel"} and $Image::Grab::Config{"patchlevel"} >= 5 and ($Image::Grab::Config{"baserev"} and $Image::Grab::Config{"baserev"} >= 5)
$Image::Grab::Config{"patchlevel"} $Image::Grab::Config{"patchlevel"} >= 5 $Image::Grab::Config{"baserev"} $Image::Grab::Config{"baserev"} >= 5
Truth table20%1/5
A and B and C and D
A= $Image::Grab::Config{"patchlevel"}B= $Image::Grab::Config{"patchlevel"} >= 5C= $Image::Grab::Config{"baserev"}D= $Image::Grab::Config{"baserev"} >= 5
ABCDresult
0XXX0
10XX0
110X0
11100
11111
42 );
43  
44 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
45  
46 11 ▶sub new {
Subroutine new: called CC=5 SCAR=16.2
Pod undocumented
47 11 my $that = shift;
48 11 ▶ my $class = ref($that) || $that;
Condition33%1/3
ref $that || $that
l!l&&r!l&&!r
0110
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table33%1/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
49 11 my $self = {
50 _permitted => \%fields,
51 %fields,
52 };
53  
54 11 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 7 11
55 4 my %arg = @_;
56  
57 4 foreach (keys %arg) {
58 croak "Can't access `$_' field"
59 5 ▶ unless exists $self->{_permitted}->{lc($_)};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_permitted"}{lc $_} 0 5 5
60 5 $self->{lc($_)} = $arg{$_};
61 }
62 }
63  
64 11 bless ($self, $class);
65 11 $self->ua(new Image::Grab::RequestAgent);
66 11 $self->{have_DigestMD5} = eval {require Digest::MD5};
67 11 $self->{have_MD5} = eval {require MD5;};
68 11 $self->{have_magick} = eval {require Image::Magick;};
69 11 return $self;
70 }
71  
72 274 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=6 SCAR=18.3
73 274 my $self = shift;
74 274 ▶ my $type = ref($self)
Branch: 1 branch
BranchTrueFalseTotal
my $type = ref $self or croak("$self is not an object") 274 0 274
75 or croak "$self is not an object";
76  
77 274 my $name = $AUTOLOAD;
78 274 $name =~ s/.*://;
79  
80 274 ▶ unless (exists $self->{_permitted}->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_permitted"}{$name}) 0 274 274
81 0 croak "Can't access `$name' field in class $type";
82 }
83  
84 274 ▶ if(@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 63 211 274
elsif (defined $self->{$name}) { } 85 126 211
85 63 my $val = shift;
86 63 ▶ carp "$name: $val" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 63 63
87 63 return $self->{$name} = $val;
88 } elsif (defined $self->{$name}) {
89 85 return $self->{$name};
90 }
91  
92 126 return undef;
93  
94 }
95  
96 # Accessor functions that we have to write.
97 0 ▶sub realm {
Subroutine realm: not called CC=3 SCAR=24.8
Pod undocumented
98 0 my $self = shift;
99 0 ▶ my $type = ref($self)
Branch: 1 branch
BranchTrueFalseTotal
my $type = ref $self or croak("$self is not an object") 0 0 0
100 or croak "$self is not an object";
101  
102 0 ▶ if($#_ == 2){
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ == 2) 0 0 0
103 0 $self->ua->register_realm(shift, shift, shift);
104 0 return 1;
105 }
106  
107 0 croak "usage: realm(\$realm, \$user, \$pass)";
108 }
109  
110 3 ▶sub getAllURLs {
Subroutine getAllURLs: called CC=11 SCAR=26.7
Pod documented
111 3 my $self = shift;
112 3 ▶ my $type = ref($self)
Branch: 1 branch
BranchTrueFalseTotal
my $type = ref $self or croak("$self is not an object") 3 0 3
113 or croak "$self is not an object";
114 3 ▶ my $times = (shift or 10);
Condition100%2/2
shift() || 10
l!l
12
MC/DC100%1/1
shift() || 10
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
115 3 my $req;
116 3 my $count = 0;
117 3 my @link;
118 my @now;
119  
120 # Need to load Cookie Jar?
121 3 $self->loadCookieJar;
122  
123 3 @now = localtime;
124 3 ▶ $self->search_url(strftime $self->search_url, @now)
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->search_url and defined $self->do_posix 3 0 3
Condition33%1/3
defined $self->search_url and defined $self->do_posix
!ll&&!rl&&r
003
MC/DC0%0/2
defined $self->search_url and defined $self->do_posix
defined $self->search_url defined $self->do_posix
Truth table33%1/3
A and B
A= defined $self->search_urlB= defined $self->do_posix
ABresult
0X0
100
111
125 if defined $self->search_url and defined $self->do_posix;
126 3 ▶ croak "Need to specify a search_url!" if !defined $self->search_url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->search_url 0 3 3
127 3 $req = $self->ua->request(new HTTP::Request 'GET', $self->search_url);
128  
129 # Try $times until successful
130 3 ▶ while( (!$req->is_success) && $count < $times){
Branch: 1 branch
BranchTrueFalseTotal
if (not $req->is_success and $count < $times) 0 3 3
Condition33%1/3
not $req->is_success and $count < $times
!ll&&!rl&&r
300
MC/DC0%0/2
not $req->is_success and $count < $times
not $req->is_success $count < $times
Truth table33%1/3
not(A) and B
A= not $req->is_successB= $count < $times
ABresult
0X0
100
111
131 0 $req = $self->ua->request(new HTTP::Request 'GET', $self->search_url);
132 0 $count = $count + 1;
133 }
134  
135 # return failure if we couldn't connect within $times tries
136 3 ▶ if($count == $times && !$req->is_success){
Branch: 1 branch
BranchTrueFalseTotal
if ($count == $times and not $req->is_success) 0 3 3
Condition33%1/3
$count == $times and not $req->is_success
!ll&&!rl&&r
300
MC/DC0%0/2
$count == $times and not $req->is_success
$count == $times not $req->is_success
Truth table33%1/3
A and not(B)
A= $count == $timesB= not $req->is_success
ABresult
0X0
100
111
137 0 return undef;
138 }
139  
140 # Get the base url
141 3 my $base_url = $req->base;
142  
143 # Get the img tags out of the document.
144 3 my $parser = new HTML::TreeBuilder;
145 3 $parser->parse($req->content);
146 3 $parser->eof;
147 3 foreach (@{$parser->extract_links(qw(img td body))}) {
148 9 push @link, URI::URL::url($$_[0])->abs($base_url)->as_string;
149 }
150 3 $parser->delete;
151  
152 3 return @link;
153 }
154  
155 0 ▶sub getRealURL {
Subroutine getRealURL: not called CC=3 SCAR=24.8
Pod documented
156 0 my $self = shift;
157 0 ▶ my $type = ref($self)
Branch: 1 branch
BranchTrueFalseTotal
my $type = ref $self or croak("$self is not an object") 0 0 0
158 or croak "$self is not an object";
159 0 ▶ my $times = (shift or 10);
Condition0%0/2
shift() || 10
l!l
00
MC/DC0%0/1
shift() || 10
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
160  
161 0 carp "getRealURL has been deprecated. Use expand_url.";
162 0 $self->expand_url(@_);
163 }
164  
165 8 ▶sub expand_url {
Subroutine expand_url: called CC=14 SCAR=29.4
Pod documented
166 8 my $self = shift;
167 8 ▶ my $type = ref($self)
Branch: 1 branch
BranchTrueFalseTotal
my $type = ref $self or croak("$self is not an object") 8 0 8
168 or croak "$self is not an object";
169 8 ▶ my $times = (shift or 10);
Condition50%1/2
shift() || 10
l!l
80
MC/DC0%0/1
shift() || 10
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
170 8 my $req;
171 8 my $count = 0;
172 8 my @link;
173 my @now;
174  
175 # Expand any POSIX time escapes
176 8 @now = localtime;
177  
178 8 ▶ if(defined $self->url) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->url) 7 1 8
179 7 ▶ $self->url(strftime($self->url, @now))
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->do_posix 7 0 7
180 if defined $self->do_posix;
181 7 return $self->url;
182 }
183 1 ▶ $self->regexp(strftime($self->regexp, @now))
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->regexp and defined $self->do_posix 1 0 1
Condition33%1/3
defined $self->regexp and defined $self->do_posix
!ll&&!rl&&r
001
MC/DC0%0/2
defined $self->regexp and defined $self->do_posix
defined $self->regexp defined $self->do_posix
Truth table33%1/3
A and B
A= defined $self->regexpB= defined $self->do_posix
ABresult
0X0
100
111
184 if defined $self->regexp and defined $self->do_posix;
185  
186 1 @link = $self->getAllURLs($times);
187 1 ▶ return undef if !@link;
Branch: 1 branch
BranchTrueFalseTotal
unless @link 0 1 1
188  
189 # if this is a relative position tag...
190 1 ▶ if($self->regexp || $self->index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->regexp or $self->index) 1 0 1
Condition33%1/3
$self->regexp or $self->index
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->regexp or $self->index
$self->regexp $self->index
Truth table33%1/3
A or B
A= $self->regexpB= $self->index
ABresult
1X1
011
000
191 1 my (@match, $re);
192  
193 1 $self->refer($self->search_url);
194 # set index to match first image
195 1 ▶ $self->index(0) if !defined $self->index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->index 1 0 1
196 1 ▶ $re = $self->regexp || '.';
Condition50%1/2
$self->regexp || '.'
l!l
10
MC/DC0%0/1
$self->regexp || '.'
$self->regexp
Truth table50%1/2
A
A= $self->regexp
Aresult
11
00
197 1 ▶ @match = grep {defined && /$re/} @link;
Condition66%2/3
defined $_ && /$re/
!ll&&!rl&&r
021
MC/DC50%1/2
defined $_ && /$re/
defined $_ /$re/
Truth table66%2/3
A && B
A= defined $_B= /$re/
ABresult
0X0
100
111
198 # Return the nth
199 1 ▶ return $match[$self->index]
Branch: 1 branch
BranchTrueFalseTotal
if @match 1 0 1
200 if @match;
201 }
202  
203 # only if we fail.
204 0 return undef;
205 }
206  
207 11 ▶sub loadCookieJar {
Subroutine loadCookieJar: called CC=6 SCAR=22.9
Pod documented
208 11 my $self = shift;
209 11 ▶ my $type = ref($self)
Branch: 1 branch
BranchTrueFalseTotal
my $type = ref $self or croak("$self is not an object") 11 0 11
210 or croak "$self is not an object";
211  
212 # need to do CookieJar initialization?
213 11 ▶ if($self->cookiefile and !-f $self->cookiefile){
Branch: 2 branches
BranchTrueFalseTotal
if ($self->cookiefile and not -f $self->cookiefile) { } 0 11 11
elsif ($self->cookiefile and not defined $self->cookiejar) { } 0 22 22
Condition33%1/3
$self->cookiefile and not -f $self->cookiefile
!ll&&!rl&&r
1100
MC/DC0%0/2
$self->cookiefile and not -f $self->cookiefile
$self->cookiefile not -f $self->cookiefile
Truth table33%1/3
A and not(B)
A= $self->cookiefileB= not -f $self->cookiefile
ABresult
0X0
100
111
214 0 carp $self->cookiefile, " is not a file";
215 ▶ } elsif ($self->cookiefile and !defined $self->cookiejar) {
Condition33%1/3
$self->cookiefile and not defined $self->cookiejar
!ll&&!rl&&r
1100
MC/DC0%0/2
$self->cookiefile and not defined $self->cookiejar
$self->cookiefile not defined $self->cookiejar
Truth table33%1/3
A and not(B)
A= $self->cookiefileB= not defined $self->cookiejar
ABresult
0X0
100
111
216 8 ▶ use HTTP::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
217  
218 0 $self->cookiejar(
219 HTTP::Cookies::Netscape->new( File => $self->cookiefile,
220 AutoSave => 0,
221 ));
222 0 $self->cookiejar->load();
223 }
224  
225 }
226  
227 8 ▶sub grab {
Subroutine grab: called CC=15 SCAR=29.5
Pod documented
228 8 my $self = shift;
229 8 my $times = 1;
230  
231 8 ▶ if(ref($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 5 3 8
232 5 ▶ if(my $c = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = shift()) 0 5 5
233 0 $times = $c;
234 }
235 } else {
236 3 ▶ if($self eq __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self eq __PACKAGE__) { } 1 2 3
237 1 $self = Image::Grab->new(@_);
238 } else {
239 2 $self = Image::Grab->new(lc $self, @_);
240 }
241 }
242 8 my $req;
243 my $count;
244 8 my $rc;
245  
246 # need to do CookieJar initialization?
247 8 $self->loadCookieJar;
248  
249 # need to find image on page?
250 8 my $url = $self->expand_url($times);
251  
252 # make sure we have a url
253 8 ▶ croak "Couldn't determine an absolute URL!\n" unless defined $url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url 0 8 8
254 8 ▶ carp "Fetching URL: ", $url if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 8 8
255  
256 # Set it up
257 8 $req = new HTTP::Request 'GET', $url;
258 8 ▶ $req->push_header('Referer', $self->refer) if defined $self->refer;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->refer 1 7 8
259 8 ▶ if($self->cookiejar){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cookiejar) 0 8 8
260 0 $self->cookiejar->add_cookie_header($req);
261 }
262  
263 # Knock it down
264 8 $count = 0;
265 8 ▶ do{
Branch: 1 branch
BranchTrueFalseTotal
$count <= $times and not $rc->is_success and do { do { ++$count; $rc = $self->ua->request($req); carp("Got: ", $rc->content) if $self->debug } } 0 8 8
Condition33%1/3
$count <= $times and not $rc->is_success
!ll&&!rl&&r
080
MC/DC0%0/2
$count <= $times and not $rc->is_success
$count <= $times not $rc->is_success
Truth table33%1/3
A and not(B)
A= $count <= $timesB= not $rc->is_success
ABresult
0X0
100
111
266 8 $count++;
267 8 $rc = $self->ua->request($req);
268 8 ▶ carp "Got: ", $rc->content
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 8 8
269 if $self->debug;
270 } while($count <= $times and not $rc->is_success);
271  
272 # Did we fail?
273 8 ▶ return 0 unless $rc->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $rc->is_success 0 8 8
274  
275 8 ▶ carp "Message: ", $rc->message if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 8 8
276  
277 # save what we got
278 8 $self->image($rc->content);
279 8 $self->date($rc->last_modified);
280  
281 8 ▶ if($self->{have_DigestMD5}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'have_DigestMD5'}) { } 8 0 8
elsif ($self->{'have_MD5'}) { } 0 0 0
282 8 $self->md5(Digest::MD5::md5_hex($self->image));
283 } elsif ($self->{have_MD5}) {
284 0 $self->md5(MD5->hexhash($self->image));
285 }
286  
287  
288 8 $self->type($rc->content_type);
289  
290 8 $self->image;
291 }
292  
293 0 ▶sub grab_new {
Subroutine grab_new: not called CC=10 SCAR=47.0
Pod documented
294 0 my $self = shift;
295 0 ▶ my $type = ref($self)
Branch: 1 branch
BranchTrueFalseTotal
my $type = ref $self or croak("$self is not an object") 0 0 0
296 or croak "$self is not an object";
297 0 ▶ my $tries = shift || 10;
Condition0%0/2
shift() || 10
l!l
00
MC/DC0%0/1
shift() || 10
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
298  
299 0 ▶ return $self->grab($tries)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->date or defined $self->md5 0 0 0
Condition0%0/3
defined $self->date or defined $self->md5
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $self->date or defined $self->md5
defined $self->date defined $self->md5
Truth table0%0/3
A or B
A= defined $self->dateB= defined $self->md5
ABresult
1X1
011
000
300 unless defined $self->date || defined $self->md5;
301  
302 0 my $tmp = $type->new;
303 0 $tmp->url($self->url);
304 0 $tmp->search_url($self->search_url);
305 0 $tmp->index($self->index);
306 0 $tmp->regexp($self->regexp);
307 0 $tmp->grab;
308  
309 0 my $grab_new = 1;
310  
311 0 ▶ $grab_new = 0
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->date and $self->date >= $tmp->date 0 0 0
Condition0%0/3
defined $self->date and $self->date >= $tmp->date
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->date and $self->date >= $tmp->date
defined $self->date $self->date >= $tmp->date
Truth table0%0/3
A and B
A= defined $self->dateB= $self->date >= $tmp->date
ABresult
0X0
100
111
312 if defined $self->date && $self->date >= $tmp->date;
313 0 ▶ $grab_new = 0
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->md5 and $self->md5 eq $tmp->md5 0 0 0
Condition0%0/3
defined $self->md5 and $self->md5 eq $tmp->md5
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->md5 and $self->md5 eq $tmp->md5
defined $self->md5 $self->md5 eq $tmp->md5
Truth table0%0/3
A and B
A= defined $self->md5B= $self->md5 eq $tmp->md5
ABresult
0X0
100
111
314 if defined $self->md5 && $self->md5 eq $tmp->md5;
315  
316 0 ▶ return $self->grab($tries)
Branch: 1 branch
BranchTrueFalseTotal
if $grab_new 0 0 0
317 if $grab_new;
318 0 return undef;
319 }
320  
321 8 1;
322 __END__
↑ Summary Grab/RequestAgent.pm »