--- trunk/perl/scripts/est-spider 2005/09/10 09:31:53 18
+++ trunk/perl/scripts/est-spider 2005/09/18 18:21:06 30
@@ -5,6 +5,8 @@
use File::Which;
use HyperEstraier;
use Text::Iconv;
+#use File::MMagic;
+use File::MMagic::XS qw/:compat/;
# do we use Node API?
my $node_url;
@@ -15,14 +17,17 @@
my $exclude;
#$verbose = 1;
+my $debug = 0;
+my $force = 0;
my $result = GetOptions(
"collection=s" => \$collection,
"path=s" => \$path_add,
"verbose!" => \$verbose,
- "debug!" => \$verbose,
+ "debug!" => \$debug,
"exclude=s" => \$exclude,
"node=s" => \$node_url,
+ "force!" => \$force,
);
my $dir = shift @ARGV || die "usage: $0 [dir]";
@@ -33,6 +38,9 @@
my $pdftotext = which('pdftotext');
+#my $mm = new File::MMagic('/usr/share/misc/file/magic');
+my $mm = new File::MMagic::XS();
+
my $iconv = new Text::Iconv('iso-8859-2', 'utf-8');
select(STDERR); $|=1;
@@ -62,7 +70,8 @@
find({ wanted => \&file,
follow => 1,
- no_chdir => 1
+ follow_skip => 2,
+ no_chdir => 1,
}, $dir);
unless ($node_url) {
@@ -92,11 +101,16 @@
# create a document object
my $doc = HyperEstraier::Document->new;
- my $title = $1 if ($contents =~ m#
(.+)#is);
+ my $title = $1 if ($contents =~ m#(.+?)#is);
+
+ # chop long titles to 100 chars
+ $title = substr($title, 0, 100) . '...' if ($title && length($title) > 100);
+ # use path if no title is found
+ $title ||= $path;
# add attributes to the document object
$doc->add_attr('@uri', "file:///$path");
- $doc->add_attr('@title', $title || $path);
+ $doc->add_attr('@title', $iconv->convert($title));
$doc->add_attr('@size', $size);
$doc->add_attr('@mtime', $mtime);
@@ -122,9 +136,27 @@
my $path = $_;
my $contents;
- return if (-l $path);
+ return if (! $force && -l $path || $path =~ m#/.svn# || $path =~ m/(~|.bak)$/);
+
+ my $mtime = (stat($path))[9];
+ 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 && $path =~ m/\.pdf$/i) {
+ if ($pdftotext && -f $path && $type =~ m/pdf/i) {
print STDERR "$path {converting}" if ($verbose);
@@ -159,19 +191,22 @@
print STDERR " $page_nr" if ($verbose);
my $pre_tmp = $pre_html;
$pre_tmp =~ s/##page_nr##/$page_nr<\/title>/s;
- dump_contents($db, $pre_tmp . $page . $post_html,time(), $path) if ($page !~ m/^\s*$/s);
+ dump_contents($db, $pre_tmp . $page . $post_html, $mtime, "$path#$page_nr") if ($page !~ m/^\s*$/s);
$page_nr++;
}
} else {
- 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);
+ return unless (-f $path && $type =~ m/html/ ||
+ ($type =~ m#text# && $path =~ m/\.(php|pl|txt|info|log|text)$/io)
+ );
# skip index files
return if (m/index_[a-z]\.html*/i || m/index_symbol\.html*/i);
open(F,"$path") || die "can't open file: $path";
- print STDERR "$path" if ($verbose);
+ print STDERR "$path ($type)" if ($verbose);
while() {
$contents .= "$_";
}
@@ -182,7 +217,7 @@
# add optional components to path
$path .= " $path_add" if ($path_add);
- dump_contents($db, $contents,time(), $path);
+ dump_contents($db, $contents, $mtime, $path);
}
print STDERR "\n" if ($verbose);