| 1 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use feature qw( state ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | package WWW::RoboCop; | ||||||||||
| 6 | 1 | $WWW::RoboCop::VERSION = '0.000004'; | |||||||||
| 7 | 1 | ▶ | use Carp qw( croak ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use MooX::HandlesVia; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use MooX::StrictConstructor; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use Mozilla::CA; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use Type::Params qw( compile ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | ▶ | use Types::Standard qw( CodeRef HashRef InstanceOf ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 1 | ▶ | use Types::URI -all; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 1 | ▶ | use URI; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 1 | ▶ | use WWW::Mechanize; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | 1 | has is_url_whitelisted => ( | |||||||||
| 19 | is => 'ro', | ||||||||||
| 20 | isa => CodeRef, | ||||||||||
| 21 | handles_via => 'Code', | ||||||||||
| 22 | handles => { _should_follow_link => 'execute' }, | ||||||||||
| 23 | required => 1, | ||||||||||
| 24 | ); | ||||||||||
| 25 | |||||||||||
| 26 | has report_for_url => ( | ||||||||||
| 27 | is => 'ro', | ||||||||||
| 28 | isa => CodeRef, | ||||||||||
| 29 | handles_via => 'Code', | ||||||||||
| 30 | handles => { _log_response => 'execute' }, | ||||||||||
| 31 | default => sub { | ||||||||||
| 32 | sub { | ||||||||||
| 33 | 0 | ▶ | my $response = shift; # HTTP::Response object | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 34 | 0 | my $referring_url = shift; # URI object | |||||||||
| 35 | return { | ||||||||||
| 36 | $response->redirects | ||||||||||
| 37 | ? ( redirects => [ | ||||||||||
| 38 | map { | ||||||||||
| 39 | 0 | ▶ | { status => $_->code, | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | uri => $_->request->uri, | ||||||||||
| 41 | } | ||||||||||
| 42 | } $response->redirects | ||||||||||
| 43 | ], | ||||||||||
| 44 | target_url => $response->request->uri, | ||||||||||
| 45 | ) | ||||||||||
| 46 | : (), | ||||||||||
| 47 | referrer => $referring_url, | ||||||||||
| 48 | status => $response->code, | ||||||||||
| 49 | }; | ||||||||||
| 50 | 0 | ▶ | }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 51 | }, | ||||||||||
| 52 | 1 | ); | |||||||||
| 53 | |||||||||||
| 54 | has ua => ( | ||||||||||
| 55 | is => 'ro', | ||||||||||
| 56 | isa => InstanceOf ['WWW::Mechanize'], | ||||||||||
| 57 | default => sub { | ||||||||||
| 58 | 1 | ▶ | WWW::Mechanize->new( autocheck => 0 ); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 59 | }, | ||||||||||
| 60 | 1 | ); | |||||||||
| 61 | |||||||||||
| 62 | has _history => ( | ||||||||||
| 63 | is => 'ro', | ||||||||||
| 64 | isa => HashRef, | ||||||||||
| 65 | handles_via => 'Hash', | ||||||||||
| 66 | handles => { | ||||||||||
| 67 | _add_url_to_history => 'set', | ||||||||||
| 68 | _has_processed_url => 'exists', | ||||||||||
| 69 | }, | ||||||||||
| 70 | init_arg => undef, | ||||||||||
| 71 | 1 | ▶ | default => sub { +{} }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 72 | 1 | ); | |||||||||
| 73 | |||||||||||
| 74 | sub _get { | ||||||||||
| 75 | 2 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_get: called CC=6 SCAR=17.9
| |||||||||||
| 76 | 2 | my $url = shift; | |||||||||
| 77 | 2 | my $referring_url = shift; | |||||||||
| 78 | |||||||||||
| 79 | 2 | my $response = $self->ua->get( $url ); | |||||||||
| 80 | 2 | my $report = $self->_log_response( $response, $referring_url ); | |||||||||
| 81 | 2 | $self->_add_url_to_history( $url, $report ); | |||||||||
| 82 | |||||||||||
| 83 | 2 | my @links = $self->ua->find_all_links; | |||||||||
| 84 | |||||||||||
| 85 | 2 | foreach my $link ( @links ) { | |||||||||
| 86 | |||||||||||
| 87 | # this just points back at the same url | ||||||||||
| 88 | 8 | ▶ | next if substr( $link->url, 0, 1 ) eq '#'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | |||||||||||
| 90 | 6 | my $uri = URI->new( $link->url_abs ); | |||||||||
| 91 | 6 | $uri->fragment( undef ); # fragments result in duplicate urls | |||||||||
| 92 | |||||||||||
| 93 | 6 | ▶ | next if $self->_has_processed_url( $uri ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | 3 | ▶ | next unless $uri->can( 'host' ); # no mailto: links | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | 3 | ▶ | next unless $self->_should_follow_link( $link, $url ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | |||||||||||
| 97 | 1 | $self->_get( $uri, $url ); | |||||||||
| 98 | } | ||||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | sub crawl { | ||||||||||
| 102 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
crawl: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 103 | |||||||||||
| 104 | 1 | state $check = compile( Uri ); | |||||||||
| 105 | 1 | my ( $url ) = $check->( @_ ); | |||||||||
| 106 | |||||||||||
| 107 | 1 | $self->_get( $url ); | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | sub get_report { | ||||||||||
| 111 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
get_report: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 112 | 1 | return %{ $self->_history }; | |||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | 1 | 1; | |||||||||
| 116 | |||||||||||
| 117 | =pod | ||||||||||
| 118 | |||||||||||
| 119 | =encoding UTF-8 | ||||||||||
| 120 | |||||||||||
| 121 | =head1 NAME | ||||||||||
| 122 | |||||||||||
| 123 | WWW::RoboCop - Police your URLs! | ||||||||||
| 124 | |||||||||||
| 125 | =head1 VERSION | ||||||||||
| 126 | |||||||||||
| 127 | version 0.000004 | ||||||||||
| 128 | |||||||||||
| 129 | =head1 SYNOPSIS | ||||||||||
| 130 | |||||||||||
| 131 | use feature qw( state ); | ||||||||||
| 132 | |||||||||||
| 133 | use WWW::RoboCop; | ||||||||||
| 134 | |||||||||||
| 135 | my $robocop = WWW::RoboCop->new( | ||||||||||
| 136 | is_url_whitelisted => sub { | ||||||||||
| 137 | state $count = 0; | ||||||||||
| 138 | return $count++ < 5; # just crawl 5 URLs | ||||||||||
| 139 | }, | ||||||||||
| 140 | ); | ||||||||||
| 141 | |||||||||||
| 142 | $robocop->crawl( 'http://host.myhost.com/start' ); | ||||||||||
| 143 | |||||||||||
| 144 | my %history = $robocop->get_report; | ||||||||||
| 145 | |||||||||||
| 146 | # %history = ( | ||||||||||
| 147 | # 'http://myhost.com/one' => { status => 200, ... }, | ||||||||||
| 148 | # 'http://myhost.com/two' => { status => 404, ... }, | ||||||||||
| 149 | # ... | ||||||||||
| 150 | # ) | ||||||||||
| 151 | |||||||||||
| 152 | =head1 DESCRIPTION | ||||||||||
| 153 | |||||||||||
| 154 | BETA BETA BETA! | ||||||||||
| 155 | |||||||||||
| 156 | C<WWW::RoboCop> is a dead simple, somewhat opinionated robot. Given a starting | ||||||||||
| 157 | page, this module will crawl only URLs which have been whitelisted by the | ||||||||||
| 158 | C<is_url_whitelisted> callback. It then creates a report of all visited pages, | ||||||||||
| 159 | keyed on URL. You are encouraged to provide your own report creation callback | ||||||||||
| 160 | so that you can collect all of the information which you require for each URL. | ||||||||||
| 161 | |||||||||||
| 162 | =head1 CONSTRUCTOR AND STARTUP | ||||||||||
| 163 | |||||||||||
| 164 | =head2 new() | ||||||||||
| 165 | |||||||||||
| 166 | Creates and returns a new C<WWW::RoboCop> object. | ||||||||||
| 167 | |||||||||||
| 168 | Below are the arguments which you may pass to C<new> when creating an object. | ||||||||||
| 169 | |||||||||||
| 170 | =head3 is_url_whitelisted | ||||||||||
| 171 | |||||||||||
| 172 | This argument is required. You must provide an anonymous subroutine which will | ||||||||||
| 173 | return true or false based on some arbitrary criteria which you provide. The | ||||||||||
| 174 | two arguments to this anonymous subroutine will be a L<WWW::Mechanize::Link> | ||||||||||
| 175 | object as well as the referring URL, in the form of a L<URI> object. | ||||||||||
| 176 | |||||||||||
| 177 | Your sub might look something like this: | ||||||||||
| 178 | |||||||||||
| 179 | use feature qw( state ); | ||||||||||
| 180 | |||||||||||
| 181 | use URI; | ||||||||||
| 182 | use WWW::RoboCop; | ||||||||||
| 183 | |||||||||||
| 184 | my $robocop = WWW::RoboCop->new( | ||||||||||
| 185 | is_url_whitelisted => sub { | ||||||||||
| 186 | my $link = shift; | ||||||||||
| 187 | my $referring_url = shift; | ||||||||||
| 188 | |||||||||||
| 189 | my $upper_limit = 100; | ||||||||||
| 190 | my $host = 'some.host.com'; | ||||||||||
| 191 | |||||||||||
| 192 | state $limit = 0; | ||||||||||
| 193 | |||||||||||
| 194 | return 0 if $limit > $upper_limit; | ||||||||||
| 195 | my $uri = URI->new( $link->url_abs ); | ||||||||||
| 196 | |||||||||||
| 197 | # if the referring_url matches the host then this is a 1st degree | ||||||||||
| 198 | # outbound web link | ||||||||||
| 199 | |||||||||||
| 200 | if ( $uri->host eq $host || $referring_url->host eq $host ) { | ||||||||||
| 201 | ++$limit; | ||||||||||
| 202 | return 1; | ||||||||||
| 203 | } | ||||||||||
| 204 | return 0; | ||||||||||
| 205 | } | ||||||||||
| 206 | ); | ||||||||||
| 207 | |||||||||||
| 208 | =head3 report_for_url | ||||||||||
| 209 | |||||||||||
| 210 | This argument is not required, but is highly recommended. The arguments to this | ||||||||||
| 211 | anonymous subroutine will be an L<HTTP::Response> object as well as the | ||||||||||
| 212 | referring URL in the form of a L<URI> object. Your sub might look something | ||||||||||
| 213 | like this: | ||||||||||
| 214 | |||||||||||
| 215 | my $reporter = sub { | ||||||||||
| 216 | my $response = shift; # HTTP::Response object | ||||||||||
| 217 | my $referring_url = shift; # URI object | ||||||||||
| 218 | return { | ||||||||||
| 219 | redirects => [ | ||||||||||
| 220 | map { +{ status => $_->code, uri => $_->base->as_string } } | ||||||||||
| 221 | $res->redirects | ||||||||||
| 222 | ], | ||||||||||
| 223 | referrer => $referring_url, | ||||||||||
| 224 | status => $res->code, | ||||||||||
| 225 | }; | ||||||||||
| 226 | }; | ||||||||||
| 227 | |||||||||||
| 228 | my $robocop = WWW::RoboCop->new( | ||||||||||
| 229 | is_url_whitelisted => sub { ... }, | ||||||||||
| 230 | report_for_url => $reporter, | ||||||||||
| 231 | ); | ||||||||||
| 232 | |||||||||||
| 233 | That would give you a HashRef with the status code for each link visited (200, | ||||||||||
| 234 | 404, 500, etc) as well as the referring URL (the page on which the link was | ||||||||||
| 235 | found) and a list of any redirects which were followed in order to get to this | ||||||||||
| 236 | URL. | ||||||||||
| 237 | |||||||||||
| 238 | The default C<report_for_url> sub will already provide something like the | ||||||||||
| 239 | above, but you should only treat this as a stub method while you get up and | ||||||||||
| 240 | running. Since it's only meant to be an example, the format of the default | ||||||||||
| 241 | report could change at some future date without notice. You should not rely on | ||||||||||
| 242 | or expect it to remain consistent in future. If you are going to rely on this | ||||||||||
| 243 | module, you should provide your own reporting logic. | ||||||||||
| 244 | |||||||||||
| 245 | =head3 ua( WWW::Mechanize ) | ||||||||||
| 246 | |||||||||||
| 247 | You can provide your own UserAgent object to this class. It should be of the | ||||||||||
| 248 | L<WWW::Mechanize> family. If you're looking for a significant speed boost | ||||||||||
| 249 | while under development, consider providing a L<WWW::Mechanize::Cached> object. | ||||||||||
| 250 | This can give you enough of a speedup to save you from getting distracted | ||||||||||
| 251 | and going off to read Hacker News while you wait. | ||||||||||
| 252 | |||||||||||
| 253 | use CHI; | ||||||||||
| 254 | use WWW::Mechanize::Cached; | ||||||||||
| 255 | use WWW::RoboCop; | ||||||||||
| 256 | |||||||||||
| 257 | my $cache = CHI->new( | ||||||||||
| 258 | driver => 'File', | ||||||||||
| 259 | root_dir => /tmp/cache-example', | ||||||||||
| 260 | ); | ||||||||||
| 261 | |||||||||||
| 262 | my $robocop = WWW::RoboCop->new( | ||||||||||
| 263 | is_url_whitelisted => sub { ... }, | ||||||||||
| 264 | ua => WWW::Mechanize::Cached->new( cache => $cache ), | ||||||||||
| 265 | ); | ||||||||||
| 266 | |||||||||||
| 267 | If you're not using a Cached agent, be sure to disable autocheck. | ||||||||||
| 268 | |||||||||||
| 269 | my $robocop = WWW::RoboCop->new( | ||||||||||
| 270 | is_url_whitelisted => sub { ... }, | ||||||||||
| 271 | ua => WWW::Mechanize->new( autocheck => 0 ), | ||||||||||
| 272 | ); | ||||||||||
| 273 | |||||||||||
| 274 | =head2 crawl( $url ) | ||||||||||
| 275 | |||||||||||
| 276 | This method sets the C<WWW::RoboCop> in motion. The robot will only come to a | ||||||||||
| 277 | halt once has exhausted all of the whitelisted URLs it can find. | ||||||||||
| 278 | |||||||||||
| 279 | =head2 get_report | ||||||||||
| 280 | |||||||||||
| 281 | This method returns a Hash of crawling results, keyed on the URLs visited. | ||||||||||
| 282 | By default, it returns a very simple Hash, containing only the status code | ||||||||||
| 283 | of the visited URL. You are encouraged to provide your own callback so that | ||||||||||
| 284 | you can get a detailed report returned to you. You can do this by providing a | ||||||||||
| 285 | C<report_for_url> callback when instantiating the object. | ||||||||||
| 286 | |||||||||||
| 287 | The default report looks something like this: | ||||||||||
| 288 | |||||||||||
| 289 | # %history = ( | ||||||||||
| 290 | # 'http://myhost.com/one' => { status => 200, ... }, | ||||||||||
| 291 | # 'http://myhost.com/two' => { status => 404, ... }, | ||||||||||
| 292 | # ) | ||||||||||
| 293 | |||||||||||
| 294 | See examples/crawl-host.pl, which is included with this distribution, to get a | ||||||||||
| 295 | dump of the default report. | ||||||||||
| 296 | |||||||||||
| 297 | =head1 AUTHOR | ||||||||||
| 298 | |||||||||||
| 299 | Olaf Alders <olaf@wundercounter.com> | ||||||||||
| 300 | |||||||||||
| 301 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 302 | |||||||||||
| 303 | This software is Copyright (c) 2015 by MaxMind, Inc.. | ||||||||||
| 304 | |||||||||||
| 305 | This is free software, licensed under: | ||||||||||
| 306 | |||||||||||
| 307 | The Artistic License 2.0 (GPL Compatible) | ||||||||||
| 308 | |||||||||||
| 309 | =cut | ||||||||||
| 310 | |||||||||||
| 311 | __END__ | ||||||||||
| 312 | |||||||||||
| 313 | # ABSTRACT: Police your URLs! | ||||||||||
| 314 | |||||||||||