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

Diff of /trunk/scripts/est-spider

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 77 by dpavlin, Mon Jan 16 21:34:14 2006 UTC revision 90 by dpavlin, Thu Jan 26 01:53:29 2006 UTC
# Line 3  use strict; Line 3  use strict;
3  use File::Find;  use File::Find;
4  use Getopt::Long;  use Getopt::Long;
5  use File::Which;  use File::Which;
 use HyperEstraier;  
6  use Search::Estraier;  use Search::Estraier;
7  use Text::Iconv;  use Text::Iconv;
8  #use File::MMagic;  #use File::MMagic;
9  use File::MMagic::XS qw/:compat/;  use File::MMagic::XS qw/:compat/;
10    
 # do we use Node API?  
 my $node_url;  
   
11  my $collection;         # name which will be inserted  my $collection;         # name which will be inserted
12  my $path_add;           # add additional info in path  my $path_add;           # add additional info in path
13  my $verbose;  my $verbose;
# Line 20  my $exclude; Line 16  my $exclude;
16  #$verbose = 1;  #$verbose = 1;
17  my $debug = 0;  my $debug = 0;
18  my $force = 0;  my $force = 0;
 my $native = 0;  
19    
20  my $result = GetOptions(  my $result = GetOptions(
21          "collection=s" => \$collection,          "collection=s" => \$collection,
# Line 28  my $result = GetOptions( Line 23  my $result = GetOptions(
23          "verbose!" => \$verbose,          "verbose!" => \$verbose,
24          "debug!" => \$debug,          "debug!" => \$debug,
25          "exclude=s" => \$exclude,          "exclude=s" => \$exclude,
         "node=s" => \$node_url,  
26          "force!" => \$force,          "force!" => \$force,
         "native!" => \$native,  
27  );  );
28    
29  my $dir = shift @ARGV || die "usage: $0 [dir]";  my ($node_url,$dir) = @ARGV;
30    
31    die <<"_END_OF_USAGE_" if (! $node_url || ! $dir);
32    usage: $0 http://localhost:1978/node/my_dir /path/to/directory
33    
34    options:
35            --collection="name of collection"
36            --path=/path/to/add/at/end
37            --exclude=regex_to_exclude
38            --verbose
39            --force
40            --debug
41    _END_OF_USAGE_
42    
43  if (! -e $dir) {  if (! -e $dir) {
44          warn "directory $dir doesn't exist, skipping\n";          warn "directory $dir doesn't exist, skipping\n";
# Line 44  if (! -e $dir) { Line 49  if (! -e $dir) {
49  #$basedir =~ s,/[^/]+$,/,;  #$basedir =~ s,/[^/]+$,/,;
50  #require "$basedir/filter.pm";  #require "$basedir/filter.pm";
51    
52  my $pdftotext = which('pdftotext');  my $filter;
53    foreach my $f (qw/pdftotext pstotext/) {
54            my $w = which($f);
55            if ($f) {
56                    $filter->{$f} = $w;
57                    print STDERR "using $f filter at $w\n" if ($verbose);
58            }
59    }
60    
61  #my $mm = new File::MMagic('/usr/share/misc/file/magic');  #my $mm = new File::MMagic('/usr/share/misc/file/magic');
62  my $mm = new File::MMagic::XS();  my $mm = new File::MMagic::XS();
# Line 54  my $iconv = new Text::Iconv('iso-8859-2' Line 66  my $iconv = new Text::Iconv('iso-8859-2'
66  select(STDERR); $|=1;  select(STDERR); $|=1;
67  select(STDOUT); $|=1;  select(STDOUT); $|=1;
68    
69  print STDERR "using $pdftotext to convert pdf into html\n" if ($pdftotext && $verbose);  my $db = new Search::Estraier::Node;
70    $db->set_url($node_url);
71  my $db;  $db->set_auth('admin', 'admin');
 if ($node_url) {  
         if ($native) {  
                 $db = HyperEstraier::Node->new($node_url);  
         } else {  
                 $db = new Search::Estraier::Node;  
                 $db->set_url($node_url);  
         }  
         $db->set_auth('admin', 'admin');  
 } else {  
         # open the database  
         $db = HyperEstraier::Database->new();  
         $db->open('/tmp/casket', $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT);  
   
         sub signal {  
                 my($sig) = @_;  
                 print "\nCaught a SIG$sig--syncing database and shutting down\n";  
                 $db->sync();  
                 exit(0);  
         }  
   
         $SIG{'INT'}  = \&signal;  
         $SIG{'QUIT'} = \&signal;  
 }  
72    
73  find({ wanted => \&file,  find({ wanted => \&file,
74          follow => 1,          follow => 1,
# Line 87  find({ wanted => \&file, Line 76  find({ wanted => \&file,
76          no_chdir => 1,          no_chdir => 1,
77  }, $dir);  }, $dir);
78    
 unless ($node_url) {  
         print "--- sync\n";  
         $db->sync();  
79    
         print "--- optimize...\n";  
         $db->optimize(0);  
 }  
80  exit;  exit;
81    
82  sub dump_contents($$$$) {  sub dump_contents {
83          my ($db,$contents,$mtime,$path) = @_;          my ($db,$contents,$mtime,$path,$size) = @_;
84    
85          return unless ($contents);      # don't die on empty files          return unless (defined($contents));     # don't die on empty files
86    
87          if ($exclude && $path =~ m/$exclude/i) {          if ($exclude && $path =~ m/$exclude/i) {
88                  print STDERR "skip: $path\n" if ($verbose);                  print STDERR "skip: $path\n" if ($verbose);
# Line 107  sub dump_contents($$$$) { Line 90  sub dump_contents($$$$) {
90          }          }
91    
92          use bytes;          use bytes;
93          my $size = length $contents;          if (! $size) {
94                    $size = length $contents;
95            }
96    
97          print STDERR " [$size]" if ($verbose);          print STDERR " [$size]" if ($verbose);
98    
99          # create a document object          # create a document object
100          my $doc;          my $doc = new Search::Estraier::Document;
         if ($native) {  
                 $doc = HyperEstraier::Document->new;  
         } else {  
                 $doc = new Search::Estraier::Document;  
         }  
101    
102          my $title = $1 if ($contents =~ m#<title>(.+?)</title>#is);          my $title = $1 if ($contents =~ m#<title>(.+?)</title>#is);
103    
# Line 132  sub dump_contents($$$$) { Line 112  sub dump_contents($$$$) {
112          $doc->add_attr('@size', $size);          $doc->add_attr('@size', $size);
113          $doc->add_attr('@mtime', $mtime);          $doc->add_attr('@mtime', $mtime);
114    
115          # html2text          if ($contents) {
116          $contents =~ s#<[^>]+/*>##gs;                  # html2text
117          $contents =~ s#\s\s+# #gs;                  $contents =~ s#<[^>]+/*>##gs;
118                    $contents =~ s#\s\s+# #gs;
119          $doc->add_text($iconv->convert($contents));  
120                    $doc->add_text($iconv->convert($contents));
121            }
122            # store path
123            $doc->add_hidden_text($path);
124            # boost title
125            $doc->add_hidden_text($title);
126    
127  #       print $doc->dump_draft if ($verbose);  #       print $doc->dump_draft if ($verbose);
128    
129          # register the document object to the database          # register the document object to the database
130          if ($node_url) {          $db->put_doc($doc);
                 $db->put_doc($doc);  
         } else {  
                 $db->put_doc($doc, $HyperEstraier::Database::PDCLEAN);  
         }  
131    
132  }  }
133    
134  sub file {  sub filter_to_pages {
135                    my ($path, $mtime, $command) = @_;
         my $path = $_;  
         my $contents;  
   
         return if (! $force && -l $path || $path =~ m#/.svn# || $path =~ m/(~|.bak)$/);  
   
         my $mtime = (stat($path))[9] || -1;  
         my $mtime_db = $db->get_doc_attr_by_uri("file:///$path", '@mtime') || -2;  
   
         if ($mtime == $mtime_db) {  
                 print STDERR "# same: $path $mtime\n" if ($verbose);  
                 return unless($force);  
         } else {  
                 print STDERR "# changed: $path $mtime != $mtime_db\n" if ($debug);  
         }  
   
         # skip files on which File::MMagic::XS croaks  
         return if ($path =~ m#\.au$#);  
   
         my $type = $mm->checktype_filename($path);  
         $type =~ s/\s+/ /gs;  
   
         print STDERR "# $path $type\n" if ($debug);  
   
         if ($pdftotext && -f $path && $type =~ m/pdf/i) {  
136    
137                  print STDERR "$path {converting}" if ($verbose);                  print STDERR "$path {converting}" if ($verbose);
138    
139                  open(F,"$pdftotext -htmlmeta \"$path\" - |") || die "can't open $pdftotext with '$path'";                  open(F,"$command |") || die "can't open $command with '$path': $!";
140                  my $html;                  my $html;
141                  while(<F>) {                  while(<F>) {
                         # XXX why pdftotext barks if I try to use this is beyond me.  
                         #$contents .= $_;  
   
142                          $html .= $_;                          $html .= $_;
143                  }                  }
144                  close(F);                  close(F);
# Line 216  sub file { Line 171  sub file {
171                          $page_nr++;                          $page_nr++;
172                  }                  }
173    
174    
175    
176    }
177    
178    sub file {
179    
180            my $path = $_;
181            my $contents;
182    
183            return if (! $force && -l $path || $path =~ m#/.svn# || $path =~ m/(~|.bak)$/);
184    
185            my $mtime = (stat($path))[9] || -1;
186            my $mtime_db = $db->get_doc_attr_by_uri("file:///$path", '@mtime') || -2;
187    
188            if ($mtime == $mtime_db) {
189                    print STDERR "# same: $path $mtime\n" if ($verbose);
190                    return unless($force);
191            } else {
192                    print STDERR "# changed: $path $mtime != $mtime_db\n" if ($debug);
193            }
194    
195            # skip files on which File::MMagic::XS croaks
196            if ($path =~ m#\.au$#) {
197                    warn "skipped '$path' to prevent File::MMagic::XS bug\n" if ($debug);
198                    return;
199            }
200    
201            my $type = $mm->checktype_filename($path);
202            $type =~ s/\s+/ /gs;
203    
204            print STDERR "# $path $type\n" if ($debug);
205    
206            if ($type =~ m/pdf/i) {
207                    if ($filter->{pdftotext}) {
208                            filter_to_pages($path, $mtime, qq( $filter->{pdftotext} -htmlmeta "$path" - ));
209                    } else {
210                            warn "skipping '$path', no pdftotext filter\n" if ($verbose);
211                            return;
212                    }
213            } elsif ($type eq 'application/postscript') {
214                    if ($filter->{pstotext}) {
215                            filter_to_pages($path, $mtime, qq( $filter->{pstotext} "$path" ));
216                    } else {
217                            warn "skipping '$path', no pstotext filter\n" if ($verbose);
218                            return;
219                    }
220          } else {          } else {
221    
222  #               return if (! -f $path || ! m/\.(html*|php|pl|txt|info|log|text)$/i);  #               return if (! -f $path || ! m/\.(html*|php|pl|txt|info|log|text)$/i);
223                  if (-f $path && $type =~ m/html/ ||                  if (-f $path && $type =~ m/html/ ||
224                          ($type !~ m#text# && $path =~ m/\.(php|pl|txt|info|log|text)$/io)                          ($path !~ m/\.(php|pl|txt|info|log|text)$/io)
225                  ) {                  ) {
226                          dump_contents($db, "<title>$path</title> $type", $mtime, $path);                          dump_contents($db, '', $mtime, $path, -s $path);
227                            return;
228                  }                  }
229    
230                  # skip index files                  # skip index files

Legend:
Removed from v.77  
changed lines
  Added in v.90

  ViewVC Help
Powered by ViewVC 1.1.26