/[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 178 by dpavlin, Tue Aug 15 16:38:06 2006 UTC revision 197 by dpavlin, Fri Jan 5 22:19:01 2007 UTC
# Line 8  use Text::Iconv; Line 8  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/;  use Time::HiRes qw/time/;
11    use HTML::TreeBuilder;
12    use Data::Dump qw/dump/;
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 18  my $exclude; Line 20  my $exclude;
20  my $debug = 0;  my $debug = 0;
21  my $force = 0;  my $force = 0;
22  my $all = 0;  my $all = 0;
23    my $skip_images = 0;
24    
25  my $result = GetOptions(  my $result = GetOptions(
26          "collection=s" => \$collection,          "collection=s" => \$collection,
# Line 27  my $result = GetOptions( Line 30  my $result = GetOptions(
30          "exclude=s" => \$exclude,          "exclude=s" => \$exclude,
31          "force!" => \$force,          "force!" => \$force,
32          "all!" => \$all,          "all!" => \$all,
33            "skip-images!" => \$skip_images,
34  );  );
35    
36  my ($node_url,$dir) = @ARGV;  my ($node_url,$dir) = @ARGV;
# Line 38  options: Line 42  options:
42          --collection="name of collection"          --collection="name of collection"
43          --path=/path/to/add/at/end          --path=/path/to/add/at/end
44          --exclude=regex_to_exclude          --exclude=regex_to_exclude
45            --skip-images
46          --verbose          --verbose
47          --force          --force
48          --debug          --debug
# Line 59  my $start_t = time(); Line 64  my $start_t = time();
64  my $filter;  my $filter;
65  foreach my $f (qw/pdftotext pstotext/) {  foreach my $f (qw/pdftotext pstotext/) {
66          my $w = which($f);          my $w = which($f);
67          if ($f) {          if ($w) {
68                  $filter->{$f} = $w;                  $filter->{$f} = $w;
69                  print STDERR "using $f filter at $w\n" if ($verbose);                  print STDERR "using $f filter at $w\n" if ($verbose);
70          }          }
# Line 78  my $db = new Search::Estraier::Node( Line 83  my $db = new Search::Estraier::Node(
83          user => 'admin',          user => 'admin',
84          passwd => 'admin',          passwd => 'admin',
85          croak_on_error => 1,          croak_on_error => 1,
86            create => 1,
87  );  );
88    
89    #
90    # check if hhc file exists, and if it does, extract information from it
91    #
92    
93    my $hhc_file;
94    # try to find hhc
95    find({ wanted => sub {
96                    return unless( m!\.hhc$!i );
97                    $hhc_file = $_;
98                    warn "using $hhc_file for tree structure\n";
99            },
100            follow => 1,
101            follow_skip => 2,
102            no_chdir => 1,
103    }, $dir);
104    
105    my $meta;
106    
107    if ($hhc_file) {
108    
109            sub param {
110                    my ($el) = @_;
111    
112                    my $n;
113                    foreach my $p ( $el->find('param') ) {
114                            $n->{ $p->attr('name') } = $p->attr('value');
115                    }
116    
117                    if ( ! defined($n->{Local}) ) {
118                            warn "### skipped = ",dump($n),$/;
119                            return;
120                    }
121    
122                    my $path = $dir . '/' . $n->{Local};
123                    $path =~ s!//!/!g;
124                    $path = lc($path);
125    
126                    $n->{path} = $path;
127    
128                    my $nr = $n->{ImageNumber} || return $n;
129    
130                    if ($nr == 27) {
131                            $meta->{title} = $n->{Name};
132                            $meta->{index_path} = $path;
133                    } elsif ($nr == 21) {
134                            $meta->{toc_path} = $path;
135                    } elsif ($nr == 1) {
136                            $meta->{foreword_path} = $path;
137                    } elsif ($nr == 11) {
138                            # nop
139                    } else {
140                            warn "unknown ImageNumber: $nr\n";
141                    }
142    
143                    return $n;
144            }
145    
146            my $tree = HTML::TreeBuilder->new;
147            $tree->parse_file($hhc_file);
148    
149            my $prefix = $collection ? ( $collection . ' :: ' ) : '';
150    
151            my @prefix;
152            my $depth = 0;
153    
154            foreach my $e ( $tree->look_down( sub { $_[0]->tag =~ m/(object)/ } ) ) {
155    
156    #               printf("%05s %s\n", $e->parent->address(), $e->as_HTML() );
157    
158                    my $l = ($e->depth() / 2) - 1;
159    
160                    $prefix[ 0 ] = $meta->{title} || '';
161    
162                    my $n = param($e);
163                    $prefix[ $l ] = $n->{Name};
164    
165                    next unless ($n->{path});
166    
167                    my $t = '';
168                    my @p;
169                    foreach my $i ( 0 .. $l ) {
170                            push @p, $prefix[ $i ] if ($prefix[ $i ]);
171                    }
172                    $t = join(' :: ', @p ) if (@p);
173    
174                    $meta->{path2title}->{ $n->{path} } = $t;
175    
176            }
177    
178            $tree->delete;
179    
180    }
181    
182    
183  find({ wanted => \&file,  find({ wanted => \&file,
184          follow => 1,          follow => 1,
185          follow_skip => 2,          follow_skip => 2,
# Line 116  sub dump_contents { Line 216  sub dump_contents {
216          # create a document object          # create a document object
217          my $doc = new Search::Estraier::Document;          my $doc = new Search::Estraier::Document;
218    
219          my $title = $1 if ($contents =~ m#<title>(.+?)</title>#is);          my $title;
220    
221            if ( defined($meta->{path2title}->{lc($path)}) ) {
222                    $title = $meta->{path2title}->{lc($path)};
223                    warn " $title\n";
224            } else {
225    
226          # chop long titles to 100 chars                  $title = $1 if ($contents =~ m#<title>(.+?)</title>#is);
227          $title = substr($title, 0, 100) . '...' if ($title && length($title) > 100);  
228          # use path if no title is found                  # chop long titles to 100 chars
229          $title ||= $path;                  $title = substr($title, 0, 100) . '...' if ($title && length($title) > 100);
230                    # use path if no title is found
231                    $title ||= $path;
232            
233            }
234    
235          # add attributes to the document object          # add attributes to the document object
236          $doc->add_attr('@uri', "file:///$path");          $doc->add_attr('@uri', "file:///$path");
# Line 131  sub dump_contents { Line 240  sub dump_contents {
240    
241          if ($contents) {          if ($contents) {
242                  # html2text                  # html2text
243                    $contents =~ s#<script.*?</script>##gis;
244                  $contents =~ s#<[^>]+/*>##gs;                  $contents =~ s#<[^>]+/*>##gs;
245                  $contents =~ s#\s\s+# #gs;                  $contents =~ s#\s\s+# #gs;
246    
# Line 203  sub file { Line 313  sub file {
313          return if (! $all && -d $path);          return if (! $all && -d $path);
314    
315          my $mtime = (stat($path))[9] || -1;          my $mtime = (stat($path))[9] || -1;
316          my $mtime_db = eval { $db->get_doc_attr_by_uri("file:///$path", '@mtime') };          my $mtime_db;
317            eval { $db->get_doc_attr_by_uri("file:///$path", '@mtime') } unless ($force);
318          $mtime_db ||= -2;          $mtime_db ||= -2;
319    
320          if ($mtime == $mtime_db) {          if ($mtime == $mtime_db) {
# Line 238  sub file { Line 349  sub file {
349                          warn "skipping '$path', no pstotext filter\n" if ($verbose);                          warn "skipping '$path', no pstotext filter\n" if ($verbose);
350                          return;                          return;
351                  }                  }
352            } elsif ($type =~ m!^image/! && $skip_images) {
353                    warn "skipping image '$path'\n" if ($verbose);
354                    return; # don't index images
355          } else {          } else {
356    
357  #               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);

Legend:
Removed from v.178  
changed lines
  Added in v.197

  ViewVC Help
Powered by ViewVC 1.1.26