/[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 89 - (show annotations)
Wed Jan 25 23:38:57 2006 UTC (18 years, 2 months ago) by dpavlin
File size: 5297 byte(s)
removed dependency on (optional in the first place) native HyperEstraier module
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 $pdftotext = which('pdftotext');
53
54 #my $mm = new File::MMagic('/usr/share/misc/file/magic');
55 my $mm = new File::MMagic::XS();
56
57 my $iconv = new Text::Iconv('iso-8859-2', 'utf-8');
58
59 select(STDERR); $|=1;
60 select(STDOUT); $|=1;
61
62 print STDERR "using $pdftotext to convert pdf into html\n" if ($pdftotext && $verbose);
63
64 my $db = new Search::Estraier::Node;
65 $db->set_url($node_url);
66 $db->set_auth('admin', 'admin');
67
68 find({ wanted => \&file,
69 follow => 1,
70 follow_skip => 2,
71 no_chdir => 1,
72 }, $dir);
73
74
75 exit;
76
77 sub dump_contents($$$$) {
78 my ($db,$contents,$mtime,$path) = @_;
79
80 return unless ($contents); # don't die on empty files
81
82 if ($exclude && $path =~ m/$exclude/i) {
83 print STDERR "skip: $path\n" if ($verbose);
84 return;
85 }
86
87 use bytes;
88 my $size = length $contents;
89
90 print STDERR " [$size]" if ($verbose);
91
92 # create a document object
93 my $doc = new Search::Estraier::Document;
94
95 my $title = $1 if ($contents =~ m#<title>(.+?)</title>#is);
96
97 # chop long titles to 100 chars
98 $title = substr($title, 0, 100) . '...' if ($title && length($title) > 100);
99 # use path if no title is found
100 $title ||= $path;
101
102 # add attributes to the document object
103 $doc->add_attr('@uri', "file:///$path");
104 $doc->add_attr('@title', $iconv->convert($title));
105 $doc->add_attr('@size', $size);
106 $doc->add_attr('@mtime', $mtime);
107
108 # html2text
109 $contents =~ s#<[^>]+/*>##gs;
110 $contents =~ s#\s\s+# #gs;
111
112 $doc->add_text($iconv->convert($contents));
113
114 # print $doc->dump_draft if ($verbose);
115
116 # register the document object to the database
117 $db->put_doc($doc);
118
119 }
120
121 sub file {
122
123 my $path = $_;
124 my $contents;
125
126 return if (! $force && -l $path || $path =~ m#/.svn# || $path =~ m/(~|.bak)$/);
127
128 my $mtime = (stat($path))[9] || -1;
129 my $mtime_db = $db->get_doc_attr_by_uri("file:///$path", '@mtime') || -2;
130
131 if ($mtime == $mtime_db) {
132 print STDERR "# same: $path $mtime\n" if ($verbose);
133 return unless($force);
134 } else {
135 print STDERR "# changed: $path $mtime != $mtime_db\n" if ($debug);
136 }
137
138 # skip files on which File::MMagic::XS croaks
139 return if ($path =~ m#\.au$#);
140
141 my $type = $mm->checktype_filename($path);
142 $type =~ s/\s+/ /gs;
143
144 print STDERR "# $path $type\n" if ($debug);
145
146 if ($pdftotext && -f $path && $type =~ m/pdf/i) {
147
148 print STDERR "$path {converting}" if ($verbose);
149
150 open(F,"$pdftotext -htmlmeta \"$path\" - |") || die "can't open $pdftotext with '$path'";
151 my $html;
152 while(<F>) {
153 # XXX why pdftotext barks if I try to use this is beyond me.
154 #$contents .= $_;
155
156 $html .= $_;
157 }
158 close(F);
159
160 return if (! $html);
161
162 my $file_only = $path;
163 $file_only =~ s/^.*\/([^\/]+)$/$1/g;
164
165 my ($pre_html,$pages,$post_html) = ('<html><head><title>$path :: page ##page_nr##</title></head><body><pre>',$html,'</pre></body></html>');
166
167 ($pre_html,$pages,$post_html) = ($1,$2,$3) if ($html =~ m/^(<html>.+?<pre>)(.+)(<\/pre>.+?)$/si);
168
169 if ($collection) {
170 $pre_html =~ s/<title>(.+?)<\/title>/<title>$collection :: page ##page_nr##<\/title>/si;
171 } else {
172 $pre_html =~ s/<title>(.+?)<\/title>/<title>$1 :: page ##page_nr##<\/title>/si ||
173 $pre_html =~ s/<title><\/title>/<title>$file_only :: page ##page_nr##<\/title>/si;
174 }
175
176 # save empty entry as a placeholder
177 dump_contents($db, ' ', $mtime, "$path");
178
179 my $page_nr = 1;
180 foreach my $page (split(/\f/s,$pages)) {
181 print STDERR " $page_nr" if ($verbose);
182 my $pre_tmp = $pre_html;
183 $pre_tmp =~ s/##page_nr##/$page_nr<\/title>/s;
184 dump_contents($db, $pre_tmp . $page . $post_html, $mtime, "$path#$page_nr") if ($page !~ m/^\s*$/s);
185 $page_nr++;
186 }
187
188 } else {
189
190 # return if (! -f $path || ! m/\.(html*|php|pl|txt|info|log|text)$/i);
191 if (-f $path && $type =~ m/html/ ||
192 ($type !~ m#text# && $path =~ m/\.(php|pl|txt|info|log|text)$/io)
193 ) {
194 dump_contents($db, "<title>$path</title> $type", $mtime, $path);
195 }
196
197 # skip index files
198 return if (m/index_[a-z]\.html*/i || m/index_symbol\.html*/i);
199
200 open(F,"$path") || die "can't open file: $path";
201 print STDERR "$path ($type)" if ($verbose);
202 while(<F>) {
203 $contents .= "$_";
204 }
205 $contents .= "\n\n";
206
207 #$contents = filter($contents,$collection);
208
209 # add optional components to path
210 $path .= " $path_add" if ($path_add);
211
212 dump_contents($db, $contents, $mtime, $path);
213 }
214
215 print STDERR "\n" if ($verbose);
216 # die "zero size content in '$path'" if (! $contents);
217
218 }
219

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26