/[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 30 - (hide annotations)
Mon Mar 24 09:57:44 2003 UTC (21 years ago) by dpavlin
File size: 3449 byte(s)
added instructions about formating of html before indexing it (and added
ability to unroll wrongly splited tags in form which is acceptable to 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 1 print CONTENTS $contents;
77     close( CONTENTS );
78    
79     if( $response->header("content-type") =~ "text/html" ) {
80     open( LINKS, ">$localpath.links" ) || die( "Could not open links file $localpath.links\n" );
81     my $p = HTML::LinkExtor->new( \&linkcb, $url );
82     $p->parse( $contents );
83    
84     close( LINKS );
85     }
86     }
87    
88    
89     sub linkcb {
90     my($tag, %links) = @_;
91 dpavlin 15 if (($tag eq "a" || $tag eq "area") && ($links{"href"})) {
92 dpavlin 1 my $link = $links{"href"};
93    
94     #
95     # Remove fragments
96     #
97     $link =~ s/(.*)#.*/$1/;
98    
99     #
100     # Remove ../ This is important because the abs() function
101     # can leave these in and cause never ending loops.
102     #
103     $link =~ s/\.\.\///g;
104    
105     # hack for apostrophe -- changes URL, but should work for most clients.
106     $link =~ s/'/%27/g;
107    
108     # hack for Apache directory listings
109     $link =~ s,/\?[NMSD]=[AD]$,/,g;
110    
111 dpavlin 15 # speedup, skip pictures
112     return if ($link =~ m/\.(gif|jpg|png)/);
113    
114 dpavlin 1 if ($no_parent_url) {
115     if ($link =~ m/$no_parent_url/) {
116     print LINKS "$link $no_parent_url\n";
117     # print STDERR "using $link\n";
118     # } else {
119     # print STDERR "skipping $link\n";
120     }
121     } else {
122     print LINKS "$link\n";
123     }
124     }
125     }
126    

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26