line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mail::POP3::Folder::webscrape; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our @ISA = qw(Mail::POP3::Folder); |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 CONCEPTS |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=over |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=item a "listpage" is returned by the initial get_fill_submit which is parsed into: |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=item a "listpage" is parsed into: |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
{ items => \@items, pageno => $pageno, num_pages => $num_pages, |
14
|
|
|
|
|
|
|
nextlink => $nextlink, } |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=item an "item" is |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
+{ id => $id, url => $url, } |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=item the item url points to a "page" which is parsed into |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=back |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 ADDITIONAL METHODS |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head2 list_parse |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
($text, $pageurl, $listre) |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head2 one_parse |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
Function: |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
($text, $scrapespec, $scrapepostpro) |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head2 parse_fill_submit |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
Function: |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
($cjar, $html, $real_url, $vars, $varnamechange) |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head2 parse_refresh |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
Parses out redirects done with C<< Refresh >> header. |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=head2 redirect_cookie_loop |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
Gets web content, iterating through redirects while capturing cookies. |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=cut |
51
|
|
|
|
|
|
|
|
52
|
1
|
|
|
1
|
|
1414
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
31
|
|
53
|
1
|
|
|
1
|
|
513
|
use HTML::Entities; |
|
1
|
|
|
|
|
5896
|
|
|
1
|
|
|
|
|
77
|
|
54
|
1
|
|
|
1
|
|
558
|
use HTML::Form; |
|
1
|
|
|
|
|
11801
|
|
|
1
|
|
|
|
|
35
|
|
55
|
1
|
|
|
1
|
|
544
|
use HTTP::Cookies; |
|
1
|
|
|
|
|
11005
|
|
|
1
|
|
|
|
|
34
|
|
56
|
1
|
|
|
1
|
|
468
|
use HTTP::Request::Common; |
|
1
|
|
|
|
|
18189
|
|
|
1
|
|
|
|
|
84
|
|
57
|
1
|
|
|
1
|
|
428
|
use URI::URL; |
|
1
|
|
|
|
|
2937
|
|
|
1
|
|
|
|
|
109
|
|
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
my $formno = 0; # form_fill |
60
|
|
|
|
|
|
|
# this is at top so $DEBUG in L::UA::RNOk is correct one! |
61
|
|
|
|
|
|
|
our $DEBUG = 0; # form_fill, redirect_cookie_loop et al |
62
|
|
|
|
|
|
|
my $req_count = 0; # redirect_cookie_loop et al |
63
|
|
|
|
|
|
|
require Data::Dumper if $DEBUG; # form_fill |
64
|
|
|
|
|
|
|
my $CRLF = "\015\012"; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
{ |
67
|
|
|
|
|
|
|
# redirect_cookie_loop et al |
68
|
|
|
|
|
|
|
package LWP::UserAgent::RedirectNotOk; |
69
|
1
|
|
|
1
|
|
7
|
use base qw(LWP::UserAgent); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
701
|
|
70
|
0
|
0
|
|
0
|
|
0
|
sub redirect_ok { print "Redirecting...\n" if $DEBUG; 0 } |
|
0
|
|
|
|
|
0
|
|
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub new { |
74
|
|
|
|
|
|
|
my ( |
75
|
1
|
|
|
1
|
1
|
765
|
$class, |
76
|
|
|
|
|
|
|
$user_name, |
77
|
|
|
|
|
|
|
$password, |
78
|
|
|
|
|
|
|
$starturl, # from the config file |
79
|
|
|
|
|
|
|
$userfieldnames, # listref same order as values supplied in USER |
80
|
|
|
|
|
|
|
$otherfields, # hash fieldname => value |
81
|
|
|
|
|
|
|
$listre, # field => RE; fields: pageno, num_pages, nextlink, itemurls |
82
|
|
|
|
|
|
|
$itemre, # hash extractfield => RE to get it from "page" |
83
|
|
|
|
|
|
|
$itempostpro, # extractfield => sub returns pairS of field/value |
84
|
|
|
|
|
|
|
$itemurl2id, # sub taking URL, returns unique, persistent item ID |
85
|
|
|
|
|
|
|
$itemformat, # takes item hash, returns email message |
86
|
|
|
|
|
|
|
$messagesize, |
87
|
|
|
|
|
|
|
) = @_; |
88
|
1
|
|
|
|
|
2
|
my $self = {}; |
89
|
1
|
|
|
|
|
5
|
bless $self, $class; |
90
|
1
|
|
|
|
|
3
|
$user_name =~ s#\+# #g; # no spaces allowed in POP3, so "+" instead |
91
|
1
|
|
|
|
|
4
|
my @userfieldvalues = split /:/, $user_name; |
92
|
1
|
|
|
|
|
7
|
$self->{STARTURL} = $starturl; |
93
|
1
|
|
|
|
|
5
|
$self->{FIELDS} = { %$otherfields }; # copy just in case |
94
|
|
|
|
|
|
|
map { |
95
|
3
|
|
|
|
|
11
|
$self->{FIELDS}->{$userfieldnames->[$_]} = $userfieldvalues[$_]; |
96
|
1
|
|
|
|
|
3
|
} 0..$#{$userfieldnames}; |
|
1
|
|
|
|
|
5
|
|
97
|
1
|
|
|
|
|
4
|
$self->{LISTRE} = $listre; |
98
|
1
|
|
|
|
|
3
|
$self->{ITEMRE} = $itemre; |
99
|
1
|
|
|
|
|
3
|
$self->{ITEMPOSTPRO} = $itempostpro; |
100
|
1
|
|
|
|
|
3
|
$self->{ITEMURL2ID} = $itemurl2id; |
101
|
1
|
|
|
|
|
2
|
$self->{ITEMFORMAT} = $itemformat; |
102
|
1
|
|
|
|
|
2
|
$self->{MESSAGESIZE} = $messagesize; |
103
|
1
|
|
|
|
|
2
|
$self->{MESSAGECNT} = 0; |
104
|
1
|
|
|
|
|
3
|
$self->{MSG2OCTETS} = {}; |
105
|
1
|
|
|
|
|
2
|
$self->{MSG2UIDL} = {}; |
106
|
1
|
|
|
|
|
2
|
$self->{MSG2URL} = {}; |
107
|
1
|
|
|
|
|
2
|
$self->{MSG2ITEMDATA} = {}; |
108
|
1
|
|
|
|
|
3
|
$self->{TOTALOCTETS} = 0; |
109
|
1
|
|
|
|
|
3
|
$self->{DELETE} = {}; |
110
|
1
|
|
|
|
|
3
|
$self->{DELMESSAGECNT} = 0; |
111
|
1
|
|
|
|
|
2
|
$self->{DELTOTALOCTETS} = 0; |
112
|
1
|
|
|
|
|
8
|
$self->{CJAR} = HTTP::Cookies->new; |
113
|
1
|
|
|
|
|
27
|
$self->{LIST_LOADED} = 0; |
114
|
1
|
|
|
|
|
30
|
$self; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub lock_acquire { |
118
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
119
|
0
|
|
|
|
|
0
|
1; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub is_valid { |
123
|
4
|
|
|
4
|
1
|
19
|
my ($self, $msg) = @_; |
124
|
4
|
50
|
|
|
|
11
|
$self->_list_messages unless $self->{LIST_LOADED}; |
125
|
4
|
50
|
33
|
|
|
24
|
$msg > 0 and $msg <= $self->{MESSAGECNT} and !$self->is_deleted($msg); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub lock_release { |
129
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
130
|
0
|
|
|
|
|
0
|
1; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub uidl_list { |
134
|
2
|
|
|
2
|
1
|
1949
|
my ($self, $output_fh) = @_; |
135
|
2
|
100
|
|
|
|
12
|
$self->_list_messages unless $self->{LIST_LOADED}; |
136
|
2
|
|
|
|
|
7
|
for (1..$self->{MESSAGECNT}) { |
137
|
6
|
100
|
|
|
|
56
|
if (!$self->is_deleted($_)) { |
138
|
5
|
|
|
|
|
29
|
$output_fh->print("$_ $self->{MSG2UIDL}->{$_}$CRLF"); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
} |
141
|
2
|
|
|
|
|
18
|
$output_fh->print(".$CRLF"); |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
# find relevant info about available messages |
145
|
|
|
|
|
|
|
sub _list_messages { |
146
|
1
|
|
|
1
|
|
2
|
my $self = shift; |
147
|
|
|
|
|
|
|
my ($list_html, $list_url) = get_fill_submit( |
148
|
|
|
|
|
|
|
$self->{CJAR}, |
149
|
|
|
|
|
|
|
$self->{STARTURL}, |
150
|
|
|
|
|
|
|
$self->{FIELDS}, |
151
|
1
|
|
|
|
|
7
|
); |
152
|
1
|
|
|
|
|
5
|
my $list_data = list_parse($list_html, $list_url, $self->{LISTRE}); |
153
|
1
|
|
|
|
|
20
|
my @items; |
154
|
1
|
|
|
|
|
2
|
while (1) { |
155
|
2
|
|
|
|
|
25
|
my @theseitems = @{ $list_data->{itemurls} }; |
|
2
|
|
|
|
|
16
|
|
156
|
2
|
|
|
|
|
6
|
push @items, @theseitems; |
157
|
2
|
100
|
|
|
|
9
|
last if $list_data->{pageno} >= $list_data->{num_pages}; |
158
|
|
|
|
|
|
|
#last if $list_data->{pageno} >= 1; |
159
|
|
|
|
|
|
|
($list_html, $list_url) = redirect_cookie_loop( |
160
|
1
|
|
|
|
|
5
|
$self->{CJAR}, GET($list_data->{nextlink}), |
161
|
|
|
|
|
|
|
); |
162
|
1
|
|
|
|
|
156
|
$list_data = list_parse($list_html, $list_url, $self->{LISTRE}); |
163
|
|
|
|
|
|
|
} |
164
|
1
|
|
|
|
|
3
|
my $cnt = 0; |
165
|
1
|
|
|
|
|
3
|
for my $item (@items) { |
166
|
3
|
|
|
|
|
4
|
$cnt++; |
167
|
3
|
|
|
|
|
6
|
my $octets = $self->{MESSAGESIZE}; |
168
|
3
|
|
|
|
|
59
|
my $id = $self->{ITEMURL2ID}->($item); |
169
|
3
|
|
|
|
|
30
|
$self->{MSG2OCTETS}->{$cnt} = $octets; |
170
|
3
|
|
|
|
|
7
|
$self->{MSG2UIDL}->{$cnt} = $id; |
171
|
3
|
|
|
|
|
6
|
$self->{MSG2URL}->{$cnt} = $item; |
172
|
3
|
|
|
|
|
8
|
$self->{TOTALOCTETS} += $octets; |
173
|
|
|
|
|
|
|
} |
174
|
1
|
|
|
|
|
2
|
$self->{MESSAGECNT} = $cnt; |
175
|
1
|
|
|
|
|
5
|
$self->{LIST_LOADED} = 1; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
sub _get_itemlines { |
179
|
2
|
|
|
2
|
|
5
|
my ($self, $message) = @_; |
180
|
|
|
|
|
|
|
my $data = $self->{MSG2ITEMDATA}->{$message} || |
181
|
|
|
|
|
|
|
($self->{MSG2ITEMDATA}->{$message} = $self->_get_itemdata( |
182
|
2
|
|
66
|
|
|
13
|
$self->{MSG2URL}->{$message}, |
183
|
|
|
|
|
|
|
)); |
184
|
|
|
|
|
|
|
my $text = $self->{ITEMFORMAT}->( |
185
|
|
|
|
|
|
|
$data, |
186
|
2
|
|
|
|
|
8
|
$self->{MSG2UIDL}->{$message}, |
187
|
|
|
|
|
|
|
); |
188
|
|
|
|
|
|
|
# in case formatter wrongly adds \r - EMAIL::STUFFER I'M LOOKING AT YOU |
189
|
2
|
|
|
|
|
4357
|
$text =~ s#\r$##gm; |
190
|
|
|
|
|
|
|
# should truncate it below message size if bigger |
191
|
2
|
|
|
|
|
17
|
$text .= (' ' x ($self->{MESSAGESIZE} - length($text) - 2)) . "\n"; |
192
|
2
|
|
|
|
|
53
|
split /\r*\n/, $text; |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
sub _get_itemdata { |
196
|
1
|
|
|
1
|
|
4
|
my ($self, $url) = @_; |
197
|
1
|
|
|
|
|
5
|
my $request = GET($url); |
198
|
|
|
|
|
|
|
# $request->header('referer', $url); |
199
|
1
|
|
|
|
|
147
|
my ($one_html, $one_url) = redirect_cookie_loop($self->{CJAR}, $request); |
200
|
|
|
|
|
|
|
one_parse( |
201
|
|
|
|
|
|
|
$one_html, |
202
|
|
|
|
|
|
|
$self->{ITEMRE}, |
203
|
|
|
|
|
|
|
$self->{ITEMPOSTPRO}, |
204
|
1
|
|
|
|
|
24
|
); |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
# $message starts at 1 |
208
|
|
|
|
|
|
|
sub retrieve { |
209
|
1
|
|
|
1
|
1
|
1367
|
my ($self, $message, $output_fh, $mbox_destined) = @_; |
210
|
1
|
50
|
|
|
|
5
|
$self->_list_messages unless $self->{LIST_LOADED}; |
211
|
1
|
|
|
|
|
6
|
for ($self->_get_itemlines($message)) { |
212
|
|
|
|
|
|
|
# byte-stuff lines starting with . |
213
|
27
|
50
|
|
|
|
163
|
s/^\./\.\./o unless $mbox_destined; |
214
|
27
|
50
|
|
|
|
55
|
my $line = $mbox_destined ? "$_\n" : "$_$CRLF"; |
215
|
27
|
|
|
|
|
49
|
$output_fh->print($line); |
216
|
|
|
|
|
|
|
} |
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
# $message starts at 1 |
220
|
|
|
|
|
|
|
# returns number of bytes |
221
|
|
|
|
|
|
|
sub top { |
222
|
1
|
|
|
1
|
1
|
559
|
my ($self, $message, $output_fh, $body_lines) = @_; |
223
|
1
|
50
|
|
|
|
6
|
$self->_list_messages unless $self->{LIST_LOADED}; |
224
|
1
|
|
|
|
|
3
|
my $top_bytes = 0; |
225
|
1
|
|
|
|
|
5
|
my @lines = $self->_get_itemlines($message); |
226
|
1
|
|
|
|
|
3
|
my $linecount = 0; |
227
|
|
|
|
|
|
|
# print the headers |
228
|
1
|
|
|
|
|
4
|
while ($linecount < @lines) { |
229
|
9
|
|
|
|
|
20
|
$_ = $lines[$linecount++]; |
230
|
9
|
|
|
|
|
16
|
my $out = "$_$CRLF"; |
231
|
9
|
|
|
|
|
24
|
$output_fh->print($out); |
232
|
9
|
|
|
|
|
62
|
$top_bytes += length($out); |
233
|
9
|
100
|
|
|
|
32
|
last if /^\s*$/; |
234
|
|
|
|
|
|
|
} |
235
|
1
|
|
|
|
|
3
|
my $cnt = 0; |
236
|
|
|
|
|
|
|
# print the TOP arg number of body lines |
237
|
1
|
|
|
|
|
5
|
while ($linecount < @lines) { |
238
|
6
|
|
|
|
|
12
|
$_ = $lines[$linecount++]; |
239
|
6
|
|
|
|
|
8
|
++$cnt; |
240
|
6
|
100
|
|
|
|
12
|
last if $cnt > $body_lines; |
241
|
|
|
|
|
|
|
# byte-stuff lines starting with . |
242
|
5
|
|
|
|
|
9
|
s/^\./\.\./o; |
243
|
5
|
|
|
|
|
12
|
my $out = "$_$CRLF"; |
244
|
5
|
|
|
|
|
11
|
$output_fh->print($out); |
245
|
5
|
|
|
|
|
27
|
$top_bytes += length($out); |
246
|
|
|
|
|
|
|
} |
247
|
1
|
|
|
|
|
6
|
$output_fh->print(".$CRLF"); |
248
|
1
|
|
|
|
|
9
|
$top_bytes; |
249
|
|
|
|
|
|
|
} |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
sub is_deleted { |
252
|
12
|
|
|
12
|
1
|
26
|
my ($self, $message) = @_; |
253
|
12
|
|
|
|
|
43
|
return $self->{DELETE}->{$message}; |
254
|
|
|
|
|
|
|
} |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
sub delete { |
257
|
2
|
|
|
2
|
1
|
6
|
my ($self, $message) = @_; |
258
|
2
|
|
|
|
|
7
|
$self->{DELETE}->{$message} = 1; |
259
|
2
|
|
|
|
|
4
|
$self->{DELMESSAGECNT} += 1; |
260
|
2
|
|
|
|
|
6
|
$self->{DELTOTALOCTETS} += $self->{MSG2OCTETS}->{$message}; |
261
|
|
|
|
|
|
|
} |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
0
|
1
|
|
sub flush_delete { } |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
sub reset { |
266
|
1
|
|
|
1
|
1
|
4
|
my $self = shift; |
267
|
1
|
|
|
|
|
4
|
$self->{DELETE} = {}; |
268
|
1
|
|
|
|
|
2
|
$self->{DELMESSAGECNT} = 0; |
269
|
1
|
|
|
|
|
3
|
$self->{DELTOTALOCTETS} = 0; |
270
|
|
|
|
|
|
|
} |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
sub octets { |
273
|
3
|
|
|
3
|
1
|
688
|
my ($self, $message) = @_; |
274
|
3
|
50
|
|
|
|
9
|
$self->_list_messages unless $self->{LIST_LOADED}; |
275
|
3
|
100
|
|
|
|
10
|
if (defined $message) { |
276
|
1
|
|
|
|
|
8
|
$self->{MSG2OCTETS}->{$message}; |
277
|
|
|
|
|
|
|
} else { |
278
|
2
|
|
|
|
|
14
|
$self->{TOTALOCTETS} - $self->{DELTOTALOCTETS}; |
279
|
|
|
|
|
|
|
} |
280
|
|
|
|
|
|
|
} |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
sub messages { |
283
|
2
|
|
|
2
|
1
|
208
|
my ($self) = @_; |
284
|
2
|
50
|
|
|
|
8
|
$self->_list_messages unless $self->{LIST_LOADED}; |
285
|
2
|
|
|
|
|
7
|
$self->{MESSAGECNT} - $self->{DELMESSAGECNT}; |
286
|
|
|
|
|
|
|
} |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
sub uidl { |
289
|
1
|
|
|
1
|
1
|
3
|
my ($self, $message) = @_; |
290
|
1
|
50
|
|
|
|
5
|
$self->_list_messages unless $self->{LIST_LOADED}; |
291
|
1
|
|
|
|
|
6
|
$self->{MSG2UIDL}->{$message}; |
292
|
|
|
|
|
|
|
} |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
sub get_fill_submit { |
295
|
1
|
|
|
1
|
1
|
2
|
my ($cjar, $url, $vars, $varnamechange) = @_; |
296
|
1
|
|
|
|
|
5
|
my ($html, $real_url) = redirect_cookie_loop($cjar, GET($url)); |
297
|
1
|
|
|
|
|
7489
|
parse_fill_submit($cjar, $html, $real_url, $vars, $varnamechange); |
298
|
|
|
|
|
|
|
} |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
sub parse_fill_submit { |
301
|
1
|
|
|
1
|
1
|
4
|
my ($cjar, $html, $real_url, $vars, $varnamechange) = @_; |
302
|
1
|
|
|
|
|
9
|
my $form = HTML::Form->parse($html, $real_url); |
303
|
|
|
|
|
|
|
map { |
304
|
1
|
|
|
|
|
5785
|
$form->value($_, $vars->{$_}); |
|
7
|
|
|
|
|
575
|
|
305
|
|
|
|
|
|
|
} keys %$vars; |
306
|
1
|
|
|
|
|
114
|
$formno++; |
307
|
1
|
50
|
|
|
|
5
|
to_file("f$formno.wri", Data::Dumper::Dumper($varnamechange, $form)) if $DEBUG; |
308
|
|
|
|
|
|
|
map { |
309
|
1
|
50
|
|
|
|
2
|
my $input = $form->find_input(undef, undef, $_); |
|
0
|
|
|
|
|
0
|
|
310
|
0
|
|
|
|
|
0
|
$input->name($varnamechange->{$_}); |
311
|
0
|
|
|
|
|
0
|
local $^W = 0; # don't want to hear about "readonly" |
312
|
0
|
0
|
|
|
|
0
|
$input->value('') unless defined $input->value; |
313
|
|
|
|
|
|
|
} keys %$varnamechange |
314
|
|
|
|
|
|
|
if $varnamechange; |
315
|
1
|
50
|
|
|
|
3
|
to_file("f$formno-after.wri", Data::Dumper::Dumper($varnamechange, $form)) if $DEBUG; |
316
|
1
|
|
|
|
|
2
|
my $form_html; |
317
|
1
|
|
|
|
|
5
|
($form_html, $real_url) = redirect_cookie_loop( |
318
|
|
|
|
|
|
|
$cjar, |
319
|
|
|
|
|
|
|
$form->click, |
320
|
|
|
|
|
|
|
); |
321
|
1
|
|
|
|
|
1823
|
($form_html, $real_url); |
322
|
|
|
|
|
|
|
} |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
# special case - nextlink value will be absolutised |
325
|
|
|
|
|
|
|
# special case - itemurls value will be listref of absolutised values |
326
|
|
|
|
|
|
|
sub list_parse { |
327
|
2
|
|
|
2
|
1
|
6
|
my ($text, $pageurl, $listre) = @_; |
328
|
2
|
|
|
|
|
3
|
my %list; |
329
|
2
|
|
|
|
|
7
|
for my $key (keys %$listre) { |
330
|
8
|
100
|
|
|
|
22
|
if ($key eq 'itemurls') { |
331
|
|
|
|
|
|
|
my @hits = map { |
332
|
2
|
|
|
|
|
59
|
URI::URL->new($_, $pageurl)->abs->as_string; |
|
3
|
|
|
|
|
710
|
|
333
|
|
|
|
|
|
|
} $text =~ m#$listre->{$key}#gsi; |
334
|
2
|
|
|
|
|
1127
|
$list{$key} = \@hits; |
335
|
|
|
|
|
|
|
} else { |
336
|
6
|
|
|
|
|
147
|
my ($match) = $text =~ m#$listre->{$key}#si; |
337
|
6
|
|
|
|
|
33
|
$list{$key} = decode_entities($match); |
338
|
|
|
|
|
|
|
} |
339
|
|
|
|
|
|
|
} |
340
|
2
|
|
|
|
|
12
|
$list{nextlink} = URI::URL->new($list{nextlink}, $pageurl)->abs->as_string; |
341
|
2
|
|
|
|
|
1217
|
\%list; |
342
|
|
|
|
|
|
|
} |
343
|
|
|
|
|
|
|
|
344
|
|
|
|
|
|
|
sub one_parse { |
345
|
1
|
|
|
1
|
1
|
3
|
my ($text, $scrapespec, $scrapepostpro) = @_; |
346
|
1
|
|
|
|
|
2
|
my %item; |
347
|
1
|
|
|
|
|
7
|
for my $key (keys %$scrapespec) { |
348
|
15
|
|
|
|
|
405
|
my ($match) = $text =~ m#$scrapespec->{$key}#si; |
349
|
15
|
100
|
|
|
|
45
|
$match = '' unless defined $match; |
350
|
15
|
|
|
|
|
55
|
$item{$key} = decode_entities($match); |
351
|
|
|
|
|
|
|
} |
352
|
|
|
|
|
|
|
# postpro - sub that returns list of key => value |
353
|
|
|
|
|
|
|
# might be more than one pair |
354
|
1
|
|
|
|
|
5
|
for my $key (keys %$scrapepostpro) { |
355
|
4
|
|
|
|
|
24
|
my %ret = $scrapepostpro->{$key}->($key, $item{$key}); |
356
|
4
|
|
|
|
|
37
|
map { $item{$_} = $ret{$_} } keys %ret; |
|
4
|
|
|
|
|
12
|
|
357
|
|
|
|
|
|
|
} |
358
|
1
|
|
|
|
|
11
|
\%item; |
359
|
|
|
|
|
|
|
} |
360
|
|
|
|
|
|
|
|
361
|
|
|
|
|
|
|
# modify input $cjar, return also a $response |
362
|
|
|
|
|
|
|
sub redirect_cookie_loop { |
363
|
|
|
|
|
|
|
my ($cjar, $request) = @_; |
364
|
|
|
|
|
|
|
# otherwise cookies set during redirects get lost... |
365
|
|
|
|
|
|
|
my $ua = LWP::UserAgent::RedirectNotOk->new; |
366
|
|
|
|
|
|
|
$ua->agent('Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)'); |
367
|
|
|
|
|
|
|
my $response; |
368
|
|
|
|
|
|
|
while (1) { |
369
|
|
|
|
|
|
|
$req_count++; |
370
|
|
|
|
|
|
|
$cjar->add_cookie_header($request); |
371
|
|
|
|
|
|
|
print "req $req_count: ", $request->uri, "\n" if $DEBUG; |
372
|
|
|
|
|
|
|
to_file("r${req_count}req.wri", $request->as_string) if $DEBUG; |
373
|
|
|
|
|
|
|
$response = $ua->request($request); |
374
|
|
|
|
|
|
|
to_file("r${req_count}resp.wri", $response->as_string) if $DEBUG; |
375
|
|
|
|
|
|
|
unless ($response->is_success or $response->is_redirect) { |
376
|
|
|
|
|
|
|
my $text = $response->error_as_HTML; |
377
|
|
|
|
|
|
|
$text =~ s/\s+$//; |
378
|
|
|
|
|
|
|
die "Request: ".$request->as_string."\nFailed: $text\n"; |
379
|
|
|
|
|
|
|
} |
380
|
|
|
|
|
|
|
$cjar->extract_cookies($response); |
381
|
|
|
|
|
|
|
my $new_loc; |
382
|
|
|
|
|
|
|
if ($response->is_redirect) { |
383
|
|
|
|
|
|
|
#print "302\n"; |
384
|
|
|
|
|
|
|
$new_loc = $response->header('location'); |
385
|
|
|
|
|
|
|
} elsif ($response->header('refresh')) { |
386
|
|
|
|
|
|
|
#print "refresh\n"; |
387
|
|
|
|
|
|
|
$new_loc = parse_refresh($response->header('refresh')); |
388
|
|
|
|
|
|
|
} else { |
389
|
|
|
|
|
|
|
last; |
390
|
|
|
|
|
|
|
} |
391
|
|
|
|
|
|
|
#use Data::Dumper; print Dumper($response); |
392
|
|
|
|
|
|
|
$request = GET(URI::URL->new($new_loc, $request->uri)->abs->as_string); |
393
|
|
|
|
|
|
|
} |
394
|
|
|
|
|
|
|
($response->content, $response->request->uri->as_string); |
395
|
|
|
|
|
|
|
} |
396
|
|
|
|
|
|
|
|
397
|
|
|
|
|
|
|
sub parse_refresh { |
398
|
0
|
|
|
0
|
1
|
|
my $header_val = shift; |
399
|
0
|
|
|
|
|
|
my ($url) = $header_val =~ m#url=['"]?([^'"\s]*)#i; |
400
|
0
|
|
|
|
|
|
$url; |
401
|
|
|
|
|
|
|
} |
402
|
|
|
|
|
|
|
|
403
|
|
|
|
|
|
|
1; |
404
|
|
|
|
|
|
|
|
405
|
|
|
|
|
|
|
__END__ |