/[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 29 by dpavlin, Sat Sep 17 23:55:09 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 12  my $verbose; Line 17  my $verbose;
17  my $exclude;  my $exclude;
18    
19  #$verbose = 1;  #$verbose = 1;
20    my $debug = 0;
21    my $force = 0;
22    
23  my $result = GetOptions(  my $result = GetOptions(
24          "collection=s" => \$collection,          "collection=s" => \$collection,
25          "path=s" => \$path_add,          "path=s" => \$path_add,
26          "verbose!" => \$verbose,          "verbose!" => \$verbose,
27          "debug!" => \$verbose,          "debug!" => \$debug,
28          "exclude=s" => \$exclude,          "exclude=s" => \$exclude,
29            "node=s" => \$node_url,
30            "force!" => \$force,
31  );  );
32    
33  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 38  my $dir = shift @ARGV || die "usage: $0
38    
39  my $pdftotext = which('pdftotext');  my $pdftotext = which('pdftotext');
40    
41    #my $mm = new File::MMagic('/usr/share/misc/file/magic');
42    my $mm = new File::MMagic::XS();
43    
44  my $iconv = new Text::Iconv('iso-8859-2', 'utf-8');  my $iconv = new Text::Iconv('iso-8859-2', 'utf-8');
45    
46  select(STDERR); $|=1;  select(STDERR); $|=1;
# Line 36  select(STDOUT); $|=1; Line 48  select(STDOUT); $|=1;
48    
49  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);
50    
51    my $db;
52    if ($node_url) {
53            $db = HyperEstraier::Node->new($node_url);
54            $db->set_auth('admin', 'admin');
55    } else {
56            # open the database
57            $db = HyperEstraier::Database->new();
58            $db->open('/tmp/casket', $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT);
59    
60            sub signal {
61                    my($sig) = @_;
62                    print "\nCaught a SIG$sig--syncing database and shutting down\n";
63                    $db->sync();
64                    exit(0);
65            }
66    
67  # open the database          $SIG{'INT'}  = \&signal;
68  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);  
69  }  }
70    
 $SIG{'INT'}  = \&signal;  
 $SIG{'QUIT'} = \&signal;  
   
71  find({ wanted => \&file,  find({ wanted => \&file,
72          follow => 1,          follow => 1,
73          no_chdir => 1          follow_skip => 2,
74            no_chdir => 1,
75  }, $dir);  }, $dir);
76    
77  print "--- sync\n";  unless ($node_url) {
78  $db->sync();          print "--- sync\n";
79            $db->sync();
80    
81  print "--- optimize...\n";          print "--- optimize...\n";
82  $db->optimize(0);          $db->optimize(0);
83    }
84  exit;  exit;
85    
86  sub dump_contents($$$$) {  sub dump_contents($$$$) {
# Line 81  sub dump_contents($$$$) { Line 101  sub dump_contents($$$$) {
101          # create a document object          # create a document object
102          my $doc = HyperEstraier::Document->new;          my $doc = HyperEstraier::Document->new;
103    
104          my $title = $1 if ($contents =~ m#<title>(.+)</title>#is);          my $title = $1 if ($contents =~ m#<title>(.+?)</title>#is);
105    
106            # chop long titles to 100 chars
107            $title = substr($title, 0, 100) . '...' if ($title && length($title) > 100);
108            # use path if no title is found
109            $title ||= $path;
110    
111          # add attributes to the document object          # add attributes to the document object
112          $doc->add_attr('@uri', "file:///$path");          $doc->add_attr('@uri', "file:///$path");
113          $doc->add_attr('@title', $title || $path);          $doc->add_attr('@title', $iconv->convert($title));
114          $doc->add_attr('@size', $size);          $doc->add_attr('@size', $size);
115          $doc->add_attr('@mtime', $mtime);          $doc->add_attr('@mtime', $mtime);
116    
# Line 98  sub dump_contents($$$$) { Line 123  sub dump_contents($$$$) {
123  #       print $doc->dump_draft if ($verbose);  #       print $doc->dump_draft if ($verbose);
124    
125          # register the document object to the database          # register the document object to the database
126          $db->put_doc($doc, $HyperEstraier::Database::PDCLEAN);          if ($node_url) {
127                    $db->put_doc($doc);
128            } else {
129                    $db->put_doc($doc, $HyperEstraier::Database::PDCLEAN);
130            }
131    
132  }  }
133    
# Line 107  sub file { Line 136  sub file {
136          my $path = $_;          my $path = $_;
137          my $contents;          my $contents;
138    
139          return if (-l $path);          return if (! $force && -l $path || $path =~ m#/.svn# || $path =~ m/(~|.bak)$/);
140    
141            my $mtime = (stat($path))[9];
142            my $mtime_db = $db->get_doc_attr_by_uri("file:///$path", '@mtime') || -2;
143    
144            if ($mtime == $mtime_db) {
145                    print STDERR "# same: $path $mtime\n" if ($verbose);
146                    return unless($force);
147            } else {
148                    print STDERR "# changed: $path $mtime != $mtime_db\n" if ($debug);
149            }
150    
151            # skip files on which File::MMagic::XS croaks
152            return if ($path =~ m#\.au$#);
153    
154            my $type = $mm->checktype_filename($path);
155            $type =~ s/\s+/ /gs;
156    
157            print STDERR "# $path $type\n" if ($debug);
158    
159          if ($pdftotext && -f $path && $path =~ m/\.pdf$/i) {          if ($pdftotext && -f $path && $type =~ m/pdf/i) {
160    
161                  print STDERR "$path {converting}" if ($verbose);                  print STDERR "$path {converting}" if ($verbose);
162    
# Line 144  sub file { Line 191  sub file {
191                          print STDERR " $page_nr" if ($verbose);                          print STDERR " $page_nr" if ($verbose);
192                          my $pre_tmp = $pre_html;                          my $pre_tmp = $pre_html;
193                          $pre_tmp =~ s/##page_nr##/$page_nr<\/title>/s;                          $pre_tmp =~ s/##page_nr##/$page_nr<\/title>/s;
194                          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) if ($page !~ m/^\s*$/s);
195                          $page_nr++;                          $page_nr++;
196                  }                  }
197    
198          } else {          } else {
199    
200                  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);
201                    return unless (-f $path && $type =~ m/html/ ||
202                            ($type =~ m#text# && $path =~ m/\.(php|pl|txt|info|log|text)$/io)
203                    );
204    
205                  # skip index files                  # skip index files
206                  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);
207    
208                  open(F,"$path") || die "can't open file: $path";                  open(F,"$path") || die "can't open file: $path";
209                  print STDERR "$path" if ($verbose);                  print STDERR "$path ($type)" if ($verbose);
210                  while(<F>) {                  while(<F>) {
211                          $contents .= "$_";                          $contents .= "$_";
212                  }                  }
# Line 167  sub file { Line 217  sub file {
217                  # add optional components to path                  # add optional components to path
218                  $path .= " $path_add" if ($path_add);                  $path .= " $path_add" if ($path_add);
219    
220                  dump_contents($db, $contents,time(), $path);                  dump_contents($db, $contents, $mtime, $path);
221          }          }
222    
223          print STDERR "\n" if ($verbose);          print STDERR "\n" if ($verbose);

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

  ViewVC Help
Powered by ViewVC 1.1.26