/[swish]/trunk/spider/progspider
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/spider/progspider

Parent Directory Parent Directory | Revision Log Revision Log


Revision 92 - (show annotations)
Mon Nov 22 17:09:23 2004 UTC (19 years, 5 months ago) by dpavlin
File size: 2978 byte(s)
skip symlinks

1 #!/usr/bin/perl -w
2 use strict;
3 use File::Find;
4 use Getopt::Long;
5 use File::Which;
6
7 my $collection; # name which will be inserted
8 my $path_add; # add additional info in path
9 my $verbose;
10
11 #$verbose = 1;
12
13 my $result = GetOptions(
14 "collection=s" => \$collection,
15 "path=s" => \$path_add,
16 "verbose!" => \$verbose,
17 "debug!" => \$verbose,
18 );
19
20 my $dir = shift @ARGV || die "usage: $0 [dir]";
21
22 my $basedir = $0;
23 $basedir =~ s,/[^/]+$,/,;
24 require "$basedir/filter.pm";
25
26 my $pdftotext = which('pdftotext');
27
28 select(STDERR); $|=1;
29 select(STDOUT); $|=1;
30
31 print STDERR "using $pdftotext to convert pdf into html\n" if ($pdftotext && $verbose);
32
33 find({ wanted => \&file,
34 follow => 1,
35 no_chdir => 1
36 }, $dir);
37
38 sub dump_contents($$$) {
39 my ($contents,$mtime,$path) = @_;
40
41 return if (! $contents); # don't die on empty files
42
43 use bytes;
44 my $size = length $contents;
45
46 print STDERR " [$size]" if ($verbose);
47
48 # Output the document (to swish)
49 print <<EOF;
50 Path-Name: $path
51 Content-Length: $size
52 Last-Mtime: $mtime
53 Document-Type: html*
54
55 EOF
56 print $contents;
57
58 }
59
60 sub file {
61
62 my $path = $_;
63 my $contents;
64
65 return if (-l $path);
66
67 if ($pdftotext && -f $path && $path =~ m/\.pdf$/i) {
68
69 print STDERR "$path {converting}" if ($verbose);
70
71 open(F,"$pdftotext -htmlmeta \"$path\" - |") || die "can't open $pdftotext with '$path'";
72 my $html;
73 while(<F>) {
74 # XXX why pdftotext barks if I try to use this is beyond me.
75 #$contents .= $_;
76
77 $html .= $_;
78 }
79 close(F);
80
81 return if (! $html);
82
83 my $file_only = $path;
84 $file_only =~ s/^.*\/([^\/]+)$/$1/g;
85
86 my ($pre_html,$pages,$post_html) = ('<html><head><title>$path :: page ##page_nr##</title></head><body><pre>',$html,'</pre></body></html>');
87
88 ($pre_html,$pages,$post_html) = ($1,$2,$3) if ($html =~ m/^(<html>.+?<pre>)(.+)(<\/pre>.+?)$/si);
89
90 if ($collection) {
91 $pre_html =~ s/<title>(.+?)<\/title>/<title>$collection :: page ##page_nr##<\/title>/si;
92 } else {
93 $pre_html =~ s/<title>(.+?)<\/title>/<title>$1 :: page ##page_nr##<\/title>/si ||
94 $pre_html =~ s/<title><\/title>/<title>$file_only :: page ##page_nr##<\/title>/si;
95 }
96
97 my $page_nr = 1;
98 foreach my $page (split(/\f/s,$pages)) {
99 print STDERR " $page_nr" if ($verbose);
100 my $pre_tmp = $pre_html;
101 $pre_tmp =~ s/##page_nr##/$page_nr<\/title>/s;
102 dump_contents($pre_tmp . $page . $post_html,time(), $path) if ($page !~ m/^\s*$/s);
103 $page_nr++;
104 }
105
106 } else {
107
108 return if (! -f $path || ! m/\.(html*|php|pl|txt|info|log|text)$/i);
109
110 # skip index files
111 return if (m/index_[a-z]\.html*/i || m/index_symbol\.html*/i);
112
113 open(F,"$path") || die "can't open file: $path";
114 print STDERR "$path" if ($verbose);
115 while(<F>) {
116 $contents .= "$_";
117 }
118 $contents .= "\n\n";
119
120 $contents = filter($contents,$collection);
121
122 # add optional components to path
123 $path .= " $path_add" if ($path_add);
124
125 dump_contents($contents,time(), $path);
126 }
127
128 print STDERR "\n" if ($verbose);
129 # die "zero size content in '$path'" if (! $contents);
130
131 }
132

Properties

Name Value
cvs2svn:cvs-rev 1.6
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26