Gutenberg/Catalog.pm

filter: Statementstmt 92.3% 24 / 26 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 94.2% 33 / 35 CC 1 SCAR 0.3
CC
1
Cov
92%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Gutenberg/Home.pm ↑ Summary Gutenberg.pm »
1 package EBook::Gutenberg::Catalog;
2 3 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 our $VERSION = '1.00';
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use File::Fetch;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use File::Temp qw(tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use List::Util qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 0 ▶use Text::CSV_XS qw(csv);
Subroutine BEGIN: called CC=1 SCAR=2.6
14  
15 my $CATALOG_URI = "https://www.gutenberg.org/cache/epub/feeds/pg_catalog.csv";
16 my $CATALOG_HEAD = 'Text#,Type,Issued,Title,Language,Authors,Subjects,LoCC,Bookshelves';
17  
18 sub _catalog_ok {
19  
20 my $file = shift;
21  
22 open my $fh, '<', $file
23 or die "Failed to open $file for reading: $!\n";
24 my $head = readline $fh;
25 close $fh;
26  
27 return $head =~ /^\Q$CATALOG_HEAD\E/;
28  
29 }
30  
31 sub _bookify {
32  
33 my $book = shift;
34  
35 my @b = map { s/\s+/ /gr } @$book;
36  
37 return {
38 'Text#' => $b[0],
39 'Type' => $b[1],
40 'Issued' => $b[2],
41 'Title' => $b[3],
42 'Language' => $b[4],
43 'Authors' => $b[5],
44 'Subjects' => $b[6],
45 'LoCC' => $b[7],
46 'Bookshelves' => $b[8],
47 };
48  
49 }
50  
51 sub new {
52  
53 my $class = shift;
54 my $file = shift;
55  
56 my $self;
57  
58 $$self = File::Spec->rel2abs($file);
59  
60 bless $self, $class;
61  
62 if (-d $$self) {
63 die "$$self is a directory\n";
64 }
65  
66 if (-f $$self and !_catalog_ok($$self)) {
67 die "$$self is not a valid Project Gutenberg catalog file\n";
68 }
69  
70 return $self;
71  
72 }
73  
74 sub path {
75  
76 my $self = shift;
77  
78 return $$self;
79  
80 }
81  
82 sub set_path {
83  
84 my $self = shift;
85 my $file = shift;
86  
87 $$self = File::Spec->rel2abs($file);
88  
89 if (-d $$self) {
90 die "$$self is a directory\n";
91 }
92  
93 if (-f $$self and !_catalog_ok($$self)) {
94 die "$$self is not a valid Project Gutenberg catalog file\n";
95 }
96  
97 return $$self;
98  
99 }
100  
101 sub fetch {
102  
103 my $self = shift;
104  
105 my $tmp = tempdir(CLEANUP => 1);
106  
107 my $ff = File::Fetch->new(uri => $CATALOG_URI);
108  
109 my $fetch = $ff->fetch(to => $tmp)
110 or die $ff->error;
111  
112 unless (_catalog_ok($fetch)) {
113 die "Downloaded catalog was not a valid Project Gutenberg catalog file\n";
114 }
115  
116 move($fetch, $$self)
117 or die "Failed to move $fetch to $$self: $!\n";
118  
119 rmdir $tmp;
120  
121 return $$self;
122  
123 }
124  
125 sub book {
126  
127 my $self = shift;
128 my $id = shift;
129  
130 unless (-f $$self) {
131 die "$$self is not a regular file\n";
132 }
133  
134 my $csv = Text::CSV_XS->new({ binary => 1, auto_diag => 1 });
135  
136 open my $fh, '<', $$self
137 or die "Failed to open $$self for reading: $!\n";
138  
139 my $book;
140  
141 while (my $row = $csv->getline($fh)) {
142 if ($row->[0] eq $id) {
143 $book = $row;
144 last;
145 }
146 }
147  
148 close $fh;
149  
150 return defined $book ? _bookify($book) : undef;
151  
152 }
153  
154 sub books {
155  
156 my $self = shift;
157 my $param = shift;
158  
159 unless (-f $$self) {
160 die "$$self is not a regular file\n";
161 }
162  
163 my $filter = {};
164  
165 for my $p (split /,/, $CATALOG_HEAD) {
166 next unless ref $param->{ $p } eq 'CODE';
167 $filter->{ $p } = $param->{ $p };
168 }
169  
170 my $books = csv(
171 in => $$self,
172 filter => $filter,
173 # Convert all whitespace to single space
174 after_parse => sub {
175 for my $k (keys %{ $_[1] }) {
176 $_[1]->{ $k } =~ s/\s+/ /g;
177 }
178 }
179 );
180  
181 return $books;
182  
183 }
184  
185 1;
186  
187 =head1 NAME
188  
189 EBook::Gutenberg::Catalog - Project Gutenberg catalog interface
190  
191 =head1 SYNOPSIS
192  
193 use EBook::Gutenberg::Catalog;
194  
195 my $catalog = EBook::Gutenberg::Catalog->new($path);
196  
197 =head1 DESCRIPTION
198  
199 B<EBook::Gutenberg::Catalog> is a module that provides an interface for reading
200 Project Gutenberg CSV catalog files. This is developer documentation, for
201 L<gutenberg> user documentation you should consult its manual.
202  
203 =head1 METHODS
204  
205 =over 4
206  
207 =item $cat = EBook::Gutenberg::Catalog->new($path)
208  
209 Returns a blessed B<EBook::Gutenberg::Catalog> object representing a Project
210 Gutenberg catalog file stored in C<$path>. C<$path> doesn't actually have to
211 exist, it can be fetched later via the C<fetch()> method.
212  
213 =item $path = $cat->path()
214  
215 Returns path to C<$cat>'s catalog file.
216  
217 =item $path = $cat->set_path($new)
218  
219 Set C<$cat>'s catalog file to C<$new>. Returns newly set path.
220  
221 =item $fetch = $cat->fetch()
222  
223 Fetches Project Gutenberg catalog file and writes it to the path specified in
224 C<new()>. Returns the path to the fetched file.
225  
226 =item $book = $cat->book($id)
227  
228 Get hash ref representing the book with an ID C<$id> from the catalog file. The
229 hash ref has the following format:
230  
231 {
232 'Text#' => '...',
233 'Type' => '...',
234 'Issued' => '...',
235 'Title' => '...',
236 'Language' => '...',
237 'Authors' => '...',
238 'Subjects' => '...',
239 'LoCC' => '...',
240 'Bookshelves' => '...',
241 }
242  
243 =item $books = $cat->books([\%params])
244  
245 Returns array ref of hash refs representing books from catalog that conform to
246 the parameters supplied by C<\%params>. The hash refs follow the same format
247 used by the ones returned by C<book()>.
248  
249 C<\%params> is a hash ref of ebook fields and subroutine references that are
250 used to C<grep> for specific ebooks. The subroutine will have C<$_> set to the
251 value of the field. If the subroutine does not return true when given a value,
252 the ebook will be filtered out. The following are valid fields to use for
253 C<\%params>.
254  
255 =over 4
256  
257 =item Text#
258  
259 =item Type
260  
261 =item Issued
262  
263 =item Title
264  
265 =item Language
266  
267 =item Authors
268  
269 =item Subjects
270  
271 =item LoCC
272  
273 =item Bookshelves
274  
275 =back
276  
277 =back
278  
279 =head1 AUTHOR
280  
281 Written by Samuel Young, E<lt>samyoung12788@gmail.comE<gt>.
282  
283 This project's source can be found on its
284 L<Codeberg page|https://codeberg.org/1-1sam/gutenberg>. Comments and pull
285 requests are welcome!
286  
287 =head1 COPYRIGHT
288  
289 Copyright (C) 2025 Samuel Young
290  
291 This program is free software: you can redistribute it and/or modify
292 it under the terms of the GNU General Public License as published by
293 the Free Software Foundation, either version 3 of the License, or
294 (at your option) any later version.
295  
296 =head1 SEE ALSO
297  
298 L<gutenberg>
299  
300 =cut
301  
302 # vim: expandtab shiftwidth=4
« Gutenberg/Home.pm ↑ Summary Gutenberg.pm »