/[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 40 - (hide annotations)
Sun Jun 1 11:45:19 2003 UTC (20 years, 10 months ago) by dpavlin
File size: 3897 byte(s)
- support for listing of files in .tar.gz; decompressing of .gz and .bz2
  content
- changed order of arguments for swishspider: now baseurl,url (but it's
  backwards compatibile, so your old configurations will work)
- do html fixup just on html files (to prevent binary archive corruption)
- crawl sites that have frames

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26