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
|
|
1427
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
53
|
1
|
|
|
1
|
|
568
|
use HTML::Entities; |
|
1
|
|
|
|
|
5999
|
|
|
1
|
|
|
|
|
74
|
|
54
|
1
|
|
|
1
|
|
639
|
use HTML::Form; |
|
1
|
|
|
|
|
12156
|
|
|
1
|
|
|
|
|
35
|
|
55
|
1
|
|
|
1
|
|
612
|
use HTTP::Cookies; |
|
1
|
|
|
|
|
12089
|
|
|
1
|
|
|
|
|
40
|
|
56
|
1
|
|
|
1
|
|
913
|
use HTTP::Request::Common; |
|
1
|
|
|
|
|
15360
|
|
|
1
|
|
|
|
|
82
|
|
57
|
1
|
|
|
1
|
|
484
|
use URI::URL; |
|
1
|
|
|
|
|
3001
|
|
|
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
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
748
|
|
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
|
696
|
$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
|
|
|
|
|
3
|
my $self = {}; |
89
|
1
|
|
|
|
|
2
|
bless $self, $class; |
90
|
1
|
|
|
|
|
3
|
$user_name =~ s#\+# #g; # no spaces allowed in POP3, so "+" instead |
91
|
1
|
|
|
|
|
6
|
my @userfieldvalues = split /:/, $user_name; |
92
|
1
|
|
|
|
|
7
|
$self->{STARTURL} = $starturl; |
93
|
1
|
|
|
|
|
6
|
$self->{FIELDS} = { %$otherfields }; # copy just in case |
94
|
|
|
|
|
|
|
map { |
95
|
3
|
|
|
|
|
10
|
$self->{FIELDS}->{$userfieldnames->[$_]} = $userfieldvalues[$_]; |
96
|
1
|
|
|
|
|
2
|
} 0..$#{$userfieldnames}; |
|
1
|
|
|
|
|
4
|
|
97
|
1
|
|
|
|
|
3
|
$self->{LISTRE} = $listre; |
98
|
1
|
|
|
|
|
2
|
$self->{ITEMRE} = $itemre; |
99
|
1
|
|
|
|
|
2
|
$self->{ITEMPOSTPRO} = $itempostpro; |
100
|
1
|
|
|
|
|
2
|
$self->{ITEMURL2ID} = $itemurl2id; |
101
|
1
|
|
|
|
|
2
|
$self->{ITEMFORMAT} = $itemformat; |
102
|
1
|
|
|
|
|
3
|
$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
|
|
|
|
|
2
|
$self->{DELETE} = {}; |
110
|
1
|
|
|
|
|
3
|
$self->{DELMESSAGECNT} = 0; |
111
|
1
|
|
|
|
|
2
|
$self->{DELTOTALOCTETS} = 0; |
112
|
1
|
|
|
|
|
9
|
$self->{CJAR} = HTTP::Cookies->new; |
113
|
1
|
|
|
|
|
30
|
$self->{LIST_LOADED} = 0; |
114
|
1
|
|
|
|
|
26
|
$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
|
|
|
|
10
|
$self->_list_messages unless $self->{LIST_LOADED}; |
125
|
4
|
50
|
33
|
|
|
25
|
$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
|
1936
|
my ($self, $output_fh) = @_; |
135
|
2
|
100
|
|
|
|
10
|
$self->_list_messages unless $self->{LIST_LOADED}; |
136
|
2
|
|
|
|
|
7
|
for (1..$self->{MESSAGECNT}) { |
137
|
6
|
100
|
|
|
|
53
|
if (!$self->is_deleted($_)) { |
138
|
5
|
|
|
|
|
28
|
$output_fh->print("$_ $self->{MSG2UIDL}->{$_}$CRLF"); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
} |
141
|
2
|
|
|
|
|
17
|
$output_fh->print(".$CRLF"); |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
# find relevant info about available messages |
145
|
|
|
|
|
|
|
sub _list_messages { |
146
|
1
|
|
|
1
|
|
3
|
my $self = shift; |
147
|
|
|
|
|
|
|
my ($list_html, $list_url) = get_fill_submit( |
148
|
|
|
|
|
|
|
$self->{CJAR}, |
149
|
|
|
|
|
|
|
$self->{STARTURL}, |
150
|
|
|
|
|
|
|
$self->{FIELDS}, |
151
|
1
|
|
|
|
|
8
|
); |
152
|
1
|
|
|
|
|
4
|
my $list_data = list_parse($list_html, $list_url, $self->{LISTRE}); |
153
|
1
|
|
|
|
|
20
|
my @items; |
154
|
1
|
|
|
|
|
2
|
while (1) { |
155
|
2
|
|
|
|
|
24
|
my @theseitems = @{ $list_data->{itemurls} }; |
|
2
|
|
|
|
|
6
|
|
156
|
2
|
|
|
|
|
4
|
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
|
|
|
|
|
158
|
$list_data = list_parse($list_html, $list_url, $self->{LISTRE}); |
163
|
|
|
|
|
|
|
} |
164
|
1
|
|
|
|
|
4
|
my $cnt = 0; |
165
|
1
|
|
|
|
|
4
|
for my $item (@items) { |
166
|
3
|
|
|
|
|
4
|
$cnt++; |
167
|
3
|
|
|
|
|
6
|
my $octets = $self->{MESSAGESIZE}; |
168
|
3
|
|
|
|
|
7
|
my $id = $self->{ITEMURL2ID}->($item); |
169
|
3
|
|
|
|
|
29
|
$self->{MSG2OCTETS}->{$cnt} = $octets; |
170
|
3
|
|
|
|
|
6
|
$self->{MSG2UIDL}->{$cnt} = $id; |
171
|
3
|
|
|
|
|
6
|
$self->{MSG2URL}->{$cnt} = $item; |
172
|
3
|
|
|
|
|
10
|
$self->{TOTALOCTETS} += $octets; |
173
|
|
|
|
|
|
|
} |
174
|
1
|
|
|
|
|
10
|
$self->{MESSAGECNT} = $cnt; |
175
|
1
|
|
|
|
|
4
|
$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
|
|
|
9
|
$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
|
|
|
|
|
4968
|
$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
|
|
|
|
|
51
|
split /\r*\n/, $text; |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
sub _get_itemdata { |
196
|
1
|
|
|
1
|
|
3
|
my ($self, $url) = @_; |
197
|
1
|
|
|
|
|
4
|
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
|
1433
|
my ($self, $message, $output_fh, $mbox_destined) = @_; |
210
|
1
|
50
|
|
|
|
6
|
$self->_list_messages unless $self->{LIST_LOADED}; |
211
|
1
|
|
|
|
|
4
|
for ($self->_get_itemlines($message)) { |
212
|
|
|
|
|
|
|
# byte-stuff lines starting with . |
213
|
27
|
50
|
|
|
|
166
|
s/^\./\.\./o unless $mbox_destined; |
214
|
27
|
50
|
|
|
|
53
|
my $line = $mbox_destined ? "$_\n" : "$_$CRLF"; |
215
|
27
|
|
|
|
|
53
|
$output_fh->print($line); |
216
|
|
|
|
|
|
|
} |
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
# $message starts at 1 |
220
|
|
|
|
|
|
|
# returns number of bytes |
221
|
|
|
|
|
|
|
sub top { |
222
|
1
|
|
|
1
|
1
|
577
|
my ($self, $message, $output_fh, $body_lines) = @_; |
223
|
1
|
50
|
|
|
|
6
|
$self->_list_messages unless $self->{LIST_LOADED}; |
224
|
1
|
|
|
|
|
2
|
my $top_bytes = 0; |
225
|
1
|
|
|
|
|
4
|
my @lines = $self->_get_itemlines($message); |
226
|
1
|
|
|
|
|
3
|
my $linecount = 0; |
227
|
|
|
|
|
|
|
# print the headers |
228
|
1
|
|
|
|
|
4
|
while ($linecount < @lines) { |
229
|
9
|
|
|
|
|
18
|
$_ = $lines[$linecount++]; |
230
|
9
|
|
|
|
|
16
|
my $out = "$_$CRLF"; |
231
|
9
|
|
|
|
|
24
|
$output_fh->print($out); |
232
|
9
|
|
|
|
|
66
|
$top_bytes += length($out); |
233
|
9
|
100
|
|
|
|
33
|
last if /^\s*$/; |
234
|
|
|
|
|
|
|
} |
235
|
1
|
|
|
|
|
2
|
my $cnt = 0; |
236
|
|
|
|
|
|
|
# print the TOP arg number of body lines |
237
|
1
|
|
|
|
|
4
|
while ($linecount < @lines) { |
238
|
6
|
|
|
|
|
11
|
$_ = $lines[$linecount++]; |
239
|
6
|
|
|
|
|
9
|
++$cnt; |
240
|
6
|
100
|
|
|
|
11
|
last if $cnt > $body_lines; |
241
|
|
|
|
|
|
|
# byte-stuff lines starting with . |
242
|
5
|
|
|
|
|
10
|
s/^\./\.\./o; |
243
|
5
|
|
|
|
|
9
|
my $out = "$_$CRLF"; |
244
|
5
|
|
|
|
|
12
|
$output_fh->print($out); |
245
|
5
|
|
|
|
|
29
|
$top_bytes += length($out); |
246
|
|
|
|
|
|
|
} |
247
|
1
|
|
|
|
|
5
|
$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
|
|
|
|
|
49
|
return $self->{DELETE}->{$message}; |
254
|
|
|
|
|
|
|
} |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
sub delete { |
257
|
2
|
|
|
2
|
1
|
6
|
my ($self, $message) = @_; |
258
|
2
|
|
|
|
|
5
|
$self->{DELETE}->{$message} = 1; |
259
|
2
|
|
|
|
|
4
|
$self->{DELMESSAGECNT} += 1; |
260
|
2
|
|
|
|
|
5
|
$self->{DELTOTALOCTETS} += $self->{MSG2OCTETS}->{$message}; |
261
|
|
|
|
|
|
|
} |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
0
|
1
|
|
sub flush_delete { } |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
sub reset { |
266
|
1
|
|
|
1
|
1
|
2
|
my $self = shift; |
267
|
1
|
|
|
|
|
4
|
$self->{DELETE} = {}; |
268
|
1
|
|
|
|
|
3
|
$self->{DELMESSAGECNT} = 0; |
269
|
1
|
|
|
|
|
3
|
$self->{DELTOTALOCTETS} = 0; |
270
|
|
|
|
|
|
|
} |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
sub octets { |
273
|
3
|
|
|
3
|
1
|
747
|
my ($self, $message) = @_; |
274
|
3
|
50
|
|
|
|
10
|
$self->_list_messages unless $self->{LIST_LOADED}; |
275
|
3
|
100
|
|
|
|
7
|
if (defined $message) { |
276
|
1
|
|
|
|
|
8
|
$self->{MSG2OCTETS}->{$message}; |
277
|
|
|
|
|
|
|
} else { |
278
|
2
|
|
|
|
|
24
|
$self->{TOTALOCTETS} - $self->{DELTOTALOCTETS}; |
279
|
|
|
|
|
|
|
} |
280
|
|
|
|
|
|
|
} |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
sub messages { |
283
|
2
|
|
|
2
|
1
|
202
|
my ($self) = @_; |
284
|
2
|
50
|
|
|
|
10
|
$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
|
|
|
|
|
22
|
$self->{MSG2UIDL}->{$message}; |
292
|
|
|
|
|
|
|
} |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
sub get_fill_submit { |
295
|
1
|
|
|
1
|
1
|
3
|
my ($cjar, $url, $vars, $varnamechange) = @_; |
296
|
1
|
|
|
|
|
5
|
my ($html, $real_url) = redirect_cookie_loop($cjar, GET($url)); |
297
|
1
|
|
|
|
|
7810
|
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
|
|
|
|
|
6030
|
$form->value($_, $vars->{$_}); |
|
7
|
|
|
|
|
576
|
|
305
|
|
|
|
|
|
|
} keys %$vars; |
306
|
1
|
|
|
|
|
115
|
$formno++; |
307
|
1
|
50
|
|
|
|
4
|
to_file("f$formno.wri", Data::Dumper::Dumper($varnamechange, $form)) if $DEBUG; |
308
|
|
|
|
|
|
|
map { |
309
|
1
|
50
|
|
|
|
4
|
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
|
|
|
|
|
1699
|
($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
|
7
|
my ($text, $pageurl, $listre) = @_; |
328
|
2
|
|
|
|
|
3
|
my %list; |
329
|
2
|
|
|
|
|
7
|
for my $key (keys %$listre) { |
330
|
8
|
100
|
|
|
|
24
|
if ($key eq 'itemurls') { |
331
|
|
|
|
|
|
|
my @hits = map { |
332
|
2
|
|
|
|
|
39
|
URI::URL->new($_, $pageurl)->abs->as_string; |
|
3
|
|
|
|
|
781
|
|
333
|
|
|
|
|
|
|
} $text =~ m#$listre->{$key}#gsi; |
334
|
2
|
|
|
|
|
1118
|
$list{$key} = \@hits; |
335
|
|
|
|
|
|
|
} else { |
336
|
6
|
|
|
|
|
197
|
my ($match) = $text =~ m#$listre->{$key}#si; |
337
|
6
|
|
|
|
|
45
|
$list{$key} = decode_entities($match); |
338
|
|
|
|
|
|
|
} |
339
|
|
|
|
|
|
|
} |
340
|
2
|
|
|
|
|
11
|
$list{nextlink} = URI::URL->new($list{nextlink}, $pageurl)->abs->as_string; |
341
|
2
|
|
|
|
|
1266
|
\%list; |
342
|
|
|
|
|
|
|
} |
343
|
|
|
|
|
|
|
|
344
|
|
|
|
|
|
|
sub one_parse { |
345
|
1
|
|
|
1
|
1
|
4
|
my ($text, $scrapespec, $scrapepostpro) = @_; |
346
|
1
|
|
|
|
|
2
|
my %item; |
347
|
1
|
|
|
|
|
8
|
for my $key (keys %$scrapespec) { |
348
|
15
|
|
|
|
|
430
|
my ($match) = $text =~ m#$scrapespec->{$key}#si; |
349
|
15
|
100
|
|
|
|
46
|
$match = '' unless defined $match; |
350
|
15
|
|
|
|
|
60
|
$item{$key} = decode_entities($match); |
351
|
|
|
|
|
|
|
} |
352
|
|
|
|
|
|
|
# postpro - sub that returns list of key => value |
353
|
|
|
|
|
|
|
# might be more than one pair |
354
|
1
|
|
|
|
|
10
|
for my $key (keys %$scrapepostpro) { |
355
|
4
|
|
|
|
|
15
|
my %ret = $scrapepostpro->{$key}->($key, $item{$key}); |
356
|
4
|
|
|
|
|
34
|
map { $item{$_} = $ret{$_} } keys %ret; |
|
4
|
|
|
|
|
13
|
|
357
|
|
|
|
|
|
|
} |
358
|
1
|
|
|
|
|
10
|
\%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__ |