line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package URL::Grab; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
32640
|
use 5.008; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
44
|
|
4
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
5
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
67
|
|
6
|
|
|
|
|
|
|
require Carp; |
7
|
|
|
|
|
|
|
require LWP::UserAgent; |
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
6
|
use Carp qw/carp/; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
85
|
|
10
|
1
|
|
|
1
|
|
15362
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
86126
|
|
|
1
|
|
|
|
|
1559
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
require Exporter; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
15
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [ qw() ] ); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
our @EXPORT = qw(); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
(our $VERSION) = '$Revision: 1.4 $' =~ /([\d.]+)/; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub new { |
24
|
1
|
|
|
1
|
0
|
11
|
my $class = shift; |
25
|
1
|
|
|
|
|
2
|
my $args = shift; |
26
|
1
|
|
|
|
|
2
|
my $self = { }; |
27
|
|
|
|
|
|
|
|
28
|
1
|
|
50
|
|
|
8
|
$self->{retries} = $args->{retries} || 0; |
29
|
1
|
|
|
|
|
15
|
$self->{ua} = LWP::UserAgent->new(agent => "URL::Grab $VERSION"); |
30
|
1
|
|
50
|
|
|
6349
|
$self->{ua}->{timeout} = $args->{timeout} || 30; |
31
|
|
|
|
|
|
|
|
32
|
1
|
|
|
|
|
4
|
bless($self, $class); |
33
|
1
|
|
|
|
|
4
|
return $self; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub grab_single { |
37
|
1
|
|
|
1
|
0
|
3
|
my $self = shift; |
38
|
1
|
|
|
|
|
10
|
my $url = shift; |
39
|
|
|
|
|
|
|
|
40
|
1
|
|
|
|
|
2
|
my $retval; |
41
|
1
|
50
|
33
|
|
|
11
|
if($url =~ /^https?:\/\// || $url =~ /^ftp:\/\//) { |
42
|
1
|
|
|
|
|
11
|
my $res = $self->{ua}->get($url); |
43
|
1
|
|
|
|
|
453637
|
my $retries = 0; |
44
|
1
|
|
|
|
|
7
|
while($self->{retries} >= $retries) { |
45
|
1
|
50
|
|
|
|
7
|
if($res->is_success()) { |
46
|
1
|
|
|
|
|
20
|
return { $url => $res->content() }; |
47
|
|
|
|
|
|
|
} |
48
|
0
|
|
|
|
|
0
|
$retries++; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
} else { |
51
|
0
|
0
|
0
|
|
|
0
|
if($url =~ /^file:\/\// || $url =~ /^\//) { |
52
|
0
|
|
|
|
|
0
|
my $tmp_url = $url; |
53
|
0
|
|
|
|
|
0
|
$tmp_url =~ s/^file://; |
54
|
0
|
0
|
|
|
|
0
|
if(-f $tmp_url) { |
55
|
0
|
|
|
|
|
0
|
my $res; |
56
|
0
|
|
|
|
|
0
|
open(FH, $tmp_url); |
57
|
0
|
|
|
|
|
0
|
$res .= $_ while(); |
58
|
0
|
|
|
|
|
0
|
close(FH); |
59
|
0
|
|
|
|
|
0
|
return { $url => $res }; |
60
|
|
|
|
|
|
|
} else { |
61
|
0
|
|
|
|
|
0
|
carp "No such file or directory"; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} else { |
64
|
0
|
|
|
|
|
0
|
carp "Unknown transport protocol"; |
65
|
0
|
|
|
|
|
0
|
return undef; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
0
|
|
|
|
|
0
|
return undef; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub grab { |
72
|
1
|
|
|
1
|
0
|
5
|
my $self = shift; |
73
|
1
|
|
|
|
|
2
|
my @urls; |
74
|
1
|
|
|
|
|
5
|
while(my $arg = shift) { |
75
|
1
|
50
|
|
|
|
3
|
if(ref $arg eq 'ARRAY') { push @urls, $_ foreach(@{$arg}); |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
76
|
1
|
|
|
|
|
4
|
} else { push @urls, $arg; } |
77
|
|
|
|
|
|
|
} |
78
|
1
|
|
|
|
|
4
|
$self->{retval}->{$_} = $self->grab_single($_) foreach (@urls); |
79
|
1
|
|
|
|
|
115
|
return $self->{retval}; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub grab_failover { |
83
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
84
|
0
|
|
|
|
|
|
my @urls; |
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
|
while (my $arg = shift) { |
87
|
0
|
|
|
|
|
|
push @urls, $arg; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
|
foreach my $url (@urls) { |
91
|
0
|
|
|
|
|
|
my $content = $self->grab_single($url); |
92
|
0
|
0
|
|
|
|
|
return $content if $content; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub grab_mirrorlist { |
97
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
98
|
0
|
|
|
|
|
|
my @urls; |
99
|
0
|
|
|
|
|
|
while(my $arg = shift) { |
100
|
0
|
|
|
|
|
|
push @urls, $arg; |
101
|
|
|
|
|
|
|
} |
102
|
0
|
|
|
|
|
|
foreach my $mirror (@urls) { |
103
|
0
|
0
|
0
|
|
|
|
if(ref $mirror eq 'SCALAR' || ref \$mirror eq 'SCALAR') { |
104
|
0
|
|
|
|
|
|
$self->{retval} = $self->grab_single($mirror) |
105
|
|
|
|
|
|
|
} |
106
|
0
|
0
|
|
|
|
|
$self->{retval} = $self->grab_failover(@{$mirror}) if ref $mirror eq 'ARRAY'; |
|
0
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
} |
108
|
0
|
|
|
|
|
|
return $self->{retval}; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
1; |
112
|
|
|
|
|
|
|
__END__ |