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

Contents of /trunk/perl/scripts/est-spider

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5 - (show annotations)
Sat Sep 3 19:16:48 2005 UTC (18 years, 7 months ago) by dpavlin
File size: 4094 byte(s)
example spider for filesystem (like estcmd gather)
1 #!/usr/bin/perl -w
2 use strict;
3 use File::Find;
4 use Getopt::Long;
5 use File::Which;
6 use HyperEstraier;
7 use Text::Iconv;
8
9 my $collection; # name which will be inserted
10 my $path_add; # add additional info in path
11 my $verbose;
12 my $exclude;
13
14 #$verbose = 1;
15
16 my $result = GetOptions(
17 "collection=s" => \$collection,
18 "path=s" => \$path_add,
19 "verbose!" => \$verbose,
20 "debug!" => \$verbose,
21 "exclude=s" => \$exclude,
22 );
23
24 my $dir = shift @ARGV || die "usage: $0 [dir]";
25
26 #my $basedir = $0;
27 #$basedir =~ s,/[^/]+$,/,;
28 #require "$basedir/filter.pm";
29
30 my $pdftotext = which('pdftotext');
31
32 my $iconv = new Text::Iconv('iso-8859-2', 'utf-8');
33
34 select(STDERR); $|=1;
35 select(STDOUT); $|=1;
36
37 print STDERR "using $pdftotext to convert pdf into html\n" if ($pdftotext && $verbose);
38
39
40 # open the database
41 my $db = HyperEstraier::Database->new();
42 $db->open('/tmp/casket', $HyperEstraier::Database::DBWRITER | $HyperEstraier::Database::DBCREAT);
43
44 sub signal {
45 my($sig) = @_;
46 print "\nCaught a SIG$sig--syncing database and shutting down\n";
47 $db->sync();
48 exit(0);
49 }
50
51 $SIG{'INT'} = \&signal;
52 $SIG{'QUIT'} = \&signal;
53
54 find({ wanted => \&file,
55 follow => 1,
56 no_chdir => 1
57 }, $dir);
58
59 print "--- sync\n";
60 $db->sync();
61
62 print "--- optimize...\n";
63 $db->optimize(0);
64 exit;
65
66 sub dump_contents($$$$) {
67 my ($db,$contents,$mtime,$path) = @_;
68
69 return unless ($contents); # don't die on empty files
70
71 if ($exclude && $path =~ m/$exclude/i) {
72 print STDERR "skip: $path\n" if ($verbose);
73 return;
74 }
75
76 use bytes;
77 my $size = length $contents;
78
79 print STDERR " [$size]" if ($verbose);
80
81 # create a document object
82 my $doc = HyperEstraier::Document->new;
83
84 my $title = $1 if ($contents =~ m#<title>(.+)</title>#is);
85
86 # add attributes to the document object
87 $doc->add_attr('@uri', "file:///$path");
88 $doc->add_attr('@title', $title || $path);
89 $doc->add_attr('@size', $size);
90 $doc->add_attr('@mtime', $mtime);
91
92 # html2text
93 $contents =~ s#<[^>]+/*>##gs;
94 $contents =~ s#\s\s+# #gs;
95
96 $doc->add_text($iconv->convert($contents));
97
98 # print $doc->dump_draft if ($verbose);
99
100 # register the document object to the database
101 $db->put_doc($doc, $HyperEstraier::Database::PDCLEAN);
102
103 }
104
105 sub file {
106
107 my $path = $_;
108 my $contents;
109
110 return if (-l $path);
111
112 if ($pdftotext && -f $path && $path =~ m/\.pdf$/i) {
113
114 print STDERR "$path {converting}" if ($verbose);
115
116 open(F,"$pdftotext -htmlmeta \"$path\" - |") || die "can't open $pdftotext with '$path'";
117 my $html;
118 while(<F>) {
119 # XXX why pdftotext barks if I try to use this is beyond me.
120 #$contents .= $_;
121
122 $html .= $_;
123 }
124 close(F);
125
126 return if (! $html);
127
128 my $file_only = $path;
129 $file_only =~ s/^.*\/([^\/]+)$/$1/g;
130
131 my ($pre_html,$pages,$post_html) = ('<html><head><title>$path :: page ##page_nr##</title></head><body><pre>',$html,'</pre></body></html>');
132
133 ($pre_html,$pages,$post_html) = ($1,$2,$3) if ($html =~ m/^(<html>.+?<pre>)(.+)(<\/pre>.+?)$/si);
134
135 if ($collection) {
136 $pre_html =~ s/<title>(.+?)<\/title>/<title>$collection :: page ##page_nr##<\/title>/si;
137 } else {
138 $pre_html =~ s/<title>(.+?)<\/title>/<title>$1 :: page ##page_nr##<\/title>/si ||
139 $pre_html =~ s/<title><\/title>/<title>$file_only :: page ##page_nr##<\/title>/si;
140 }
141
142 my $page_nr = 1;
143 foreach my $page (split(/\f/s,$pages)) {
144 print STDERR " $page_nr" if ($verbose);
145 my $pre_tmp = $pre_html;
146 $pre_tmp =~ s/##page_nr##/$page_nr<\/title>/s;
147 dump_contents($db, $pre_tmp . $page . $post_html,time(), $path) if ($page !~ m/^\s*$/s);
148 $page_nr++;
149 }
150
151 } else {
152
153 return if (! -f $path || ! m/\.(html*|php|pl|txt|info|log|text)$/i);
154
155 # skip index files
156 return if (m/index_[a-z]\.html*/i || m/index_symbol\.html*/i);
157
158 open(F,"$path") || die "can't open file: $path";
159 print STDERR "$path" if ($verbose);
160 while(<F>) {
161 $contents .= "$_";
162 }
163 $contents .= "\n\n";
164
165 #$contents = filter($contents,$collection);
166
167 # add optional components to path
168 $path .= " $path_add" if ($path_add);
169
170 dump_contents($db, $contents,time(), $path);
171 }
172
173 print STDERR "\n" if ($verbose);
174 # die "zero size content in '$path'" if (! $contents);
175
176 }
177

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26