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
| #!C:\Program Files\Perl\bin\perl.exe -w
use strict;
use URI::URL;
use HTTP::Request;
use HTTP::Response;
use HTTP::Cookies;
use POSIX;
use HTTP::Size;
require LWP::Parallel::UserAgent;
use HTML::LinkExtor;
#===========================================================================#
# Begin of configuration #
#===========================================================================#
my $arg;
my %urls;
my @web;
$| = 1;
my $VERSION = "Bot/1.1";
my $ua = LWP::Parallel::UserAgent->new('Mozilla/5.0 (compatible; Bot/1.1');
$ua->max_hosts(50);
$ua->max_req(50);
$ua->wait(0);
$ua->max_redirect(8000000000);
$ua->protocols_allowed( [ 'http' ] );
$ua->protocols_forbidden( [ 'file', 'mailto', 'https', 'ftp', 'socks', 'gopher'] );
$ua->requests_redirectable( [ 'HEAD' ] );
$ua->timeout(4);
$ua->env_proxy;
$ua->nonblock(1);
$ua->duplicates(1);
$ua->remember_failures(1);
$ua->redirect(1);
my $cookie_jar = HTTP::Cookies->new(
file => 'lwp_cookies.dat',
autosave => 1,
);
$ua->cookie_jar($cookie_jar);
#===========================================================================#
# The Robot #
#===========================================================================#
robot(@ARGV);
{
my %vus;
my @avoir;
sub robot {
#gerer la liste
foreach my $link ( @_ ) {
next if defined $vus{$link}; # deja vu
push( @avoir, $link ); # les nvx url vont a la fin de la liste
}
return if $#avoir == -1; # plus rien à voir
#parser les urls
while (my $page = shift @avoir; ++$vus{$page}) {
my $verif_links = verif_link_format($page);
if ($verif_links == 1 ) {
my $req = HTTP::Request->new( 'GET', $page );
$req->header('Accept' => [
qw(
text/html application/xhtml+xml application/msexcel
application/msword application/pdf application/rtf
application/x-javascript application/x-httpd-php
text/rtf text/nux application/sxc application/sxw
application/sxd application/mdi application/x-httpd-asp
application/x-httpd-aspx application/x-httpd-mspx
application/x-httpd-cfm application/x-httpd-jthml
application/x-httpd-pm )
]
);
my $res = $ua->request($req, $arg, 4096);
if ( $res->is_success ) {
my ($code, $date, $size, %page, $base, $content_type);
$code = $res->content;
$date = date();
$size = size($page);
$base = $res->base;
$content_type = $res->content_type;
%page = (
result => [
"$code", "$page",
"$date", "$size KB",
"$content_type"
]
);
#print "$page{'result'}->[0]\n";
print "$page{'result'}->[1]\n";
#print "$page{'result'}->[2]\n";
#print "$page{'result'}->[3]\n";
#print "$page{'result'}->[4]\n";
my $p = HTML::LinkExtor->new(\&callback);
$p->parse($code);
@web = map { $_ = url($_, $base)->abs; } @web;
@web = illegal(@web);
@web = grep_array(@web);
robot(@web);
}
else {
print "Bad link\n";
}
}
else {
print "Bad link\n";
}
}
}
}
#===========================================================================#
# Extract links #
#===========================================================================#
sub callback {
my($tag, %attr) = @_;
return if $tag ne 'a';
push(@web, values %attr);
}
#===========================================================================#
# Keep only good extension to extor #
#===========================================================================#
sub verif_link_format
{
foreach my $link_format ( @_ )
{
if ( $link_format =~ m!^http://[_a-zA-Z0-9-]+\.[_a-zA-Z0-9-]+[.a-zA-Z0-9-]*(/~|/?)[/_.a-zA-Z0-9#?&=+-]*$!i) {
return 1;
}
else
{
return 0;
}
}
}
#===========================================================================#
# Get date #
#===========================================================================#
sub date
{
my @tab_date =localtime(time);
my $jour = $tab_date[3];
my $mois = ($tab_date[4]+1);
my $annee = ($tab_date[5]+1900);
return ("$jour/$mois/$annee");
}
#===========================================================================#
# Get size #
#===========================================================================#
sub size
{
foreach my $url ( @_ ) {
my $size = HTTP::Size::get_size( $url );
if (defined $size)
{
my $result = $size/1024;
$result = floor($result);
return $result;
} else {
return "?";
}
}
} |
Partager