-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbookspider.pl
executable file
·298 lines (216 loc) · 6.68 KB
/
bookspider.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
use Carp;
use Safe;
use Getopt::Long;
use Pod::Usage;
use LWP;
use WWW::Mechanize;
use HTML::TreeBuilder::XPath;
use HTML::FormatText::WithLinks;
use Text::Iconv;
use Encode;
use IO::File;
use lib '/opt/scripts/bookspider';
use BookSpider::Plugin;
=head1 NAME
bookspider.pl - Download an eBook from a website that does not support exporting in eBook format.
=head1 SYNOPSIS
bookspider.pl [-h|--help --man] [-v|--verbose] [-p|--plugin PLUGIN] [-d|--pluginpath PATH] [-f|--output OUTPUT] [-l|--limit NUM] [-i|--incode INCODE] [-o|--outcode OUTCODE] [bookId]
=head1 OPTIONS
=over 8
=item B<--help>
Print a brief help message and exits.
=item B<--man>
Prints the manual page and exits.
=item B<--verbose>
Prints verbose information to STDERR about spidering and processing of book content.
=item B<--plugin PLUGIN>
Use the specified plugin to interact with the site.
=item B<--pluginpath PATH>
Path where plugin files reside.
=item B<--output>
Output book content to a file. If not specified, content will be printed to STDOUT.
=item B<--limit NUM>
Limit chapters to NUM chapters. Useful when developing plugins
=item B<--incode | --outcode>
The input and output encodings to use. By default, incode will plugin-specific and output will be UTF-8.
=back
=head1 DESCRIPTION
B<bookspider.pl> Uses WWW::Mechanize to get all chapters of an eBook from the site by spidering it. Uses plugins to accommodate each site's quirks.
=cut
my $pluginName = "";
my $verbose = 0;
my $pluginPath = "/opt/scripts/bookspider";
my $output="";
my $incode="";
my $outcode="";
my $limit=9999;
my $listplugins=0;
my $opts = Getopt::Long::GetOptions(
"help|h" => sub { pod2usage(7) },
"man|m" => sub {pod2usage(-verbose => 2) },
"plugin|p=s" => \$pluginName,
"verbose|v+" => \$verbose,
"pluginpath|d=s" => \$pluginPath,
"output|f=s" => \$output,
"incode|i=s" => \$incode,
"outcode|o=s" => \$outcode,
"limit|l=i" => \$limit,
"listplugins+" => sub {print join("\n", getPluginList()); exit(); }
) || pod2usage(2);
my $bookId = shift;
pod2usage(-msg => "You must specify a book ID.", -exitval => 1) if (! $bookId);
if (! $pluginName) {
print "You must specify a plugin via --plugin.\n\n";
print "available plugins: ".join(" ", getPluginList())."\n\n";
pod2usage(1);
}
if ($output) {
if (open(OUTPUT, "> $output")) {
print STDERR "Outputting text to $output\n" if ($verbose >= 2);
} else {
die "Warning: Could not open $output";
}
} else {
if (open(OUTPUT, ">&STDOUT")) {
binmode STDOUT, ":utf8";
print STDERR "Outputting text to STDOUT\n" if ($verbose >= 2);
}
}
# Read in the plugin, overriding previously-defined functions.
sub load_plugin {
my $plugin = shift;
my $sub;
my $pluginFile = $pluginPath."/".$plugin.".pm";
open PLUGIN, "<".$pluginFile or die "Could not open $pluginFile: $!";
{
local $/ = undef;
$sub = <PLUGIN>
}
close PLUGIN;
my $eval = (
"\n" .
"package BookSpider::$plugin;" .
'use vars qw(@ISA);' .
'use strict;' .
'@ISA = qw(BookSpider::Plugin);' .
$sub .
"# End of plugin.\n"
);
eval $eval;
die "ERROR: $plugin - eval $@" if $@;
my $pclass = "BookSpider::$plugin";
my $plug_obj = $pclass->new($bookId, $incode, $outcode, $verbose);
#$plug_obj->{PLUGIN_NAME} = $plugin;
return ( $plug_obj );
}
#my $plugincode;
#while (<PLUGIN>) {
# $plugincode = $plugincode . $_;
#}
#eval <PLUGIN>;
my $plugin = load_plugin($pluginName);
#push @INC, $pluginPath;
#use autouse $plugin => qw( isMatchingUrl isValidBookId getName parseIndex parseDocument convert_encoding print_chapter getIndexUrl );
#use vars qw($Plugin);
#my $plug = new Safe 'Plugin';
#
#$plug->share(qw( init isMatchingUrl isValidBookId getName parseIndex parseDocument convert_encoding print_chapter getIndexUrl ));
#
#my $result = $plug->reval(<PLUGIN>);
#
#print Dumper($plugin);
$plugin->init($bookId, $incode, $outcode);
# FUNCTIONS GO HERE--------------------------------------------------------------
sub spider {
my $indexUrl = shift;
my %history = ();
my %queue = ();
my $pageCount=0;
my $mech = WWW::Mechanize->new();
$queue{$indexUrl} = 1;
while ((keys %queue > 0) && ($pageCount <= $limit)) {
my @queueItems = keys %queue;
my $url = $queueItems[0];
if ($history{$url}) {
print STDERR "Skipping already-seen url: $url\n" if ($verbose >= 2);
delete $queue{$url};
next;
}
if ((! $plugin->isMatchingUrl($url)) && ($url ne $indexUrl)) {
print STDERR "Discarding non-matching url: $url\n" if ($verbose >= 2);
delete $queue{$url};
$history{$url} = 1;
next;
}
print STDERR "Retrieving $url\n" if $verbose;
$mech->get( $url );
if (! $mech->success()) {
print "Error retrieving url: $url, status ".$mech->status()."\n";
delete $queue{$url};
}
my @links = $mech->links();
foreach my $link (@links) {
my $linkUrl = $link->url_abs();
if ((! $plugin->isMatchingUrl($linkUrl)) && ($linkUrl ne $indexUrl)) {
next;
}
if (! $plugin->{BOOK_DATA}{$linkUrl}{'linkText'}) {
$plugin->{BOOK_DATA}{$linkUrl}{'linktext'} = $link->text();
}
$queue{$linkUrl} = 1;
}
#TODO We shouldn't do this here. Leave it up to the plugin.
if ($url eq $indexUrl) {
$plugin->parseIndex($mech, $url);
} else {
$plugin->parseDocument($mech, $url);
}
#Remember this URL for next time, so we don't re-process it.
$history{$url} = 1;
$pageCount++;
}
if ($pageCount >= $limit) {
print STDERR "Reached page limit ($limit). Stopping spider process.\n";
}
}
sub getPluginList {
opendir (DIR, $pluginPath) or die "Could not open $pluginPath: $!";
my @files = grep(/\.pm$/, readdir(DIR));
closedir(DIR);
my @plugins;
foreach my $file (@files) {
$file =~ s/\.p[lm]//g;
push(@plugins, $file);
}
return @plugins;
}
# PROGRAM STARTS HERE------------------------------------------------------------
#my $indexUrl = $rxIndexUrl;
#$indexUrl =~ s/%s/$bookId/g;
# Do error checking
if (!$plugin->hasValidBookId()) { die "That bookId is not valid with this plugin" }
spider($plugin->getIndexUrl());
my %sorthash = ();
my $bookData = $plugin->{BOOK_DATA};
foreach my $key (keys %$bookData) {
if ($bookData->{$key}{'number'}) {
my $tmpnum = $bookData->{$key}{'number'};
$sorthash{$tmpnum} = $key;
} else {
#TODO: These are getting put into bookdata. Why? Does plugin do it, or bookspider?
print STDERR "Ignoring content from $key (not a chapter.)\n";
}
}
my $buffer;
foreach my $key (sort myplugin::plugin_sort (keys(%sorthash))) { # {$a <=> $b} is "numerically ascending".
#TODO: Does this work with chinese numbers? (一二三四)?
my $url = $sorthash{$key};
$buffer = $buffer . $plugin->print_chapter($bookData->{$url});
}
#print Dumper($bookData);
print OUTPUT $buffer;
close(OUTPUT);