/[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 89 by dpavlin, Wed Jan 25 23:38:57 2006 UTC revision 90 by dpavlin, Thu Jan 26 01:53:29 2006 UTC
# Line 49  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 59  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    
 print STDERR "using $pdftotext to convert pdf into html\n" if ($pdftotext && $verbose);  
   
69  my $db = new Search::Estraier::Node;  my $db = new Search::Estraier::Node;
70  $db->set_url($node_url);  $db->set_url($node_url);
71  $db->set_auth('admin', 'admin');  $db->set_auth('admin', 'admin');
# Line 74  find({ wanted => \&file, Line 79  find({ wanted => \&file,
79    
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 85  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    
# Line 105  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    
120          $doc->add_text($iconv->convert($contents));                  $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    
# Line 118  sub dump_contents($$$$) { Line 131  sub dump_contents($$$$) {
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 185  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.89  
changed lines
  Added in v.90

  ViewVC Help
Powered by ViewVC 1.1.26