line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Pipeline::Adapter::FetchPage; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
2189
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
extends 'Data::Pipeline::Adapter'; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use Data::Pipeline::Types qw( Iterator ); |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use LWP; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
has url => ( |
11
|
|
|
|
|
|
|
isa => Iterator, |
12
|
|
|
|
|
|
|
is => 'rw', |
13
|
|
|
|
|
|
|
required => 1, |
14
|
|
|
|
|
|
|
coerce => 1 |
15
|
|
|
|
|
|
|
); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
has cut_start => ( |
18
|
|
|
|
|
|
|
isa => 'Str', |
19
|
|
|
|
|
|
|
is => 'rw', |
20
|
|
|
|
|
|
|
required => 1 |
21
|
|
|
|
|
|
|
); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
has cut_end => ( |
24
|
|
|
|
|
|
|
isa => 'Str', |
25
|
|
|
|
|
|
|
is => 'rw', |
26
|
|
|
|
|
|
|
required => 1 |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
has split => ( |
30
|
|
|
|
|
|
|
isa => 'Str', |
31
|
|
|
|
|
|
|
is => 'rw', |
32
|
|
|
|
|
|
|
required => 1 |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
has '+source' => ( |
36
|
|
|
|
|
|
|
default => sub { |
37
|
|
|
|
|
|
|
my $self = shift; |
38
|
|
|
|
|
|
|
my $start = -1; |
39
|
|
|
|
|
|
|
my $inc = length($self -> split); |
40
|
|
|
|
|
|
|
my $end; |
41
|
|
|
|
|
|
|
my $content; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
Data::Pipeline::Iterator::Source -> new( |
44
|
|
|
|
|
|
|
has_next => sub { $start != -1 || !$self -> url -> finished }, |
45
|
|
|
|
|
|
|
get_next => sub { |
46
|
|
|
|
|
|
|
if( $start == -1 ) { |
47
|
|
|
|
|
|
|
return +{ content => '' } if $self -> url -> finished; |
48
|
|
|
|
|
|
|
$content = $self -> fetch_page; |
49
|
|
|
|
|
|
|
$start = 0; |
50
|
|
|
|
|
|
|
$end = index( $content, $self -> split ); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
my $c; |
53
|
|
|
|
|
|
|
if( $end == -1 ) { |
54
|
|
|
|
|
|
|
$c = substr($content, $start); |
55
|
|
|
|
|
|
|
$start = -1; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
else { |
58
|
|
|
|
|
|
|
$c = substr($content, $start, $end - $start ); |
59
|
|
|
|
|
|
|
$start = $end + $inc; |
60
|
|
|
|
|
|
|
$end = index( $content, $self -> split, $start ); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
#print STDERR "content: [$c]\n"; |
63
|
|
|
|
|
|
|
return +{ content => $c }; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub fetch_page { |
70
|
|
|
|
|
|
|
my($self) = @_; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
return '' if $self -> url -> finished; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new; |
75
|
|
|
|
|
|
|
$ua->timeout(10); |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
my $u; |
78
|
|
|
|
|
|
|
my $response = $ua->get($u = $self -> url -> next); |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
unless ($response->is_success) { |
81
|
|
|
|
|
|
|
Carp::croak $response->status_line; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
my $content = $response -> content; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
my $start = index($content, $self -> cut_start); |
87
|
|
|
|
|
|
|
my $end = rindex( $content, $self -> cut_end ); |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
return substr($content, $start, $end - $start); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
1; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
__END__ |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 NAME |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Data::Pipeline::Adapter::FetchPage - fetch a web page and split it into items |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 SYNOPSIS |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
use Data::Pipeline qw( FetchPage ); |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Pipeline( |
105
|
|
|
|
|
|
|
FetchPage( |
106
|
|
|
|
|
|
|
cut_begin => ..., |
107
|
|
|
|
|
|
|
cut_end => ..., |
108
|
|
|
|
|
|
|
split => ..., |
109
|
|
|
|
|
|
|
url => ... |
110
|
|
|
|
|
|
|
) |
111
|
|
|
|
|
|
|
) |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 DESCRIPTION |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
|