/[swish]/trunk/spider/swishspider
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/spider/swishspider

Parent Directory Parent Directory | Revision Log Revision Log


Revision 32 - (hide annotations)
Wed Apr 30 12:40:09 2003 UTC (20 years, 11 months ago) by dpavlin
File size: 3668 byte(s)
added make_config.pl which creates swish config file
added checkbox to hide document properties (like content, size etc)
remove comments between <html> and <head> which confuse swish

1 dpavlin 1 #!/usr/local/bin/perl -w
2     use strict;
3    
4     use LWP::UserAgent;
5     use HTTP::Status;
6     use HTML::LinkExtor;
7    
8     if (scalar(@ARGV) != 2) {
9     print STDERR "Usage: SwishSpider localpath url\n";
10     exit(1);
11     }
12    
13     my $ua = new LWP::UserAgent;
14     $ua->agent( "SwishSpider http://swish-e.org" );
15    
16     my $localpath = shift;
17     my $url = shift;
18    
19     my $no_parent_url;
20     if ($url =~ m/\s/) {
21     ($url,$no_parent_url) = split(/\s/,$url,2);
22     }
23    
24     my $request = new HTTP::Request( "GET", $url );
25     my $response = $ua->simple_request( $request );
26    
27     #
28     # Write out important meta-data. This includes the HTTP code. Depending on the
29     # code, we write out other data. Redirects have the location printed, everything
30     # else gets the content-type.
31     #
32     open( RESP, ">$localpath.response" ) || die( "Could not open response file $localpath.response" );
33    
34     print RESP $response->code() . "\n";
35     if( $response->code() == RC_OK ) {
36     print RESP $response->header( "content-type" ) . "\n";
37     } elsif( $response->is_redirect() ) {
38     my $link = $response->header( "location" );
39     if ($no_parent_url) {
40     if ($link =~ m/$no_parent_url/) {
41     # if this URL is below parent URL o.k....
42     print RESP "$link $no_parent_url\n";
43     } else {
44     # if not, crawl just this page!
45     print RESP "$link $link\n";
46     }
47     } else {
48     print RESP "$link\n";
49     }
50     }
51     close( RESP );
52    
53     #
54     # Write out the actual data assuming the retrieval was succesful. Also, if
55     # we have actual data and it's of type text/html, write out all the links it
56     # refers to
57     #
58     if( $response->code() == RC_OK ) {
59     my $contents = $response->content();
60    
61     open( CONTENTS, ">$localpath.contents" ) || die( "Could not open contents file $localpath.contents\n" );
62 dpavlin 30 # if you don't want content to be indexed, include it in
63     # <noindex> foobar </noindex> tags or surround it with comments
64     # <!-- noindex --> foobar <!-- /noindex -->
65 dpavlin 1 $contents =~ s,<noindex>.+?</noindex>,,isg;
66     $contents =~ s,<!--\s*noindex\s*-->.+?<!--\s*/noindex\s*-->,,isg;
67 dpavlin 30 # this will remove all script from indexing content
68 dpavlin 1 $contents =~ s,<script>.+?</script>,,isg;
69 dpavlin 30 # remap Windows charset to ISO-8859-2
70 dpavlin 1 $contents =~ tr/šðžèæŠÐŽÈÆ/¹ð¾èæ©Ð®ÈÆ/; # 1250 -> iso8859-2
71 dpavlin 30 # this will fix badly formatted html in form:
72     # <head><title>some text</title
73     # ></head>
74     # which will confuse indexer (or libxml2?)
75     $contents =~ s/[\n\r]^(>)/$1\n/msg;
76 dpavlin 32 # remove comments between <html> and <head> texi2html inserts them
77     # there and swish can't find document title then (libxml or swish bug?)
78     while ($contents =~ s/(<html>.*)<!--.*?-->(.*<head>)/$1$2/msi) { };
79 dpavlin 1 print CONTENTS $contents;
80     close( CONTENTS );
81    
82     if( $response->header("content-type") =~ "text/html" ) {
83     open( LINKS, ">$localpath.links" ) || die( "Could not open links file $localpath.links\n" );
84     my $p = HTML::LinkExtor->new( \&linkcb, $url );
85     $p->parse( $contents );
86    
87     close( LINKS );
88     }
89     }
90    
91    
92     sub linkcb {
93     my($tag, %links) = @_;
94 dpavlin 15 if (($tag eq "a" || $tag eq "area") && ($links{"href"})) {
95 dpavlin 1 my $link = $links{"href"};
96    
97     #
98     # Remove fragments
99     #
100     $link =~ s/(.*)#.*/$1/;
101    
102     #
103     # Remove ../ This is important because the abs() function
104     # can leave these in and cause never ending loops.
105     #
106     $link =~ s/\.\.\///g;
107    
108     # hack for apostrophe -- changes URL, but should work for most clients.
109     $link =~ s/'/%27/g;
110    
111     # hack for Apache directory listings
112     $link =~ s,/\?[NMSD]=[AD]$,/,g;
113    
114 dpavlin 15 # speedup, skip pictures
115     return if ($link =~ m/\.(gif|jpg|png)/);
116    
117 dpavlin 1 if ($no_parent_url) {
118     if ($link =~ m/$no_parent_url/) {
119     print LINKS "$link $no_parent_url\n";
120     # print STDERR "using $link\n";
121     # } else {
122     # print STDERR "skipping $link\n";
123     }
124     } else {
125     print LINKS "$link\n";
126     }
127     }
128     }
129    

Properties

Name Value
cvs2svn:cvs-rev 1.4
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26