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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26