/[hyperestraier_wrappers]/trunk/perl/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/perl/scripts/est-spider

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

revision 5 by dpavlin, Sat Sep 3 19:16:48 2005 UTC revision 27 by dpavlin, Sat Sep 17 23:07:52 2005 UTC
# Line 5  use Getopt::Long; Line 5  use Getopt::Long;
5  use File::Which;  use File::Which;
6  use HyperEstraier;  use HyperEstraier;
7  use Text::Iconv;  use Text::Iconv;
8    #use File::MMagic;
9    use File::MMagic::XS qw/:compat/;
10    
11    # do we use Node API?
12    my $node_url;
13    
14  my $collection;         # name which will be inserted  my $collection;         # name which will be inserted
15  my $path_add;           # add additional info in path  my $path_add;           # add additional info in path
# Line 19  my $result = GetOptions( Line 24  my $result = GetOptions(
24          "verbose!" => \$verbose,          "verbose!" => \$verbose,
25          "debug!" => \$verbose,          "debug!" => \$verbose,
26          "exclude=s" => \$exclude,          "exclude=s" => \$exclude,
27            "node=s" => \$node_url,
28  );  );
29    
30  my $dir = shift @ARGV || die "usage: $0 [dir]";  my $dir = shift @ARGV || die "usage: $0 [dir]";
# Line 29  my $dir = shift @ARGV || die "usage: $0 Line 35  my $dir = shift @ARGV || die "usage: $0
35    
36  my $pdftotext = which('pdftotext');  my $pdftotext = which('pdftotext');
37    
38    #my $mm = new File::MMagic('/usr/share/misc/file/magic');
39    my $mm = new File::MMagic::XS();
40    
41  my $iconv = new Text::Iconv('iso-8859-2', 'utf-8');  my $iconv = new Text::Iconv('iso-8859-2', 'utf-8');
42    
43  select(STDERR); $|=1;  select(STDERR); $|=1;
# Line 36  select(STDOUT); $|=1; Line 45  select(STDOUT); $|=1;
45    
46  print STDERR "using $pdftotext to convert pdf into html\n" if ($pdftotext && $verbose);  print STDERR "using $pdftotext to convert pdf into html\n" if ($pdftotext && $verbose);
47    
48    my $db;
49    if ($node_url) {
50            $db = HyperEstraier::Node->new($node_url);
51            $db->set_auth('admin', 'admin');
52    } else {
53            # open the database
54            $db = HyperEstraier::Database->new();
55            $db->open('/tmp/casket', $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT);
56    
57            sub signal {
58                    my($sig) = @_;
59                    print "\nCaught a SIG$sig--syncing database and shutting down\n";
60                    $db->sync();
61                    exit(0);
62            }
63    
64  # open the database          $SIG{'INT'}  = \&signal;
65  my $db = HyperEstraier::Database->new();          $SIG{'QUIT'} = \&signal;
 $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);  
66  }  }
67    
 $SIG{'INT'}  = \&signal;  
 $SIG{'QUIT'} = \&signal;  
   
68  find({ wanted => \&file,  find({ wanted => \&file,
69          follow => 1,          follow => 1,
70          no_chdir => 1          follow_skip => 2,
71            no_chdir => 1,
72  }, $dir);  }, $dir);
73    
74  print "--- sync\n";  unless ($node_url) {
75  $db->sync();          print "--- sync\n";
76            $db->sync();
77    
78  print "--- optimize...\n";          print "--- optimize...\n";
79  $db->optimize(0);          $db->optimize(0);
80    }
81  exit;  exit;
82    
83  sub dump_contents($$$$) {  sub dump_contents($$$$) {
# Line 83  sub dump_contents($$$$) { Line 100  sub dump_contents($$$$) {
100    
101          my $title = $1 if ($contents =~ m#<title>(.+)</title>#is);          my $title = $1 if ($contents =~ m#<title>(.+)</title>#is);
102    
103            # chop long titles to 100 chars
104            $title = substr($title, 0, 100) . '...' if ($title && length($title) > 100);
105            # use path if no title is found
106            $title ||= $path;
107    
108          # add attributes to the document object          # add attributes to the document object
109          $doc->add_attr('@uri', "file:///$path");          $doc->add_attr('@uri', "file:///$path");
110          $doc->add_attr('@title', $title || $path);          $doc->add_attr('@title', $iconv->convert($title));
111          $doc->add_attr('@size', $size);          $doc->add_attr('@size', $size);
112          $doc->add_attr('@mtime', $mtime);          $doc->add_attr('@mtime', $mtime);
113    
# Line 98  sub dump_contents($$$$) { Line 120  sub dump_contents($$$$) {
120  #       print $doc->dump_draft if ($verbose);  #       print $doc->dump_draft if ($verbose);
121    
122          # register the document object to the database          # register the document object to the database
123          $db->put_doc($doc, $HyperEstraier::Database::PDCLEAN);          if ($node_url) {
124                    $db->put_doc($doc);
125            } else {
126                    $db->put_doc($doc, $HyperEstraier::Database::PDCLEAN);
127            }
128    
129  }  }
130    
# Line 107  sub file { Line 133  sub file {
133          my $path = $_;          my $path = $_;
134          my $contents;          my $contents;
135    
136          return if (-l $path);          return if (-l $path || $path =~ m#/.svn#);
137    
138            my $type = $mm->checktype_filename($path);
139            $type =~ s/\s+/ /gs;
140    
141            print STDERR "# $path $type\n" if ($verbose);
142    
143          if ($pdftotext && -f $path && $path =~ m/\.pdf$/i) {          if ($pdftotext && -f $path && $type =~ m/pdf/i) {
144    
145                  print STDERR "$path {converting}" if ($verbose);                  print STDERR "$path {converting}" if ($verbose);
146    
# Line 150  sub file { Line 181  sub file {
181    
182          } else {          } else {
183    
184                  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);
185                    return if (! -f $path || (
186                            $type !~ m/html/ ||
187                            ($type !~ m#text/plain# && m/\.(php|pl|txt|info|log|text)$/)
188                    ));
189    
190                  # skip index files                  # skip index files
191                  return if (m/index_[a-z]\.html*/i || m/index_symbol\.html*/i);                  return if (m/index_[a-z]\.html*/i || m/index_symbol\.html*/i);
192    
193                  open(F,"$path") || die "can't open file: $path";                  open(F,"$path") || die "can't open file: $path";
194                  print STDERR "$path" if ($verbose);                  print STDERR "$path ($type)" if ($verbose);
195                  while(<F>) {                  while(<F>) {
196                          $contents .= "$_";                          $contents .= "$_";
197                  }                  }

Legend:
Removed from v.5  
changed lines
  Added in v.27

  ViewVC Help
Powered by ViewVC 1.1.26