Gutenberg/Get.pm

filter: Statementstmt 70.3% 38 / 54 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 94.4% 17 / 18 Podpod 100% 2 / 2 totaltotal 64.7% 57 / 88 CC 7 SCAR 28.4
CC
7
Cov
58%
gutenberg_get
56.0
BEGIN
1.0
BEGIN
1.0
CRAP 17.0
↑ Summary Gutenberg/Dialog.pm »
1 package EBook::Gutenberg::Get;
2 1 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 our $VERSION = '1.00';
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 our @EXPORT = qw(gutenberg_link gutenberg_get);
9  
10 1 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Fetch;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use File::Temp qw(tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 our %FORMATS = (
16 'html' => {
17 1 ▶ link => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
18 1 "https://www.gutenberg.org/ebooks/$_[0].html.images"
19 },
20 suffix => 'html',
21 },
22 'epub3' => {
23 1 ▶ link => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
24 1 "https://www.gutenberg.org/ebooks/$_[0].epub3.images"
25 },
26 suffix => 'epub',
27 },
28 'epub' => {
29 1 ▶ link => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
30 1 "https://www.gutenberg.org/ebooks/$_[0].epub.images"
31 },
32 suffix => 'epub',
33 },
34 'epub-noimages' => {
35 1 ▶ link => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
36 1 "https://www.gutenberg.org/ebooks/$_[0].epub.noimages"
37 },
38 suffix => 'epub',
39 },
40 'kindle' => {
41 1 ▶ link => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
42 1 "https://www.gutenberg.org/ebooks/$_[0].kf8.images"
43 },
44 suffix => 'azw3',
45 },
46 'mobi' => {
47 1 ▶ link => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
48 1 "https://www.gutenberg.org/ebooks/$_[0].kindle.images"
49 },
50 suffix => 'mobi',
51 },
52 'text' => {
53 1 ▶ link => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
54 1 "https://www.gutenberg.org/ebooks/$_[0].txt.utf-8"
55 },
56 suffix => 'txt',
57 },
58 'zip' => {
59 1 ▶ link => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
60 1 "https://www.gutenberg.org/cache/epub/$_[0]/pg$_[0]-h.zip"
61 },
62 1 suffix => 'zip',
63 },
64 );
65  
66 8 ▶sub gutenberg_link {
Subroutine gutenberg_link: called CC=1 SCAR=0.0
Pod documented
67  
68 8 my $id = shift;
69 8 my $fmt = shift;
70  
71 8 return $FORMATS{ $fmt }->{ link }($id);
72  
73 }
74  
75 0 ▶sub gutenberg_get {
Subroutine gutenberg_get: not called CC=7 SCAR=40.3
Pod documented
76  
77 0 my $id = shift;
78 0 my $param = shift;
79  
80 0 ▶ unless ($id =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id =~ /^\d+$/u) 0 0 0
81 0 die "id must be an integar\n";
82 }
83  
84 0 ▶ my $fmt = $param->{ fmt } // 'epub3';
Condition0%0/2
$param->{"fmt"} // "epub3"
l!l
00
MC/DC0%0/1
$param->{"fmt"} // "epub3"
$param->{"fmt"}
Truth table0%0/2
A
A= $param->{"fmt"}
Aresult
11
00
85 0 ▶ my $to = $param->{ to } // "$id.*";
Condition0%0/2
$param->{"to"} // "$id.*"
l!l
00
MC/DC0%0/1
$param->{"to"} // "$id.*"
$param->{"to"}
Truth table0%0/2
A
A= $param->{"to"}
Aresult
11
00
86  
87 0 $to =~ s/\.\*$/\.$FORMATS{ $fmt }->{ suffix }/;
88  
89 0 ▶ if (-d $to) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $to) 0 0 0
90 0 die "Cannot download file to $to: is a directory\n";
91 }
92  
93 0 my $link = gutenberg_link($id, $fmt);
94  
95 0 my $tmp = tempdir(CLEANUP => 1);
96  
97 0 my $ff = File::Fetch->new(uri => $link);
98  
99 0 ▶ my $fetch = $ff->fetch(to => $tmp)
Branch: 1 branch
BranchTrueFalseTotal
my $fetch = $ff->fetch("to", $tmp) or die $ff->error 0 0 0
100 or die $ff->error;
101  
102 0 ▶ move($fetch, $to)
Branch: 1 branch
BranchTrueFalseTotal
move($fetch, $to) or die "Failed to move $fetch to ${to}: $!\n" 0 0 0
103 or die "Failed to move $fetch to $to: $!\n";
104  
105 0 rmdir $tmp;
106  
107 0 return $to;
108  
109 }
110  
111 1 1;
112  
113 =head1 NAME
114  
115 EBook::Gutenberg::Get - Fetch ebooks from Project Gutenberg
116  
117 =head1 SYNOPSIS
118  
119 use EBook::Gutenberg::Get;
120  
121 my $ebook = gutenberg_get($id);
122  
123 =head1 DESCRIPTION
124  
125 B<EBook::Gutenberg::Get> is a module that provides some subroutines related to
126 downloading ebooks from Project Gutenberg. This is developer documentation,
127 for L<gutenberg> user documentation you should consult its manual.
128  
129 Note that this module is not designed to perform bulk downloading/scraping.
130 Attempting to use this module to do so may result in Project Gutenberg banning
131 you from their site. You have been warned.
132  
133 =head1 SUBROUTINES
134  
135 The following subroutines are exported by C<EBook::Gutenberg::Get>
136 automatically.
137  
138 =over 4
139  
140 =item $get = gutenberg_get($id, [\%param])
141  
142 Fetches an ebook from Project Gutenberg. C<$id> is the ID of the ebook to
143 fetch. C<\%params> is a hash ref of extra parameters to configure the download
144 process.
145  
146 The following are valid parameters:
147  
148 =over 4
149  
150 =item to
151  
152 Path to write downloaded file to. Defaults to C<'$id.*'>. If the provided path
153 has a C<'.*'> suffix, the C<'.*'> will be substituted by the ebook format's
154 file suffix.
155  
156 =item fmt
157  
158 Format of ebook to download. The following are valid options:
159  
160 =over 4
161  
162 =item html
163  
164 =item epub3
165  
166 =item epub
167  
168 =item epub-noimages
169  
170 =item kindle
171  
172 =item mobi
173  
174 =item text
175  
176 =item zip
177  
178 =back
179  
180 Defaults to C<'epub3'>.
181  
182 =back
183  
184 =item $link = gutenberg_link($id, $fmt)
185  
186 Returns the download link of an ebook. C<$id> is the ID of the ebook. C<$fmt>
187 is the ebook format.
188  
189 =back
190  
191 =head1 GLOBAL VARIABLES
192  
193 =over 4
194  
195 =item %EBook::Gutenberg::FORMATS
196  
197 Hash of valid ebook formats and a hash ref of some format data. See the
198 documentation for C<fmt> in C<gutenberg_get> for a list of ebook formats.
199  
200 The format hash ref has this format:
201  
202 =over 4
203  
204 =item link
205  
206 Subroutine reference that, when given an ebook ID, returns a download link to
207 that ebook.
208  
209 =item suffix
210  
211 The default file suffix to use with the format.
212  
213 =back
214  
215 =back
216  
217 =head1 AUTHOR
218  
219 Written by Samuel Young, E<lt>samyoung12788@gmail.comE<gt>.
220  
221 This project's source can be found on its
222 L<Codeberg page|https://codeberg.org/1-1sam/gutenberg>. Comments and pull
223 requests are welcome!
224  
225 =head1 COPYRIGHT
226  
227 Copyright (C) 2025 Samuel Young
228  
229 This program is free software: you can redistribute it and/or modify
230 it under the terms of the GNU General Public License as published by
231 the Free Software Foundation, either version 3 of the License, or
232 (at your option) any later version.
233  
234 =head1 SEE ALSO
235  
236 L<gutenberg>, L<File::Fetch>
237  
238 =cut
239  
240 # vim: expandtab shiftwidth=4
↑ Summary Gutenberg/Dialog.pm »