/[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

Contents of /trunk/scripts/est-spider

Parent Directory Parent Directory | Revision Log Revision Log


Revision 90 - (show annotations)
Thu Jan 26 01:53:29 2006 UTC (18 years, 2 months ago) by dpavlin
File size: 5906 byte(s)
created separate filter_to_pages sub, added text/postscript support via pstotext
1 #!/usr/bin/perl -w
2 use strict;
3 use File::Find;
4 use Getopt::Long;
5 use File::Which;
6 use Search::Estraier;
7 use Text::Iconv;
8 #use File::MMagic;
9 use File::MMagic::XS qw/:compat/;
10
11 my $collection; # name which will be inserted
12 my $path_add; # add additional info in path
13 my $verbose;
14 my $exclude;
15
16 #$verbose = 1;
17 my $debug = 0;
18 my $force = 0;
19
20 my $result = GetOptions(
21 "collection=s" => \$collection,
22 "path=s" => \$path_add,
23 "verbose!" => \$verbose,
24 "debug!" => \$debug,
25 "exclude=s" => \$exclude,
26 "force!" => \$force,
27 );
28
29 my ($node_url,$dir) = @ARGV;
30
31 die <<"_END_OF_USAGE_" if (! $node_url || ! $dir);
32 usage: $0 http://localhost:1978/node/my_dir /path/to/directory
33
34 options:
35 --collection="name of collection"
36 --path=/path/to/add/at/end
37 --exclude=regex_to_exclude
38 --verbose
39 --force
40 --debug
41 _END_OF_USAGE_
42
43 if (! -e $dir) {
44 warn "directory $dir doesn't exist, skipping\n";
45 exit 1;
46 }
47
48 #my $basedir = $0;
49 #$basedir =~ s,/[^/]+$,/,;
50 #require "$basedir/filter.pm";
51
52 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');
62 my $mm = new File::MMagic::XS();
63
64 my $iconv = new Text::Iconv('iso-8859-2', 'utf-8');
65
66 select(STDERR); $|=1;
67 select(STDOUT); $|=1;
68
69 my $db = new Search::Estraier::Node;
70 $db->set_url($node_url);
71 $db->set_auth('admin', 'admin');
72
73 find({ wanted => \&file,
74 follow => 1,
75 follow_skip => 2,
76 no_chdir => 1,
77 }, $dir);
78
79
80 exit;
81
82 sub dump_contents {
83 my ($db,$contents,$mtime,$path,$size) = @_;
84
85 return unless (defined($contents)); # don't die on empty files
86
87 if ($exclude && $path =~ m/$exclude/i) {
88 print STDERR "skip: $path\n" if ($verbose);
89 return;
90 }
91
92 use bytes;
93 if (! $size) {
94 $size = length $contents;
95 }
96
97 print STDERR " [$size]" if ($verbose);
98
99 # create a document object
100 my $doc = new Search::Estraier::Document;
101
102 my $title = $1 if ($contents =~ m#<title>(.+?)</title>#is);
103
104 # chop long titles to 100 chars
105 $title = substr($title, 0, 100) . '...' if ($title && length($title) > 100);
106 # use path if no title is found
107 $title ||= $path;
108
109 # add attributes to the document object
110 $doc->add_attr('@uri', "file:///$path");
111 $doc->add_attr('@title', $iconv->convert($title));
112 $doc->add_attr('@size', $size);
113 $doc->add_attr('@mtime', $mtime);
114
115 if ($contents) {
116 # html2text
117 $contents =~ s#<[^>]+/*>##gs;
118 $contents =~ s#\s\s+# #gs;
119
120 $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);
128
129 # register the document object to the database
130 $db->put_doc($doc);
131
132 }
133
134 sub filter_to_pages {
135 my ($path, $mtime, $command) = @_;
136
137 print STDERR "$path {converting}" if ($verbose);
138
139 open(F,"$command |") || die "can't open $command with '$path': $!";
140 my $html;
141 while(<F>) {
142 $html .= $_;
143 }
144 close(F);
145
146 return if (! $html);
147
148 my $file_only = $path;
149 $file_only =~ s/^.*\/([^\/]+)$/$1/g;
150
151 my ($pre_html,$pages,$post_html) = ('<html><head><title>$path :: page ##page_nr##</title></head><body><pre>',$html,'</pre></body></html>');
152
153 ($pre_html,$pages,$post_html) = ($1,$2,$3) if ($html =~ m/^(<html>.+?<pre>)(.+)(<\/pre>.+?)$/si);
154
155 if ($collection) {
156 $pre_html =~ s/<title>(.+?)<\/title>/<title>$collection :: page ##page_nr##<\/title>/si;
157 } else {
158 $pre_html =~ s/<title>(.+?)<\/title>/<title>$1 :: page ##page_nr##<\/title>/si ||
159 $pre_html =~ s/<title><\/title>/<title>$file_only :: page ##page_nr##<\/title>/si;
160 }
161
162 # save empty entry as a placeholder
163 dump_contents($db, ' ', $mtime, "$path");
164
165 my $page_nr = 1;
166 foreach my $page (split(/\f/s,$pages)) {
167 print STDERR " $page_nr" if ($verbose);
168 my $pre_tmp = $pre_html;
169 $pre_tmp =~ s/##page_nr##/$page_nr<\/title>/s;
170 dump_contents($db, $pre_tmp . $page . $post_html, $mtime, "$path#$page_nr") if ($page !~ m/^\s*$/s);
171 $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 {
221
222 # return if (! -f $path || ! m/\.(html*|php|pl|txt|info|log|text)$/i);
223 if (-f $path && $type =~ m/html/ ||
224 ($path !~ m/\.(php|pl|txt|info|log|text)$/io)
225 ) {
226 dump_contents($db, '', $mtime, $path, -s $path);
227 return;
228 }
229
230 # skip index files
231 return if (m/index_[a-z]\.html*/i || m/index_symbol\.html*/i);
232
233 open(F,"$path") || die "can't open file: $path";
234 print STDERR "$path ($type)" if ($verbose);
235 while(<F>) {
236 $contents .= "$_";
237 }
238 $contents .= "\n\n";
239
240 #$contents = filter($contents,$collection);
241
242 # add optional components to path
243 $path .= " $path_add" if ($path_add);
244
245 dump_contents($db, $contents, $mtime, $path);
246 }
247
248 print STDERR "\n" if ($verbose);
249 # die "zero size content in '$path'" if (! $contents);
250
251 }
252

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26