| 1 | package AnyEvent::WebArchive; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use AnyEvent::HTTP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | our @EXPORT = qw(restore_url); | |||||||||||||||||||||||||||
| 10 | 2 | my $DEBUG = 0; | |||||||||||||||||||||||||||
| 11 | 0 | ▶ | sub restore_url { | ||||||||||||||||||||||||||
|
Subroutine
restore_url: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 12 | 0 | my $url = shift; | |||||||||||||||||||||||||||
| 13 | 0 | my $cb = pop; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 0 | $url =~ s/^www\.//; | |||||||||||||||||||||||||||
| 16 | 0 | ▶ | my $opt = ref $_[0] ? $_[0] : {@_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 0 | ▶ | $AnyEvent::HTTP::USERAGENT = $opt->{'user_agent'} || 'Opera/9.80 (Windows NT 5.1; U; ru) Presto/2.5.24 Version/10.52'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$opt->{'user_agent'} || 'Opera/9.80 (Windows NT 5.1; U; ru) Presto/2.5.24 Version/10.52'
MC/DC0%0/1
$opt->{'user_agent'} || 'Opera/9.80 (Windows NT 5.1; U; ru) Presto/2.5.24 Version/10.52'
$opt->{'user_agent'}
Truth table0%0/2
A
A= $opt->{'user_agent'}
| |||||||||||||||||||||||||||||
| 19 | 0 | ▶ | $AnyEvent::HTTP::MAX_PER_HOST ||= $opt->{'max_per_host'}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$AnyEvent::HTTP::MAX_PER_HOST ||= $opt->{"max_per_host"}
MC/DC0%0/2
$AnyEvent::HTTP::MAX_PER_HOST ||= $opt->{"max_per_host"}
$AnyEvent::HTTP::MAX_PER_HOST
$opt->{"max_per_host"}
Truth table0%0/3
A ||= B
A= $AnyEvent::HTTP::MAX_PER_HOSTB= $opt->{"max_per_host"}
| |||||||||||||||||||||||||||||
| 20 | 0 | ▶ | $AnyEvent::HTTP::ACTIVE ||= $opt->{'active' }; | ||||||||||||||||||||||||||
|
Condition0%0/3
$AnyEvent::HTTP::ACTIVE ||= $opt->{"active"}
MC/DC0%0/2
$AnyEvent::HTTP::ACTIVE ||= $opt->{"active"}
$AnyEvent::HTTP::ACTIVE
$opt->{"active"}
Truth table0%0/3
A ||= B
A= $AnyEvent::HTTP::ACTIVEB= $opt->{"active"}
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | my $count; | |||||||||||||||||||||||||||
| 23 | 0 | my $worker = {}; | |||||||||||||||||||||||||||
| 24 | 0 | bless $worker, __PACKAGE__; | |||||||||||||||||||||||||||
| 25 | 0 | $worker->{'domain'} = $url; | |||||||||||||||||||||||||||
| 26 | 0 | ▶ | http_get _search($url), sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 27 | 0 | $url = $url; | |||||||||||||||||||||||||||
| 28 | 0 | ▶ | $DEBUG && warn "GET $url\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 0 | my ($body, $headers) = @_; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | for my $job (grep { $_->[0] } # XXX | |||||||||||||||||||||||||||
| 32 | 0 | map { [ /href="([^"]+)"/sg, />([^<]+)<\/a>/sg ] } map { split /(<br>){2}/ } | |||||||||||||||||||||||||||
| 33 | $body =~ m{<!-- SEARCH RESULTS -->(.*?)<!-- /SEARCH RESULTS -->}si | ||||||||||||||||||||||||||||
| 34 | ) { | ||||||||||||||||||||||||||||
| 35 | 0 | ▶ | $DEBUG && warn "GET $job->[0]\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | $count++; | |||||||||||||||||||||||||||
| 37 | 0 | ▶ | http_get $job->[0], sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 38 | 0 | my ($body, $headers) = @_; | |||||||||||||||||||||||||||
| 39 | 0 | ▶ | if ($headers->{'Status'} == 200) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 0 | $worker->_save_file($job->[1], $body); | |||||||||||||||||||||||||||
| 41 | } else { | ||||||||||||||||||||||||||||
| 42 | 0 | warn "Bad status for url $job->[0]: $_" for Dumper($headers); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | $cb->() unless --$count; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 0 | } | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 0 | } | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | sub _filename { | ||||||||||||||||||||||||||
|
Subroutine
_filename: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 52 | 0 | my $str = shift; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | $str =~ s/[^a-z\.\,\s\;-]/_/sig; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | return $str; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | sub _search { | ||||||||||||||||||||||||||
|
Subroutine
_search: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 60 | 0 | return "http://web.archive.org/web/*sr_1nr_10000/$_*" for shift; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | sub _save_file { | ||||||||||||||||||||||||||
|
Subroutine
_save_file: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 64 | 0 | my ($worker, $url,$body) = @_; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | $url = $worker->{'domain'} . $worker->_normalize_url($url); | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | my $path; | |||||||||||||||||||||||||||
| 69 | 0 | for (split /\//, $url) { | |||||||||||||||||||||||||||
| 70 | 0 | ▶ | last if /^\?/ || $url =~ /$_$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
/^\?/ or $url =~ /$_$/
MC/DC0%0/2
/^\?/ or $url =~ /$_$/
/^\?/
$url =~ /$_$/
Truth table0%0/3
A or B
A= /^\?/B= $url =~ /$_$/
| |||||||||||||||||||||||||||||
| 71 | 0 | $path .= "$_/"; | |||||||||||||||||||||||||||
| 72 | 0 | ▶ | $DEBUG && warn "mkdir $path\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | mkdir $path; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | return warn "file $url already exists, skipping\n" if -e $url; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | $DEBUG && warn "writing $url\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | open my $fh, '>', $url or warn "$!: $url"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | print $fh $worker->_normalize($body); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 1 | ▶ | sub _normalize { | ||||||||||||||||||||||||||
|
Subroutine
_normalize: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 85 | 1 | my ($worker,$body) = @_; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 1 | $body =~ s/(?<=href=")([^"]+)(?=")/$worker->_normalize_url($1)/sieg; | |||||||||||||||||||||||||||
| 88 | 1 | $body =~ s{(?<=</body>).*(?=</html>)}{}si; | |||||||||||||||||||||||||||
| 89 | 1 | return $body; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 3 | ▶ | sub _normalize_url { | ||||||||||||||||||||||||||
|
Subroutine
_normalize_url: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 93 | 3 | my ($worker,$url) = @_; | |||||||||||||||||||||||||||
| 94 | 3 | $url =~ s/\?$//; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 3 | ▶ | $url =~ s/^.*?$worker->{domain}//i unless $url =~ /^\//; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 3 | ▶ | $url .= 'index.html' if $url =~ /\/$/; # dirs | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 3 | ▶ | $url .= '.html' unless $url =~ /\..{1,7}$/; # w/o extension | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 3 | $url =~ s/[^a-z0-9_\/\.\-\+=%&]/_/ig; # strip bad characters in filename | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 3 | return $url; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 2 | 1; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =head1 NAME | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | AnyEvent::WebArchive - simple non-blocking WebArchive client | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =head1 VERSION | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0.02 | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | use AnyEvent::WebArchive; | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | my $c = AnyEvent->condvar; | ||||||||||||||||||||||||||||
| 120 | restore_url('cpan.org', sub { $c->send }); | ||||||||||||||||||||||||||||
| 121 | $c->recv; | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =head1 METHODS | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =over 4 | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =item restore_url $url, option => value, ..., $callback | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | Restore all data from WebArchive cache for C<$url> | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =back | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =head1 OPTIONS | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =over 4 | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =item user_agent - UserAgent string | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =item active - number of active connections for L<AnyEvent::HTTP> | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =item max_per_host - maximum connections per one host for L<AnyEvent::HTTP> | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =back | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =over 4 | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =item * Repository | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | L<http://github.com/konstantinov/AnyEvent-WebArchive> | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =back | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | L<AnyEvent>, L<AnyEvent::HTTP> | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | Copyright 2009 Dmitry Konstantinov. All right reserved. | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||