line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
############################################################################### |
2
|
|
|
|
|
|
|
package CrawlerCommons::RobotDirective; |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
# MODULE IMPORTS |
5
|
|
|
|
|
|
|
######################################## |
6
|
|
|
|
|
|
|
# Pragmas |
7
|
|
|
|
|
|
|
#------------------# |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
# CPAN/Core |
10
|
|
|
|
|
|
|
#------------------# |
11
|
2
|
|
|
2
|
|
11
|
use Const::Fast; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
15
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# Moose Setup |
14
|
|
|
|
|
|
|
#------------------# |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# Moose Pragmas |
17
|
|
|
|
|
|
|
#------------------# |
18
|
2
|
|
|
2
|
|
121
|
use Moose; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
11
|
|
19
|
2
|
|
|
2
|
|
12916
|
use MooseX::ClassAttribute; |
|
2
|
|
|
|
|
161621
|
|
|
2
|
|
|
|
|
16
|
|
20
|
2
|
|
|
2
|
|
896913
|
use namespace::autoclean; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
20
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# Custom Modules |
23
|
|
|
|
|
|
|
#------------------# |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# VARIABLES/CONSTANTS |
27
|
|
|
|
|
|
|
######################################## |
28
|
|
|
|
|
|
|
# Constants |
29
|
|
|
|
|
|
|
#------------------# |
30
|
|
|
|
|
|
|
const my $DEBUG => $ENV{DEBUG} // 0; |
31
|
|
|
|
|
|
|
const my $TEST => $ENV{TEST} // 1; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
const my $CRAWLDELAY_MISSPELLINGS=>["crawl delay"]; |
34
|
|
|
|
|
|
|
const my $DISALLOW_MISSPELLINGS => [qw(desallow dissalow dssalow dsallow)]; |
35
|
|
|
|
|
|
|
const my $DIRECTIVES_LIST => [ |
36
|
|
|
|
|
|
|
'USER_AGENT', 'DISALLOW', |
37
|
|
|
|
|
|
|
'ALLOW', 'CRAWL_DELAY', 'SITEMAP', |
38
|
|
|
|
|
|
|
'HOST', |
39
|
|
|
|
|
|
|
'NO_INDEX', |
40
|
|
|
|
|
|
|
# Extended standard |
41
|
|
|
|
|
|
|
'REQUEST_RATE', 'VISIT_TIME', 'ROBOT_VERSION', 'COMMENT', |
42
|
|
|
|
|
|
|
# Treated as sitemap directive |
43
|
|
|
|
|
|
|
'HTTP']; |
44
|
|
|
|
|
|
|
const my $PREFIX_DIRECTIVES => [qw(ACAP_)]; |
45
|
|
|
|
|
|
|
const my $SPECIAL_DIRECTIVES => [qw(UNKNOWN MISSING)]; |
46
|
|
|
|
|
|
|
const my $USERAGENT_MISSPELLINGS=> [qw(useragent useg-agent ser-agent)]; |
47
|
|
|
|
|
|
|
const my $VALUES_ENUM_LIST => |
48
|
|
|
|
|
|
|
[ map {my $v = lc( $_ ); $v =~ s/\_$//; $v;} |
49
|
|
|
|
|
|
|
map { @{ $_ } } |
50
|
|
|
|
|
|
|
( $DIRECTIVES_LIST, $PREFIX_DIRECTIVES, $SPECIAL_DIRECTIVES ) ]; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# Variables |
53
|
|
|
|
|
|
|
#------------------# |
54
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# MOOSE ATTRIBUTES |
58
|
|
|
|
|
|
|
######################################## |
59
|
|
|
|
|
|
|
# Class |
60
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------# |
61
|
|
|
|
|
|
|
class_has 'directive_map' => ( |
62
|
|
|
|
|
|
|
builder => 'load_directives_map', |
63
|
|
|
|
|
|
|
handles => { |
64
|
|
|
|
|
|
|
directive_exists => 'exists', |
65
|
|
|
|
|
|
|
get_directive => 'get', |
66
|
|
|
|
|
|
|
}, |
67
|
|
|
|
|
|
|
is => 'ro', |
68
|
|
|
|
|
|
|
isa => 'HashRef', |
69
|
|
|
|
|
|
|
lazy => 1, |
70
|
|
|
|
|
|
|
traits => ['Hash'], |
71
|
|
|
|
|
|
|
); |
72
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------# |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# Instance |
75
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------# |
76
|
|
|
|
|
|
|
has 'is_prefix' => ( |
77
|
|
|
|
|
|
|
default => 0, |
78
|
|
|
|
|
|
|
is => 'ro', |
79
|
|
|
|
|
|
|
isa => 'Bool', |
80
|
|
|
|
|
|
|
); |
81
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------# |
82
|
|
|
|
|
|
|
has 'is_special' => ( |
83
|
|
|
|
|
|
|
default => 0, |
84
|
|
|
|
|
|
|
is => 'ro', |
85
|
|
|
|
|
|
|
isa => 'Bool', |
86
|
|
|
|
|
|
|
); |
87
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------# |
88
|
|
|
|
|
|
|
has 'value' => ( |
89
|
|
|
|
|
|
|
enum => $VALUES_ENUM_LIST, |
90
|
|
|
|
|
|
|
handles => 1, |
91
|
|
|
|
|
|
|
is => 'ro', |
92
|
|
|
|
|
|
|
required => 1, |
93
|
|
|
|
|
|
|
traits => ['Enumeration'], |
94
|
|
|
|
|
|
|
); |
95
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------# |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# METHODS |
98
|
|
|
|
|
|
|
######################################## |
99
|
|
|
|
|
|
|
# Construction |
100
|
|
|
|
|
|
|
#------------------# |
101
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------# |
102
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------# |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
# Class Methods |
105
|
|
|
|
|
|
|
#------------------# |
106
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------# |
107
|
|
|
|
|
|
|
sub load_directives_map { |
108
|
1
|
|
|
1
|
0
|
4
|
my $pkg = shift; |
109
|
|
|
|
|
|
|
|
110
|
1
|
|
|
|
|
7
|
my $map = { |
111
|
|
|
|
|
|
|
$pkg->_map_directive_list( $DIRECTIVES_LIST, 0, 0), |
112
|
|
|
|
|
|
|
$pkg->_map_directive_list( $PREFIX_DIRECTIVES, 1, 0), |
113
|
|
|
|
|
|
|
$pkg->_map_directive_list( $SPECIAL_DIRECTIVES, 0, 1), |
114
|
|
|
|
|
|
|
}; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# setup common user_agent, disallow and crawl_delya directive misspellings |
117
|
1
|
|
|
|
|
4
|
$map->{$_} = $map->{'crawl-delay'} for @{ $CRAWLDELAY_MISSPELLINGS }; |
|
1
|
|
|
|
|
7
|
|
118
|
1
|
|
|
|
|
3
|
$map->{$_} = $map->{disallow} for @{ $DISALLOW_MISSPELLINGS }; |
|
1
|
|
|
|
|
9
|
|
119
|
1
|
|
|
|
|
3
|
$map->{$_} = $map->{'user-agent'} for @{ $USERAGENT_MISSPELLINGS }; |
|
1
|
|
|
|
|
112
|
|
120
|
|
|
|
|
|
|
|
121
|
1
|
|
|
|
|
58
|
return $map; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------# |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# Instance Methods |
126
|
|
|
|
|
|
|
#------------------# |
127
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------# |
128
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------# |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
# Private Methods |
131
|
|
|
|
|
|
|
#------------------# |
132
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------# |
133
|
|
|
|
|
|
|
sub _map_directive_list { |
134
|
3
|
|
|
3
|
|
9
|
my ($pkg, $directives_list, $is_prefix, $is_special) = @_; |
135
|
3
|
|
|
|
|
10
|
my %map = (); |
136
|
|
|
|
|
|
|
|
137
|
3
|
|
|
|
|
25
|
for my $directive_str ( @{ $directives_list } ) { |
|
3
|
|
|
|
|
10
|
|
138
|
15
|
|
|
|
|
111
|
(my $prefix = lc($directive_str)) =~ s!_!\-!g; |
139
|
15
|
|
|
|
|
40
|
(my $value = lc( $directive_str )) =~ s!_$!!g; |
140
|
15
|
100
|
|
|
|
624
|
$map{$prefix} = $is_prefix ? |
|
|
100
|
|
|
|
|
|
141
|
|
|
|
|
|
|
$pkg->new(value => $value, is_prefix => 1) : |
142
|
|
|
|
|
|
|
($is_special ? |
143
|
|
|
|
|
|
|
$pkg->new(value => $value, is_special => 1) : |
144
|
|
|
|
|
|
|
$pkg->new(value => $value) ); |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
3
|
|
|
|
|
25
|
return %map; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------# |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
############################################################################### |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
############################################################################### |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
1; |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
__END__ |