line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WWW::Crawler::Mojo::Job; |
2
|
11
|
|
|
11
|
|
59589
|
use strict; |
|
11
|
|
|
|
|
30
|
|
|
11
|
|
|
|
|
370
|
|
3
|
11
|
|
|
11
|
|
52
|
use warnings; |
|
11
|
|
|
|
|
32
|
|
|
11
|
|
|
|
|
301
|
|
4
|
11
|
|
|
11
|
|
56
|
use utf8; |
|
11
|
|
|
|
|
19
|
|
|
11
|
|
|
|
|
54
|
|
5
|
11
|
|
|
11
|
|
705
|
use Mojo::Base -base; |
|
11
|
|
|
|
|
160785
|
|
|
11
|
|
|
|
|
59
|
|
6
|
11
|
|
|
11
|
|
4359
|
use Mojo::DOM; |
|
11
|
|
|
|
|
111918
|
|
|
11
|
|
|
|
|
388
|
|
7
|
11
|
|
|
11
|
|
4793
|
use Mojo::URL; |
|
11
|
|
|
|
|
69086
|
|
|
11
|
|
|
|
|
95
|
|
8
|
11
|
|
|
11
|
|
438
|
use Mojo::Util qw(md5_sum); |
|
11
|
|
|
|
|
17
|
|
|
11
|
|
|
|
|
9942
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
has 'closed'; |
11
|
|
|
|
|
|
|
has '_context'; |
12
|
|
|
|
|
|
|
has depth => 0; |
13
|
|
|
|
|
|
|
has '_dom'; |
14
|
|
|
|
|
|
|
has 'literal_uri'; |
15
|
|
|
|
|
|
|
has 'method'; |
16
|
|
|
|
|
|
|
has 'referrer'; |
17
|
|
|
|
|
|
|
has redirect_history => sub { [] }; |
18
|
|
|
|
|
|
|
has 'tx_params'; |
19
|
|
|
|
|
|
|
has '_url'; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub context { |
22
|
30
|
|
|
30
|
1
|
5285
|
my $self = shift; |
23
|
30
|
50
|
|
|
|
69
|
if (@_) { |
24
|
0
|
|
|
|
|
0
|
$self->_context("$_[0]"); |
25
|
0
|
0
|
0
|
|
|
0
|
$self->_dom(1) if ref $_[0] && ref $_[0] eq 'Mojo::DOM'; |
26
|
|
|
|
|
|
|
} |
27
|
30
|
50
|
|
|
|
76
|
if (my $c = $self->_context) { |
28
|
30
|
100
|
|
|
|
182
|
return Mojo::DOM->new($c)->[0] if $self->_dom; |
29
|
1
|
|
|
|
|
9
|
return Mojo::URL->new($c); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub new { |
34
|
102
|
|
|
102
|
1
|
18822
|
my ($class, %args) = (@_); |
35
|
102
|
100
|
|
|
|
311
|
$args{_url} = (delete $args{url}) if $args{url}; |
36
|
102
|
100
|
|
|
|
291
|
$args{_url} = "$args{_url}" if $args{_url}; |
37
|
|
|
|
|
|
|
|
38
|
102
|
50
|
|
|
|
19552
|
$args{_context} = (delete $args{context}) if $args{context}; |
39
|
102
|
100
|
100
|
|
|
513
|
$args{_dom} = 1 if ref $args{_context} && ref $args{_context} eq 'Mojo::DOM'; |
40
|
102
|
100
|
|
|
|
304
|
$args{_context} = "$args{_context}" if $args{_context}; |
41
|
|
|
|
|
|
|
|
42
|
102
|
|
|
|
|
6338
|
$class->SUPER::new(%args); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub url { |
46
|
110
|
|
|
110
|
1
|
15518
|
my $self = shift; |
47
|
110
|
100
|
|
|
|
358
|
$self->_url("$_[0]") if @_; |
48
|
110
|
|
|
|
|
412
|
return Mojo::URL->new($self->_url); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub upgrade { |
52
|
60
|
|
|
60
|
1
|
344
|
my ($class, $job) = @_; |
53
|
|
|
|
|
|
|
|
54
|
60
|
100
|
100
|
|
|
236
|
if (!ref $job || ref $job ne __PACKAGE__) { |
55
|
8
|
|
|
|
|
22
|
$job = $class->new(_url => $job); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
60
|
|
|
|
|
219
|
return $job; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub clone { |
62
|
1
|
|
|
1
|
1
|
650
|
my $self = shift; |
63
|
1
|
|
|
|
|
4
|
return __PACKAGE__->new(%$self); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub close { |
67
|
14
|
|
|
14
|
1
|
1170
|
my $self = shift; |
68
|
14
|
|
|
|
|
58
|
$self->{closed} = 1; |
69
|
14
|
|
|
|
|
679
|
$self->{referrer} = undef; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub child { |
73
|
77
|
|
|
77
|
1
|
3254
|
my $self = shift; |
74
|
77
|
|
|
|
|
226
|
return __PACKAGE__->new(@_, referrer => $self, depth => $self->depth + 1); |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub digest { |
78
|
64
|
|
|
64
|
1
|
186
|
my $self = shift; |
79
|
64
|
|
100
|
|
|
125
|
my $md5_seed = $self->_url . ($self->method || ''); |
80
|
64
|
100
|
|
|
|
520
|
$md5_seed .= $self->tx_params->to_string if ($self->tx_params); |
81
|
64
|
|
|
|
|
813
|
return md5_sum($md5_seed); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub redirect { |
85
|
14
|
|
|
14
|
1
|
58
|
my ($self, @history) = @_; |
86
|
14
|
|
|
|
|
37
|
@history = map {"$_"} @history; |
|
16
|
|
|
|
|
75
|
|
87
|
14
|
|
|
|
|
3543
|
my $last = shift @history; |
88
|
14
|
|
|
|
|
73
|
$self->url($last); |
89
|
14
|
|
|
|
|
2058
|
$self->redirect_history(\@history); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub original_url { |
93
|
1
|
|
|
1
|
1
|
1511
|
my $self = shift; |
94
|
1
|
|
|
|
|
1
|
my @histry = @{$self->redirect_history}; |
|
1
|
|
|
|
|
3
|
|
95
|
1
|
50
|
|
|
|
17
|
my $url = scalar @histry ? $histry[$#histry] : $self->url; |
96
|
1
|
|
|
|
|
4
|
return Mojo::URL->new($url); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
1; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head1 NAME |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
WWW::Crawler::Mojo::Job - Single crawler job |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head1 SYNOPSIS |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
my $job1 = WWW::Crawler::Mojo::Job->new; |
108
|
|
|
|
|
|
|
$job1->url('http://example.com/'); |
109
|
|
|
|
|
|
|
my $job2 = $job1->child; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 DESCRIPTION |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
This class represents a single crawler job. |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head2 context |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Either L or L instance that the job is referrered by. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
$job->context($dom); |
122
|
|
|
|
|
|
|
say $job->context; |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head2 closed |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
A flag indecates whether the job is closed or not. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
$job->closed(1); |
129
|
|
|
|
|
|
|
say $job->closed; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head2 depth |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
The depth of the job in referrer series. |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
my $job1 = WWW::Crawler::Mojo::Job->new; |
136
|
|
|
|
|
|
|
my $job2 = $job1->child; |
137
|
|
|
|
|
|
|
my $job3 = $job2->child; |
138
|
|
|
|
|
|
|
say $job1->depth; # 0 |
139
|
|
|
|
|
|
|
say $job2->depth; # 1 |
140
|
|
|
|
|
|
|
say $job3->depth; # 2 |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head2 literal_uri |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
A L instance of the literal URL that has appeared in the referrer |
145
|
|
|
|
|
|
|
document. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
$job1->literal_uri('./index.html'); |
148
|
|
|
|
|
|
|
say $job1->literal_uri; # './index.html' |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head2 referrer |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
A job instance that has referred the URL. |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
$job1->referrer($job); |
155
|
|
|
|
|
|
|
my $job2 = $job1->referrer; |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head2 redirect_history |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
An array reference that contains URLs of redirect history. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
$job1->redirect_history([$url1, $url2, $url3]); |
162
|
|
|
|
|
|
|
my $history = $job1->redirect_history; |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head2 url |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
A L instance of the resolved URL. |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
$job1->url('http://example.com/'); |
169
|
|
|
|
|
|
|
say $job1->url; # 'http://example.com/' |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head2 method |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
HTTP request method such as GET or POST. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
$job1->method('GET'); |
176
|
|
|
|
|
|
|
say $job1->method; # GET |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head2 tx_params |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
A hash reference that contains params for L. |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
$job1->tx_params({foo => 'bar'}); |
183
|
|
|
|
|
|
|
$params = $job1->tx_params; |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=head1 METHODS |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=head2 clone |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
Clones the job. |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
my $job2 = $job1->clone; |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=head2 close |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
Closes the job and cuts the referrer series. |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
$job->close; |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head2 child |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
Instantiates a child job by parent job. The parent URL is set to child referrer. |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
my $job1 = WWW::Crawler::Mojo::Job->new(url => 'http://example.com/1'); |
204
|
|
|
|
|
|
|
my $job2 = $job1->child(url => 'http://example.com/2'); |
205
|
|
|
|
|
|
|
say $job2->referrer->url # 'http://example.com/1' |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=head2 digest |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
Generates digest string with C, C, C attributes. |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
say $job->digest; |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=head2 redirect |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
Replaces the resolved URL and history at once. |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
my $job = WWW::Crawler::Mojo::Job->new; |
218
|
|
|
|
|
|
|
$job->url($url1); |
219
|
|
|
|
|
|
|
$job->redirect($url2, $url3); |
220
|
|
|
|
|
|
|
say $job->url # $url2 |
221
|
|
|
|
|
|
|
say $job->redirect_history # [$url1, $url3] |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=head2 original_url |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
Returns the original URL of redirected job. If redirected, returns last element |
226
|
|
|
|
|
|
|
of C attribute, otherwise returns C attribute. |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
$job1->redirect_history([$url1, $url2, $url3]); |
229
|
|
|
|
|
|
|
my $url4 = $job1->original_url; # $url4 is $url3 |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=head2 upgrade |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
Instanciates a job with string or a L instance. |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
=head1 AUTHOR |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
Keita Sugama, Esugama@jamadam.comE |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
Copyright (C) Keita Sugama. |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or |
244
|
|
|
|
|
|
|
modify it under the same terms as Perl itself. |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
=cut |