1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
# $Id: RobotRules.pm,v 1.16 1998/06/09 12:14:43 aas Exp $
package WWW::RobotRules;
=head1 NAME
WWW::RobotsRules - Parse robots.txt files
=head1 SYNOPSIS
require WWW::RobotRules;
my $robotsrules = new WWW::RobotRules 'MOMspider/1.0';
use LWP::Simple qw(get);
$url = "http://some.place/robots.txt";
my $robots_txt = get $url;
$robotsrules->parse($url, $robots_txt);
$url = "http://some.other.place/robots.txt";
my $robots_txt = get $url;
$robotsrules->parse($url, $robots_txt);
# Now we are able to check if a URL is valid for those servers that
# we have obtained and parsed "robots.txt" files for.
if($robotsrules->allowed($url)) {
$c = get $url;
...
}
=head1 DESCRIPTION
This module parses a F<robots.txt> file as specified in
"A Standard for Robot Exclusion", described in
<URL:http://info.webcrawler.com/mak/projects/robots/norobots.html>
Webmasters can use the F<robots.txt> file to disallow conforming
robots access to parts of their WWW server.
The parsed file is kept in the WWW::RobotRules object, and this object
provide methods to check if access to a given URL is prohibited. The
same WWW::RobotRules object can parse multiple F<robots.txt> files.
The following methods are provided:
=over 4
=cut
$VERSION = sprintf("%d.%02d", q$Revision: 1.16 $ =~ /(\d+)\.(\d+)/);
sub Version { $VERSION; }
use URI::URL ();
use strict;
=item $rules = new WWW::RobotRules 'MOMspider/1.0'
This is the constructor for WWW::RobotRules objects. The first
argument given to new() is the name of the robot.
=cut
sub new {
my($class, $ua) = @_;
# This ugly hack is needed to ensure backwards compatability.
# The "WWW::RobotRules" class is now really abstract.
$class = "WWW::RobotRules::InCore" if $class eq "WWW::RobotRules";
my $self = bless { }, $class;
$self->agent($ua);
$self;
}
=item $rules->parse($url, $content, $fresh_until)
The parse() method takes as arguments the URL that was used to
retrieve the F</robots.txt> file, and the contents of the file.
=cut
sub parse {
my($self, $url, $txt, $fresh_until) = @_;
$url = new URI::URL $url unless ref($url); # make it URL
my $netloc = $url->netloc;
$self->clear_rules($netloc);
$self->fresh_until($netloc, $fresh_until || (time + 365*24*3600));
my $ua;
my $is_me = 0; # 1 iff this record is for me
my $is_anon = 0; # 1 iff this record is for *
my @me_disallowed = (); # rules disallowed for me
my @anon_disallowed = (); # rules disallowed for *
# blank lines are significant, so turn CRLF into LF to avoid generating
# false ones
$txt =~ s/\015\012/\012/g;
# split at \012 (LF) or \015 (CR) (Mac text files have just CR for EOL)
for(split(/[\012\015]/, $txt)) {
# Lines containing only a comment are discarded completely, and
# therefore do not indicate a record boundary.
next if /^\s*\#/;
s/\s*\#.*//; # remove comments at end-of-line
if (/^\s*$/) { # blank line
last if $is_me; # That was our record. No need to read the rest.
$is_anon = 0;
}
elsif (/^User-Agent:\s*(.*)/i) {
$ua = $1;
$ua =~ s/\s+$//;
if ($is_me) {
# This record already had a User-agent that
# we matched, so just continue.
}
elsif ($ua eq '*') {
$is_anon = 1;
}
elsif($self->is_me($ua)) {
$is_me = 1;
}
}
elsif (/^Disallow:\s*(.*)/i) {
unless (defined $ua) {
warn "RobotRules: Disallow without preceding User-agent\n";
$is_anon = 1; # assume that User-agent: * was intended
}
my $disallow = $1;
$disallow =~ s/\s+$//;
if (length $disallow) {
$disallow = URI::URL->new($disallow, $url)->full_path;
}
if ($is_me) {
push(@me_disallowed, $disallow);
}
elsif ($is_anon) {
push(@anon_disallowed, $disallow);
}
}
else {
warn "RobotRules: Unexpected line: $_\n";
}
}
if ($is_me) {
$self->push_rules($netloc, @me_disallowed);
} else {
$self->push_rules($netloc, @anon_disallowed);
}
}
# is_me()
#
# Returns TRUE if the given name matches the
# name of this robot
#
sub is_me {
my($self, $ua) = @_;
my $me = $self->agent;
return index(lc($ua), lc($me)) >= 0;
}
=item $rules->allowed($url)
Returns TRUE if this robot is allowed to retrieve this URL.
=cut
sub allowed {
my($self, $url) = @_;
$url = URI::URL->new($url) unless ref $url; # make it URL
my $netloc = $url->netloc;
my $fresh_until = $self->fresh_until($netloc);
return -1 if !defined($fresh_until) || $fresh_until < time;
my $str = $url->full_path;
my $rule;
for $rule ($self->rules($netloc)) {
return 1 unless length $rule;
return 0 if index($str, $rule) == 0;
}
return 1;
}
# The following methods must be provided by the subclass.
sub agent;
sub visit;
sub no_visits;
sub last_visits;
sub fresh_until;
sub push_rules;
sub clear_rules;
sub rules;
sub dump;
package WWW::RobotRules::InCore;
use vars qw(@ISA);
@ISA = qw(WWW::RobotRules);
=item $rules->agent([$name])
Get/set the agent name. NOTE: Changing the agent name will clear the robots.txt
rules and expire times out of the cache.
=cut
sub agent {
my ($self, $name) = @_;
my $old = $self->{'ua'};
if ($name) {
delete $self->{'loc'}; # all old info is now stale
$name =~ s!/?\s*\d+.\d+\s*$!!; # loose version
$self->{'ua'}=$name;
}
$old;
}
sub visit {
my($self, $netloc, $time) = @_;
$time ||= time;
$self->{'loc'}{$netloc}{'last'} = $time;
my $count = \$self->{'loc'}{$netloc}{'count'};
if (!defined $$count) {
$$count = 1;
} else {
$$count++;
}
}
sub no_visits {
my ($self, $netloc) = @_;
$self->{'loc'}{$netloc}{'count'};
}
sub last_visit {
my ($self, $netloc) = @_;
$self->{'loc'}{$netloc}{'last'};
}
sub fresh_until {
my ($self, $netloc, $fresh_until) = @_;
my $old = $self->{'loc'}{$netloc}{'fresh'};
if (defined $fresh_until) {
$self->{'loc'}{$netloc}{'fresh'} = $fresh_until;
}
$old;
}
sub push_rules {
my($self, $netloc, @rules) = @_;
push (@{$self->{'loc'}{$netloc}{'rules'}}, @rules);
}
sub clear_rules {
my($self, $netloc) = @_;
delete $self->{'loc'}{$netloc}{'rules'};
}
sub rules {
my($self, $netloc) = @_;
if (defined $self->{'loc'}{$netloc}{'rules'}) {
return @{$self->{'loc'}{$netloc}{'rules'}};
} else {
return ();
}
}
sub dump
{
my $self = shift;
for (keys %$self) {
next if $_ eq 'loc';
print "$_ = $self->{$_}\n";
}
for (keys %{$self->{'loc'}}) {
my @rules = $self->rules($_);
print "$_: ", join("; ", @rules), "\n";
}
}
1;
__END__
=back
=head1 ROBOTS.TXT
The format and semantics of the "/robots.txt" file are as follows
(this is an edited abstract of
<URL:http://info.webcrawler.com/mak/projects/robots/norobots.html>):
The file consists of one or more records separated by one or more
blank lines. Each record contains lines of the form
<field-name>: <value>
The field name is case insensitive. Text after the '#' character on a
line is ignored during parsing. This is used for comments. The
following <field-names> can be used:
=over 3
=item User-Agent
The value of this field is the name of the robot the record is
describing access policy for. If more than one I<User-Agent> field is
present the record describes an identical access policy for more than
one robot. At least one field needs to be present per record. If the
value is '*', the record describes the default access policy for any
robot that has not not matched any of the other records.
=item Disallow
The value of this field specifies a partial URL that is not to be
visited. This can be a full path, or a partial path; any URL that
starts with this value will not be retrieved
=back
=head1 ROBOTS.TXT EXAMPLES
The following example "/robots.txt" file specifies that no robots
should visit any URL starting with "/cyberworld/map/" or "/tmp/":
User-agent: *
Disallow: /cyberworld/map/ # This is an infinite virtual URL space
Disallow: /tmp/ # these will soon disappear
This example "/robots.txt" file specifies that no robots should visit
any URL starting with "/cyberworld/map/", except the robot called
"cybermapper":
User-agent: *
Disallow: /cyberworld/map/ # This is an infinite virtual URL space
# Cybermapper knows where to go.
User-agent: cybermapper
Disallow:
This example indicates that no robots should visit this site further:
# go away
User-agent: *
Disallow: /
=head1 SEE ALSO
L<LWP::RobotUA>, L<WWW::RobotRules::AnyDBM_File>
=cut
|