forked from pm5/ebook-downloader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
get.pl
executable file
·277 lines (229 loc) · 5.97 KB
/
get.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
#!/usr/bin/env perl -w
=pod
=head1 NAME
get.pl - Get ebooks
=head1 SYNOPSIS
# Grab the book ID and cookie from `container.xml` request.
$ get.pl <BOOK_ID> <COOKIE_STRING>
=head1 TECHNICAL SPECS
Readmoo's eBook reader roughly works in the following way:
=over
=item 1. Load the reader HTML from L<https://new-read.readmoo.com/mooreader/210001615000101>.
=item 1. Load the reader navigation data from L<https://reader.readmoo.com/api/book/210001615000101/nav>.
=item 1. Load ePub contents using the navigation data.
=back
=cut
use strict;
use warnings;
use v5.10;
use FindBin;
use lib "$FindBin::Bin/local/lib/perl5";
use Mojo::UserAgent;
use JSON qw/from_json/;
use XML::LibXML;
use File::Basename qw/dirname/;
use File::Spec::Functions;
use Data::Dumper;
use Archive::Zip qw/:ERROR_CODES/;
sub config
{
return {
debug => 0,
ua => Mojo::UserAgent->new,
user_agent => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:53.0) Gecko/20100101 Firefox/53.0",
reader_base_url => "https://reader.readmoo.com",
new_read_base_url => "https://new-read.readmoo.com/mooreader",
api_base_url => "https://api.readmoo.com/books",
files => [],
content => {},
};
}
sub request_readmoo
{
my ($moo, $url) = @_;
my $ua = $moo->{ua};
my $res = $ua->get($url => {
"User-Agent" => $moo->{user_agent},
"Cookie" => $moo->{cookie}
})->result;
die "Request to '$url' failed: " . $res->message . "\n"
unless $res->is_success;
$res;
}
sub save_readmoo
{
my ($moo, $url, $save_to) = @_;
my $res = request_readmoo($moo, $url);
$res->save_to($save_to);
}
sub set_book_id
{
my ($moo, $book_id) = @_;
return {
%$moo,
book_id => $book_id
};
}
sub set_cookie
{
my ($moo, $cookie) = @_;
return {
%$moo,
cookie => $cookie
};
}
sub set_build_dir
{
my ($moo, $build_dir) = @_;
return { %$moo, build_dir => $build_dir };
}
sub parse_nav
{
my $moo = shift;
for (shift) {
return {
%$moo,
opf => $_->{opf},
nav_dir => $_->{nav_dir},
message => $_->{message},
base => $_->{base},
};
}
}
sub request_nav
{
my $moo = shift;
$moo->{nav_url} = "https://reader.readmoo.com/api/book/$moo->{book_id}/nav";
my $nav;
if ($moo->{debug}) {
open my $fh, "<:utf8", "nav.json";
$nav = from_json(<$fh>);
close $fh;
} else {
$nav = request_readmoo($moo, $moo->{nav_url})->json;
}
$moo = &parse_nav($moo, $nav);
$moo;
}
sub parse_container
{
my ($moo, $xpc) = @_;
my $opf = $xpc->findnodes("//container:rootfile")->[0]->{"full-path"};
return {
%$moo,
opf => $opf,
};
$moo;
}
sub request_container
{
my $moo = shift;
my $path = "META-INF/container.xml";
$moo = {
%$moo,
container_url => $moo->{reader_base_url} . $moo->{base} . $path
};
if ($moo->{debug}) {
$moo->{content}{$path} = `cat container.xml`
} else {
$moo->{content}{$path} = request_readmoo($moo, $moo->{container_url})->text;
}
my $dom = XML::LibXML->load_xml(string => $moo->{content}{$path});
my $xpc = XML::LibXML::XPathContext->new($dom);
$xpc->registerNs("container", "urn:oasis:names:tc:opendocument:xmlns:container");
$moo = parse_container($moo, $xpc);
}
sub parse_opf
{
my ($moo, $xpc) = @_;
return {
%$moo,
files => [
map { $_->{href} } $xpc->findnodes("//opf:item")
]
};
}
sub request_opf
{
my $moo = shift;
my $path = $moo->{opf};
$moo = {
%$moo,
opf_url => $moo->{reader_base_url} . $moo->{base} . $path
};
if ($moo->{debug}) {
$moo->{content}{$path} = `cat content.opf`;
} else {
$moo->{content}{$path} = request_readmoo($moo, $moo->{opf_url})->text;
}
my $dom = XML::LibXML->load_xml(string => $moo->{content}{$path});
my $xpc = XML::LibXML::XPathContext->new($dom);
$xpc->registerNs("opf", "http://www.idpf.org/2007/opf");
$xpc->registerNs("dc", "http://purl.org/dc/elements/1.1/");
$moo = parse_opf($moo, $xpc);
}
sub write_file
{
my ($moo, $path, $content) = @_;
open my $fh, ">$moo->{build_dir}/$path"
or die "Can't write to $path: $!\n";
print $fh $content;
close $fh;
}
sub save_files
{
my ($moo) = @_;
my $build_dir = $moo->{build_dir};
mkdir for ($build_dir, "$build_dir/META-INF", "$build_dir/OEBPS");
foreach my $path (keys %{$moo->{content}}) {
write_file($moo, $path, $moo->{content}{$path});
}
foreach my $path (@{$moo->{files}}) {
my $url = $moo->{reader_base_url} . $moo->{nav_dir} . $path;
my $save_to = $moo->{build_dir} . "/OEBPS/$path";
say "$url => $save_to";
mkdir dirname($save_to);
unless ($moo->{debug}) {
save_readmoo($moo, $url, $save_to);
sleep 1;
}
}
$moo;
}
sub save_mimetype
{
my $moo = shift;
open my $fh, ">", $moo->{book_id} . "/mimetype"
or die "Can't write to mimetype: $!";
print $fh "application/epub+zip";
close $fh;
$moo;
}
sub save_epub
{
my $moo = shift;
my $zip = Archive::Zip->new;
$zip->addFile(catfile($moo->{book_id}, "mimetype"), "mimetype");
foreach (keys %{$moo->{content}}) {
$zip->addFile(catfile($moo->{book_id}, $_), $_);
}
foreach (@{$moo->{files}}) {
$zip->addFile(catfile($moo->{book_id}, "OEBPS", $_), catfile("OEBPS", $_));
}
unless ($zip->writeToFileNamed("ebook.epub") == AZ_OK) {
die "Cannot write EPUB to ebook.epub: $!";
}
$moo;
}
my $moo = config();
$moo = set_book_id($moo, $ARGV[0]);
$moo = set_cookie($moo, $ARGV[1]);
$moo = set_build_dir($moo, $ARGV[0]);
$moo = request_nav($moo);
$moo = request_container($moo);
$moo = request_opf($moo);
$moo = save_files($moo);
$moo = save_mimetype($moo);
say Dumper($moo) if $moo->{debug};
$moo = save_epub($moo);
0;