POP3/Folder/webscrape.pm

filter: Statementstmt 93.3% 183 / 196 Branchbran 62.0% 31 / 50 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 83.3% 25 / 30 Podpod 100% 19 / 19 totaltotal 85.5% 261 / 305 CC 33 SCAR 12.6
CC
33
Cov
86%
parse_fill_submit
6.5
top
6.0
redirect_ok
6.0
CRAP 3.5
« POP3/Security/User.pm ↑ Summary POP3/Folder/mbox/parse_to_disk.pm »
1 package Mail::POP3::Folder::webscrape;
2  
3 1 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 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 1 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use HTML::Form;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶use HTTP::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 1 ▶use URI::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 1 my $formno = 0; # form_fill
60 # this is at top so $DEBUG in L::UA::RNOk is correct one!
61 1 our $DEBUG = 0; # form_fill, redirect_cookie_loop et al
62 1 my $req_count = 0; # redirect_cookie_loop et al
63 1 ▶require Data::Dumper if $DEBUG; # form_fill
Branch: 1 branch
BranchTrueFalseTotal
if $Mail::POP3::Folder::webscrape::DEBUG 0 1 1
64 1 my $CRLF = "\015\012";
65  
66 {
67 # redirect_cookie_loop et al
68 package LWP::UserAgent::RedirectNotOk;
69 1 ▶ use base qw(LWP::UserAgent);
Subroutine BEGIN: called CC=1 SCAR=0.0
70 0 ▶ sub redirect_ok { print "Redirecting...\n" if $DEBUG; 0 }
Subroutine redirect_ok: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $Mail::POP3::Folder::webscrape::DEBUG 0 0 0
71 }
72  
73 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
74 my (
75 1 $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 my $self = {};
89 1 bless $self, $class;
90 1 $user_name =~ s#\+# #g; # no spaces allowed in POP3, so "+" instead
91 1 my @userfieldvalues = split /:/, $user_name;
92 1 $self->{STARTURL} = $starturl;
93 1 $self->{FIELDS} = { %$otherfields }; # copy just in case
94 map {
95 3 $self->{FIELDS}->{$userfieldnames->[$_]} = $userfieldvalues[$_];
96 1 } 0..$#{$userfieldnames};
97 1 $self->{LISTRE} = $listre;
98 1 $self->{ITEMRE} = $itemre;
99 1 $self->{ITEMPOSTPRO} = $itempostpro;
100 1 $self->{ITEMURL2ID} = $itemurl2id;
101 1 $self->{ITEMFORMAT} = $itemformat;
102 1 $self->{MESSAGESIZE} = $messagesize;
103 1 $self->{MESSAGECNT} = 0;
104 1 $self->{MSG2OCTETS} = {};
105 1 $self->{MSG2UIDL} = {};
106 1 $self->{MSG2URL} = {};
107 1 $self->{MSG2ITEMDATA} = {};
108 1 $self->{TOTALOCTETS} = 0;
109 1 $self->{DELETE} = {};
110 1 $self->{DELMESSAGECNT} = 0;
111 1 $self->{DELTOTALOCTETS} = 0;
112 1 $self->{CJAR} = HTTP::Cookies->new;
113 1 $self->{LIST_LOADED} = 0;
114 1 $self;
115 }
116  
117 0 ▶sub lock_acquire {
Subroutine lock_acquire: not called CC=1 SCAR=6.9
Pod documented
118 0 my $self = shift;
119 0 1;
120 }
121  
122 4 ▶sub is_valid {
Subroutine is_valid: called CC=4 SCAR=16.1
Pod documented
123 4 my ($self, $msg) = @_;
124 4 ▶ $self->_list_messages unless $self->{LIST_LOADED};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"LIST_LOADED"} 0 4 4
125 4 ▶ $msg > 0 and $msg <= $self->{MESSAGECNT} and !$self->is_deleted($msg);
Branch: 1 branch
BranchTrueFalseTotal
$msg > 0 and $msg <= $self->{"MESSAGECNT"} and not $self->is_deleted($msg) 4 0 4
Condition33%1/3
$msg > 0 and $msg <= $self->{"MESSAGECNT"}
!ll&&!rl&&r
004
MC/DC0%0/2
$msg > 0 and $msg <= $self->{"MESSAGECNT"}
$msg > 0 $msg <= $self->{"MESSAGECNT"}
Truth table33%1/3
A and B
A= $msg > 0B= $msg <= $self->{"MESSAGECNT"}
ABresult
0X0
100
111
126 }
127  
128 0 ▶sub lock_release {
Subroutine lock_release: not called CC=1 SCAR=6.9
Pod documented
129 0 my $self = shift;
130 0 1;
131 }
132  
133 2 ▶sub uidl_list {
Subroutine uidl_list: called CC=4 SCAR=13.9
Pod documented
134 2 my ($self, $output_fh) = @_;
135 2 ▶ $self->_list_messages unless $self->{LIST_LOADED};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"LIST_LOADED"} 1 1 2
136 2 for (1..$self->{MESSAGECNT}) {
137 6 ▶ if (!$self->is_deleted($_)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_deleted($_)) 5 1 6
138 5 $output_fh->print("$_ $self->{MSG2UIDL}->{$_}$CRLF");
139 }
140 }
141 2 $output_fh->print(".$CRLF");
142 }
143  
144 # find relevant info about available messages
145 1 ▶sub _list_messages {
Subroutine _list_messages: called CC=3 SCAR=11.0
146 1 my $self = shift;
147 my ($list_html, $list_url) = get_fill_submit(
148 $self->{CJAR},
149 $self->{STARTURL},
150 $self->{FIELDS},
151 1 );
152 1 my $list_data = list_parse($list_html, $list_url, $self->{LISTRE});
153 1 my @items;
154 1 while (1) {
155 2 my @theseitems = @{ $list_data->{itemurls} };
156 2 push @items, @theseitems;
157 2 ▶ last if $list_data->{pageno} >= $list_data->{num_pages};
Branch: 1 branch
BranchTrueFalseTotal
if $list_data->{"pageno"} >= $list_data->{"num_pages"} 1 1 2
158 #last if $list_data->{pageno} >= 1;
159 ($list_html, $list_url) = redirect_cookie_loop(
160 1 $self->{CJAR}, GET($list_data->{nextlink}),
161 );
162 1 $list_data = list_parse($list_html, $list_url, $self->{LISTRE});
163 }
164 1 my $cnt = 0;
165 1 for my $item (@items) {
166 3 $cnt++;
167 3 my $octets = $self->{MESSAGESIZE};
168 3 my $id = $self->{ITEMURL2ID}->($item);
169 3 $self->{MSG2OCTETS}->{$cnt} = $octets;
170 3 $self->{MSG2UIDL}->{$cnt} = $id;
171 3 $self->{MSG2URL}->{$cnt} = $item;
172 3 $self->{TOTALOCTETS} += $octets;
173 }
174 1 $self->{MESSAGECNT} = $cnt;
175 1 $self->{LIST_LOADED} = 1;
176 }
177  
178 2 ▶sub _get_itemlines {
Subroutine _get_itemlines: called CC=2 SCAR=7.0
179 2 my ($self, $message) = @_;
180 my $data = $self->{MSG2ITEMDATA}->{$message} ||
181 ($self->{MSG2ITEMDATA}->{$message} = $self->_get_itemdata(
182 2 ▶ $self->{MSG2URL}->{$message},
Condition66%2/3
$self->{'MSG2ITEMDATA'}{$message} || ($self->{'MSG2ITEMDATA'}{$message} = $self->_get_itemdata($self->{'MSG2URL'}{$message}))
l!l&&r!l&&!r
110
MC/DC0%0/2
$self->{'MSG2ITEMDATA'}{$message} || ($self->{'MSG2ITEMDATA'}{$message} = $self->_get_itemdata($self->{'MSG2URL'}{$message}))
$self->{'MSG2ITEMDATA'}{$message} ($self->{'MSG2ITEMDATA'}{$message} = $self->_get_itemdata($self->{'MSG2URL'}{$message}))
Truth table66%2/3
A || B
A= $self->{'MSG2ITEMDATA'}{$message}B= ($self->{'MSG2ITEMDATA'}{$message} = $self->_get_itemdata($self->{'MSG2URL'}{$message}))
ABresult
1X1
011
000
183 ));
184 my $text = $self->{ITEMFORMAT}->(
185 $data,
186 2 $self->{MSG2UIDL}->{$message},
187 );
188 # in case formatter wrongly adds \r - EMAIL::STUFFER I'M LOOKING AT YOU
189 2 $text =~ s#\r$##gm;
190 # should truncate it below message size if bigger
191 2 $text .= (' ' x ($self->{MESSAGESIZE} - length($text) - 2)) . "\n";
192 2 split /\r*\n/, $text;
193 }
194  
195 1 ▶sub _get_itemdata {
Subroutine _get_itemdata: called CC=1 SCAR=0.0
196 1 my ($self, $url) = @_;
197 1 my $request = GET($url);
198 # $request->header('referer', $url);
199 1 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 );
205 }
206  
207 # $message starts at 1
208 1 ▶sub retrieve {
Subroutine retrieve: called CC=5 SCAR=16.8
Pod documented
209 1 my ($self, $message, $output_fh, $mbox_destined) = @_;
210 1 ▶ $self->_list_messages unless $self->{LIST_LOADED};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"LIST_LOADED"} 0 1 1
211 1 for ($self->_get_itemlines($message)) {
212 # byte-stuff lines starting with .
213 27 ▶ s/^\./\.\./o unless $mbox_destined;
Branch: 1 branch
BranchTrueFalseTotal
unless $mbox_destined 27 0 27
214 27 ▶ my $line = $mbox_destined ? "$_\n" : "$_$CRLF";
Branch: 1 branch
BranchTrueFalseTotal
$mbox_destined ? : 0 27 27
215 27 $output_fh->print($line);
216 }
217 }
218  
219 # $message starts at 1
220 # returns number of bytes
221 1 ▶sub top {
Subroutine top: called CC=6 SCAR=18.0
Pod documented
222 1 my ($self, $message, $output_fh, $body_lines) = @_;
223 1 ▶ $self->_list_messages unless $self->{LIST_LOADED};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"LIST_LOADED"} 0 1 1
224 1 my $top_bytes = 0;
225 1 my @lines = $self->_get_itemlines($message);
226 1 my $linecount = 0;
227 # print the headers
228 1 ▶ while ($linecount < @lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($linecount < @lines) 9 0 9
229 9 $_ = $lines[$linecount++];
230 9 my $out = "$_$CRLF";
231 9 $output_fh->print($out);
232 9 $top_bytes += length($out);
233 9 ▶ last if /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 1 8 9
234 }
235 1 my $cnt = 0;
236 # print the TOP arg number of body lines
237 1 ▶ while ($linecount < @lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($linecount < @lines) 6 0 6
238 6 $_ = $lines[$linecount++];
239 6 ++$cnt;
240 6 ▶ last if $cnt > $body_lines;
Branch: 1 branch
BranchTrueFalseTotal
if $cnt > $body_lines 1 5 6
241 # byte-stuff lines starting with .
242 5 s/^\./\.\./o;
243 5 my $out = "$_$CRLF";
244 5 $output_fh->print($out);
245 5 $top_bytes += length($out);
246 }
247 1 $output_fh->print(".$CRLF");
248 1 $top_bytes;
249 }
250  
251 12 ▶sub is_deleted {
Subroutine is_deleted: called CC=1 SCAR=0.0
Pod documented
252 12 my ($self, $message) = @_;
253 12 return $self->{DELETE}->{$message};
254 }
255  
256 2 ▶sub delete {
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
257 2 my ($self, $message) = @_;
258 2 $self->{DELETE}->{$message} = 1;
259 2 $self->{DELMESSAGECNT} += 1;
260 2 $self->{DELTOTALOCTETS} += $self->{MSG2OCTETS}->{$message};
261 }
262  
263 0 ▶sub flush_delete { }
Subroutine flush_delete: not called CC=1 SCAR=0.0
Pod documented
264  
265 1 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
266 1 my $self = shift;
267 1 $self->{DELETE} = {};
268 1 $self->{DELMESSAGECNT} = 0;
269 1 $self->{DELTOTALOCTETS} = 0;
270 }
271  
272 3 ▶sub octets {
Subroutine octets: called CC=3 SCAR=11.0
Pod documented
273 3 my ($self, $message) = @_;
274 3 ▶ $self->_list_messages unless $self->{LIST_LOADED};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"LIST_LOADED"} 0 3 3
275 3 ▶ if (defined $message) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $message) { } 1 2 3
276 1 $self->{MSG2OCTETS}->{$message};
277 } else {
278 2 $self->{TOTALOCTETS} - $self->{DELTOTALOCTETS};
279 }
280 }
281  
282 2 ▶sub messages {
Subroutine messages: called CC=2 SCAR=7.1
Pod documented
283 2 my ($self) = @_;
284 2 ▶ $self->_list_messages unless $self->{LIST_LOADED};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"LIST_LOADED"} 0 2 2
285 2 $self->{MESSAGECNT} - $self->{DELMESSAGECNT};
286 }
287  
288 1 ▶sub uidl {
Subroutine uidl: called CC=2 SCAR=7.1
Pod documented
289 1 my ($self, $message) = @_;
290 1 ▶ $self->_list_messages unless $self->{LIST_LOADED};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"LIST_LOADED"} 0 1 1
291 1 $self->{MSG2UIDL}->{$message};
292 }
293  
294 1 ▶sub get_fill_submit {
Subroutine get_fill_submit: called CC=1 SCAR=0.0
Pod documented
295 1 my ($cjar, $url, $vars, $varnamechange) = @_;
296 1 my ($html, $real_url) = redirect_cookie_loop($cjar, GET($url));
297 1 parse_fill_submit($cjar, $html, $real_url, $vars, $varnamechange);
298 }
299  
300 1 ▶sub parse_fill_submit {
Subroutine parse_fill_submit: called CC=5 SCAR=18.7
Pod documented
301 1 my ($cjar, $html, $real_url, $vars, $varnamechange) = @_;
302 1 my $form = HTML::Form->parse($html, $real_url);
303 map {
304 1 $form->value($_, $vars->{$_});
305 } keys %$vars;
306 1 $formno++;
307 1 ▶ to_file("f$formno.wri", Data::Dumper::Dumper($varnamechange, $form)) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Mail::POP3::Folder::webscrape::DEBUG 0 1 1
308 map {
309 0 ▶ my $input = $form->find_input(undef, undef, $_);
Branch: 1 branch
BranchTrueFalseTotal
if $varnamechange 0 1 1
310 0 $input->name($varnamechange->{$_});
311 0 local $^W = 0; # don't want to hear about "readonly"
312 0 ▶ $input->value('') unless defined $input->value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input->value 0 0 0
313 } keys %$varnamechange
314 if $varnamechange;
315 1 ▶ to_file("f$formno-after.wri", Data::Dumper::Dumper($varnamechange, $form)) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Mail::POP3::Folder::webscrape::DEBUG 0 1 1
316 1 my $form_html;
317 1 ($form_html, $real_url) = redirect_cookie_loop(
318 $cjar,
319 $form->click,
320 );
321 1 ($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 2 ▶sub list_parse {
Subroutine list_parse: called CC=3 SCAR=11.0
Pod documented
327 2 my ($text, $pageurl, $listre) = @_;
328 2 my %list;
329 2 for my $key (keys %$listre) {
330 8 ▶ if ($key eq 'itemurls') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'itemurls') { } 2 6 8
331 my @hits = map {
332 2 URI::URL->new($_, $pageurl)->abs->as_string;
333 } $text =~ m#$listre->{$key}#gsi;
334 2 $list{$key} = \@hits;
335 } else {
336 6 my ($match) = $text =~ m#$listre->{$key}#si;
337 6 $list{$key} = decode_entities($match);
338 }
339 }
340 2 $list{nextlink} = URI::URL->new($list{nextlink}, $pageurl)->abs->as_string;
341 2 \%list;
342 }
343  
344 1 ▶sub one_parse {
Subroutine one_parse: called CC=4 SCAR=13.9
Pod documented
345 1 my ($text, $scrapespec, $scrapepostpro) = @_;
346 1 my %item;
347 1 for my $key (keys %$scrapespec) {
348 15 my ($match) = $text =~ m#$scrapespec->{$key}#si;
349 15 ▶ $match = '' unless defined $match;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $match 4 11 15
350 15 $item{$key} = decode_entities($match);
351 }
352 # postpro - sub that returns list of key => value
353 # might be more than one pair
354 1 for my $key (keys %$scrapepostpro) {
355 4 my %ret = $scrapepostpro->{$key}->($key, $item{$key});
356 4 map { $item{$_} = $ret{$_} } keys %ret;
357 }
358 1 \%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 0 ▶sub parse_refresh {
Subroutine parse_refresh: not called CC=1 SCAR=6.9
Pod documented
398 0 my $header_val = shift;
399 0 my ($url) = $header_val =~ m#url=['"]?([^'"\s]*)#i;
400 0 $url;
401 }
402  
403 1 1;
404  
405 __END__
« POP3/Security/User.pm ↑ Summary POP3/Folder/mbox/parse_to_disk.pm »