/[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 118 by dpavlin, Sun Apr 16 23:22:54 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    use Time::HiRes qw/time/;
 # do we use Node API?  
 my $node_url;  
11    
12  my $collection;         # name which will be inserted  my $collection;         # name which will be inserted
13  my $path_add;           # add additional info in path  my $path_add;           # add additional info in path
# Line 20  my $exclude; Line 17  my $exclude;
17  #$verbose = 1;  #$verbose = 1;
18  my $debug = 0;  my $debug = 0;
19  my $force = 0;  my $force = 0;
 my $native = 0;  
20    
21  my $result = GetOptions(  my $result = GetOptions(
22          "collection=s" => \$collection,          "collection=s" => \$collection,
# Line 28  my $result = GetOptions( Line 24  my $result = GetOptions(
24          "verbose!" => \$verbose,          "verbose!" => \$verbose,
25          "debug!" => \$debug,          "debug!" => \$debug,
26          "exclude=s" => \$exclude,          "exclude=s" => \$exclude,
         "node=s" => \$node_url,  
27          "force!" => \$force,          "force!" => \$force,
         "native!" => \$native,  
28  );  );
29    
30  my $dir = shift @ARGV || die "usage: $0 [dir]";  my ($node_url,$dir) = @ARGV;
31    
32    die <<"_END_OF_USAGE_" if (! $node_url || ! $dir);
33    usage: $0 http://localhost:1978/node/my_dir /path/to/directory
34    
35    options:
36            --collection="name of collection"
37            --path=/path/to/add/at/end
38            --exclude=regex_to_exclude
39            --verbose
40            --force
41            --debug
42    _END_OF_USAGE_
43    
44  if (! -e $dir) {  if (! -e $dir) {
45          warn "directory $dir doesn't exist, skipping\n";          warn "directory $dir doesn't exist, skipping\n";
# Line 44  if (! -e $dir) { Line 50  if (! -e $dir) {
50  #$basedir =~ s,/[^/]+$,/,;  #$basedir =~ s,/[^/]+$,/,;
51  #require "$basedir/filter.pm";  #require "$basedir/filter.pm";
52    
53  my $pdftotext = which('pdftotext');  my $docs = 0;
54    my $start_t = time();
55    
56    my $filter;
57    foreach my $f (qw/pdftotext pstotext/) {
58            my $w = which($f);
59            if ($f) {
60                    $filter->{$f} = $w;
61                    print STDERR "using $f filter at $w\n" if ($verbose);
62            }
63    }
64    
65  #my $mm = new File::MMagic('/usr/share/misc/file/magic');  #my $mm = new File::MMagic('/usr/share/misc/file/magic');
66  my $mm = new File::MMagic::XS();  my $mm = new File::MMagic::XS();
# Line 54  my $iconv = new Text::Iconv('iso-8859-2' Line 70  my $iconv = new Text::Iconv('iso-8859-2'
70  select(STDERR); $|=1;  select(STDERR); $|=1;
71  select(STDOUT); $|=1;  select(STDOUT); $|=1;
72    
73  print STDERR "using $pdftotext to convert pdf into html\n" if ($pdftotext && $verbose);  my $db = new Search::Estraier::Node;
74    $db->set_url($node_url);
75  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;  
 }  
76    
77  find({ wanted => \&file,  find({ wanted => \&file,
78          follow => 1,          follow => 1,
# Line 87  find({ wanted => \&file, Line 80  find({ wanted => \&file,
80          no_chdir => 1,          no_chdir => 1,
81  }, $dir);  }, $dir);
82    
83  unless ($node_url) {  my $dur = (time() - $start_t) || 1;
84          print "--- sync\n";  printf STDERR "%d documents in %.2fs [%.2f docs/s]\n", $docs, $dur, ($docs / $dur);
         $db->sync();  
85    
         print "--- optimize...\n";  
         $db->optimize(0);  
 }  
86  exit;  exit;
87    
88  sub dump_contents($$$$) {  sub dump_contents {
89          my ($db,$contents,$mtime,$path) = @_;          my ($db,$contents,$mtime,$path,$size) = @_;
90    
91          return unless ($contents);      # don't die on empty files          return unless (defined($contents));     # don't die on empty files
92    
93          if ($exclude && $path =~ m/$exclude/i) {          if ($exclude && $path =~ m/$exclude/i) {
94                  print STDERR "skip: $path\n" if ($verbose);                  print STDERR "skip: $path\n" if ($verbose);
# Line 107  sub dump_contents($$$$) { Line 96  sub dump_contents($$$$) {
96          }          }
97    
98          use bytes;          use bytes;
99          my $size = length $contents;          if (! $size) {
100                    $size = length $contents;
101            }
102    
103          print STDERR " [$size]" if ($verbose);          print STDERR " [$size]" if ($verbose);
104    
105          # create a document object          # create a document object
106          my $doc;          my $doc = new Search::Estraier::Document;
         if ($native) {  
                 $doc = HyperEstraier::Document->new;  
         } else {  
                 $doc = new Search::Estraier::Document;  
         }  
107    
108          my $title = $1 if ($contents =~ m#<title>(.+?)</title>#is);          my $title = $1 if ($contents =~ m#<title>(.+?)</title>#is);
109    
# Line 132  sub dump_contents($$$$) { Line 118  sub dump_contents($$$$) {
118          $doc->add_attr('@size', $size);          $doc->add_attr('@size', $size);
119          $doc->add_attr('@mtime', $mtime);          $doc->add_attr('@mtime', $mtime);
120    
121          # html2text          if ($contents) {
122          $contents =~ s#<[^>]+/*>##gs;                  # html2text
123          $contents =~ s#\s\s+# #gs;                  $contents =~ s#<[^>]+/*>##gs;
124                    $contents =~ s#\s\s+# #gs;
125          $doc->add_text($iconv->convert($contents));  
126                    $doc->add_text($iconv->convert($contents));
127            }
128            # store path
129            $doc->add_hidden_text($path);
130            # boost title
131            $doc->add_hidden_text($title);
132    
133  #       print $doc->dump_draft if ($verbose);  #       print $doc->dump_draft if ($verbose);
134    
135          # register the document object to the database          # register the document object to the database
136          if ($node_url) {          $db->put_doc($doc);
                 $db->put_doc($doc);  
         } else {  
                 $db->put_doc($doc, $HyperEstraier::Database::PDCLEAN);  
         }  
   
 }  
   
 sub file {  
   
         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);  
         }  
137    
138          # skip files on which File::MMagic::XS croaks          $docs++;
         return if ($path =~ m#\.au$#);  
139    
140          my $type = $mm->checktype_filename($path);  }
         $type =~ s/\s+/ /gs;  
   
         print STDERR "# $path $type\n" if ($debug);  
141    
142          if ($pdftotext && -f $path && $type =~ m/pdf/i) {  sub filter_to_pages {
143                    my ($path, $mtime, $command) = @_;
144    
145                  print STDERR "$path {converting}" if ($verbose);                  print STDERR "$path {converting}" if ($verbose);
146    
147                  open(F,"$pdftotext -htmlmeta \"$path\" - |") || die "can't open $pdftotext with '$path'";                  open(F,"$command |") || die "can't open $command with '$path': $!";
148                  my $html;                  my $html;
149                  while(<F>) {                  while(<F>) {
                         # XXX why pdftotext barks if I try to use this is beyond me.  
                         #$contents .= $_;  
   
150                          $html .= $_;                          $html .= $_;
151                  }                  }
152                  close(F);                  close(F);
# Line 216  sub file { Line 179  sub file {
179                          $page_nr++;                          $page_nr++;
180                  }                  }
181    
182    
183    
184    }
185    
186    sub file {
187    
188            my $path = $_;
189            my $contents;
190    
191            return if (! $force && -l $path || $path =~ m#/.svn# || $path =~ m/(~|.bak)$/);
192    
193            my $mtime = (stat($path))[9] || -1;
194            my $mtime_db = $db->get_doc_attr_by_uri("file:///$path", '@mtime') || -2;
195    
196            if ($mtime == $mtime_db) {
197                    print STDERR "# same: $path $mtime\n" if ($verbose);
198                    return unless($force);
199            } else {
200                    print STDERR "# changed: $path $mtime != $mtime_db\n" if ($debug);
201            }
202    
203            # skip files on which File::MMagic::XS croaks
204            if ($path =~ m#\.au$#) {
205                    warn "skipped '$path' to prevent File::MMagic::XS bug\n" if ($debug);
206                    return;
207            }
208    
209            my $type = $mm->checktype_filename($path);
210            $type =~ s/\s+/ /gs;
211    
212            print STDERR "# $path $type\n" if ($debug);
213    
214            if ($type =~ m/pdf/i) {
215                    if ($filter->{pdftotext}) {
216                            filter_to_pages($path, $mtime, qq( $filter->{pdftotext} -htmlmeta "$path" - ));
217                    } else {
218                            warn "skipping '$path', no pdftotext filter\n" if ($verbose);
219                            return;
220                    }
221            } elsif ($type eq 'application/postscript') {
222                    if ($filter->{pstotext}) {
223                            filter_to_pages($path, $mtime, qq( $filter->{pstotext} "$path" ));
224                    } else {
225                            warn "skipping '$path', no pstotext filter\n" if ($verbose);
226                            return;
227                    }
228          } else {          } else {
229    
230  #               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);
231                  if (-f $path && $type =~ m/html/ ||                  if (-f $path && $type =~ m/html/ ||
232                          ($type !~ m#text# && $path =~ m/\.(php|pl|txt|info|log|text)$/io)                          ($path !~ m/\.(php|pl|txt|info|log|text)$/io)
233                  ) {                  ) {
234                          dump_contents($db, "<title>$path</title> $type", $mtime, $path);                          dump_contents($db, '', $mtime, $path, -s $path);
235                            return;
236                  }                  }
237    
238                  # skip index files                  # skip index files

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

  ViewVC Help
Powered by ViewVC 1.1.26