5 |
use HTTP::Status; |
use HTTP::Status; |
6 |
use HTML::LinkExtor; |
use HTML::LinkExtor; |
7 |
|
|
8 |
|
my $basedir = $0; |
9 |
|
$basedir =~ s,/[^/]+$,/,; |
10 |
|
require "$basedir/filter.pm"; |
11 |
|
|
12 |
if (scalar(@ARGV) != 2) { |
if (scalar(@ARGV) != 2) { |
13 |
print STDERR "Usage: SwishSpider localpath url\n"; |
print STDERR "Usage: SwishSpider localpath url\n"; |
14 |
exit(1); |
exit(1); |
24 |
if ($url =~ m/\s/) { |
if ($url =~ m/\s/) { |
25 |
($no_parent_url,$url) = split(/\s/,$url,2); |
($no_parent_url,$url) = split(/\s/,$url,2); |
26 |
# old scheme had URL, no parent and new is reverse |
# old scheme had URL, no parent and new is reverse |
27 |
($url,$no_parent_url) = ($no_parent_url,$url) if ($no_parent_url =~ m/$url/); |
($url,$no_parent_url) = ($no_parent_url,$url) if ($no_parent_url =~ m/\Q$url\E/); |
28 |
} |
} |
29 |
|
|
30 |
my $request = new HTTP::Request( "GET", $url ); |
my $request = new HTTP::Request( "GET", $url ); |
31 |
my $response = $ua->simple_request( $request ); |
my $response = $ua->simple_request( $request ); |
32 |
|
my $urlbase = $response->base; |
33 |
|
$urlbase =~ s,/[^/]*$,/,; # remove filename |
34 |
|
|
35 |
# |
# |
36 |
# Write out important meta-data. This includes the HTTP code. Depending on the |
# Write out important meta-data. This includes the HTTP code. Depending on the |
69 |
open( CONTENTS, ">$localpath.contents" ) || die( "Could not open contents file $localpath.contents\n" ); |
open( CONTENTS, ">$localpath.contents" ) || die( "Could not open contents file $localpath.contents\n" ); |
70 |
# fixup just HTML files |
# fixup just HTML files |
71 |
if ($response->header("content-type") =~ "text/html") { |
if ($response->header("content-type") =~ "text/html") { |
72 |
# if you don't want content to be indexed, include it in |
$contents = filter($contents); |
|
# <noindex> foobar </noindex> tags or surround it with comments |
|
|
# <!-- noindex --> foobar <!-- /noindex --> |
|
|
$contents =~ s,<noindex>.+?</noindex>,,isg; |
|
|
$contents =~ s,<!--\s*noindex\s*-->.+?<!--\s*/noindex\s*-->,,isg; |
|
|
# this will remove all script from indexing content |
|
|
$contents =~ s,<script>.+?</script>,,isg; |
|
|
# remap Windows charset to ISO-8859-2 |
|
|
$contents =~ tr/šðžèæŠÐŽÈÆ/¹ð¾èæ©Ð®ÈÆ/; # 1250 -> iso8859-2 |
|
|
# this will fix badly formatted html in form: |
|
|
# <head><title>some text</title |
|
|
# ></head> |
|
|
# which will confuse indexer (or libxml2?) |
|
|
$contents =~ s/[\n\r]^(>)/$1\n/msg; |
|
|
# remove comments between <html> and <head> texi2html inserts them |
|
|
# there and swish can't find document title then (libxml or swish bug?) |
|
|
while ($contents =~ s/(<html>.*)<!--.*?-->(.*<head>)/$1$2/msi) { }; |
|
73 |
} |
} |
74 |
print CONTENTS $contents; |
print CONTENTS $contents; |
75 |
close( CONTENTS ); |
close( CONTENTS ); |
79 |
my $p = HTML::LinkExtor->new( \&linkcb, $url ); |
my $p = HTML::LinkExtor->new( \&linkcb, $url ); |
80 |
$p->parse( $contents ); |
$p->parse( $contents ); |
81 |
|
|
82 |
|
|
83 |
|
|
84 |
close( LINKS ); |
close( LINKS ); |
85 |
} |
} |
86 |
} |
} |
102 |
# |
# |
103 |
$link =~ s/\.\.\///g; |
$link =~ s/\.\.\///g; |
104 |
|
|
105 |
|
if ($link =~ m,javascript:displayWindow\((.+)\),i) { |
106 |
|
my $arg = $1; |
107 |
|
$arg =~ s/%([a-f0-9][a-f][0-9])/chr(hex($1))/eg; |
108 |
|
($link,undef) = split(',',$arg,2); |
109 |
|
$link =~ s/^['"]//; |
110 |
|
$link =~ s/['"]$//; |
111 |
|
$link = $urlbase.$link; |
112 |
|
} |
113 |
|
|
114 |
# hack for apostrophe -- changes URL, but should work for most clients. |
# hack for apostrophe -- changes URL, but should work for most clients. |
115 |
$link =~ s/'/%27/g; |
$link =~ s/'/%27/g; |
116 |
|
|