blib/lib/Comics.pm

filter: Statementstmt 25.7% 80 / 311 Branchbran 1.7% 2 / 116 Conditioncond 0.0% 0 / 41 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 46.5% 20 / 43 Podpod n/a totaltotal 19.1% 102 / 533 CC 83 SCAR 48.5
CC
83
Cov
18%
load_plugins
506.0
build
110.0
app_options
110.0
CRAP 128.0
« lib/Comics/Fetcher/Cascade.pm ↑ Summary lib/Comics/Plugin/Base.pm »
1 #! perl
2  
3 # Author : Johan Vromans
4 # Created On : Fri Oct 21 09:18:23 2016
5 # Last Modified By:
6 # Last Modified On: Mon Feb 26 09:40:52 2024
7 # Update Count : 393
8 # Status : Unknown, Use with caution!
9  
10 1 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 package Comics;
17  
18 1 ▶use Comics::Version;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 our $VERSION = $Comics::Version::VERSION;
21  
22 package main;
23  
24 ################ Common stuff ################
25  
26 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use File::Path qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.2
33 # Add private library if it exists.
34 1 ▶ if ( -d "$FindBin::Bin/../lib" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d "$FindBin::Bin/../lib") 1 0 1
35 1 unshift( @INC, "$FindBin::Bin/../lib" );
36 }
37 }
38  
39 # Package name.
40 1 my $my_package = 'Sciurix';
41 # Program name.
42 1 my $my_name = "comics";
43  
44 ################ Command line parameters ################
45  
46 1 ▶use Getopt::Long 2.13;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 # Command line options.
49 1 my $spooldir = File::Spec->catdir( File::Spec->tmpdir, "Comics" );
50 1 my $statefile;
51 my $refresh;
52 1 my $activate = 0; # enable/disable
53 1 my $force; # process disabled modules as well
54 my $rebuild; # rebuild index, no fetching
55 1 my $list; # produce listing
56 1 my $verbose = 1; # verbose processing
57 1 my $reuse = 0; # reuse existing fetch results
58  
59 # Development options (not shown with -help).
60 1 my $debug = 0; # debugging
61 1 my $trace = 0; # trace (show process)
62 1 my $test = 0; # test mode.
63  
64 # Extra command line arguments are taken to be plugin names.
65 # If specified, only named plugins are included.
66 1 my $pluginfilter;
67  
68 ################ Presets ################
69  
70 ################ The Process ################
71  
72 1 ▶use File::LoadLines;
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74 # Statistics.
75 1 our $stats;
76  
77 0 ▶sub init {
Subroutine init: not called CC=5 SCAR=34.0
78 0 $stats =
79 { tally => 0,
80 fail => [],
81 loaded => 0,
82 uptodate => 0,
83 excluded => 0,
84 disabled => 0,
85 };
86  
87 # Process command line options.
88 0 app_options();
89  
90 # Post-processing.
91 0 ▶ $trace |= ($debug || $test);
Condition0%0/3
$debug || $test
l!l&&r!l&&!r
000
MC/DC0%0/2
$debug || $test
$debug $test
Truth table0%0/3
A || B
A= $debugB= $test
ABresult
1X1
011
000
92 0 ▶ $verbose = 255 if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
93 0 $spooldir .= "/";
94 0 $spooldir =~ s;/+$;/;;
95  
96 0 ▶ File::Path::make_path( $spooldir, { verbose => 1 } )
Branch: 1 branch
BranchTrueFalseTotal
unless -d $spooldir 0 0 0
97 unless -d $spooldir;
98  
99 0 $statefile = spoolfile(".state.json");
100  
101 0 $pluginfilter = ".";
102 0 ▶ if ( @ARGV ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV) 0 0 0
103 0 $pluginfilter = "^(?:" . join("|", @ARGV) . ")\\.pm\$";
104 }
105 0 $pluginfilter = qr($pluginfilter)i;
106  
107 }
108  
109 0 ▶sub main {
Subroutine main: not called CC=5 SCAR=34.0
110  
111 # Initialize.
112 0 init();
113  
114 # Restore state of previous run.
115 0 get_state();
116  
117 # Load the plugins.
118 0 load_plugins();
119  
120 # Non-aggregating command: list.
121 0 ▶ if ( $list ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list) 0 0 0
122 0 list_plugins();
123 0 return;
124 }
125  
126 # Non-aggregating command: enable/disable.
127 0 ▶ if ( $activate ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($activate) 0 0 0
128 0 save_state();
129 0 ▶ return unless $rebuild;
Branch: 1 branch
BranchTrueFalseTotal
unless $rebuild 0 0 0
130 }
131  
132 0 ▶ unless ( $rebuild ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rebuild) 0 0 0
133 # Run the plugins to fetch new images.
134 0 run_plugins();
135  
136 # Save the state.
137 0 save_state();
138 }
139  
140 # Gather the HTML fragments into a single index.html.
141 0 build();
142  
143 # Show processing statistics.
144 0 statistics();
145 }
146  
147 ################ State subroutines ################
148  
149 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
150  
151 1 my $state;
152  
153 0 ▶sub get_state {
Subroutine get_state: not called CC=4 SCAR=30.0
154 0 my $opts = { split => 0, fail => "soft" };
155 0 my $data = loadlines( $statefile, $opts );
156 0 ▶ if ( $opts->{error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'error'}) { } 0 0 0
157 0 $state = { comics => { } };
158 }
159 else {
160 0 $state = JSON->new->decode($data);
161 0 ▶ if ( $refresh ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($refresh) 0 0 0
162 delete( $_->{md5} )
163 0 foreach values( %{ $state->{comics} } );
164 }
165 }
166 }
167  
168 0 ▶sub save_state {
Subroutine save_state: not called CC=1 SCAR=6.9
169 0 unlink($statefile."~");
170 0 rename( $statefile, $statefile."~" );
171 0 open( my $fd, '>:utf8', $statefile );
172 0 print $fd JSON->new->canonical->pretty(1)->encode($state);
173 0 close($fd);
174 }
175  
176 ################ Plugin subroutines ################
177  
178 my @plugins;
179  
180 0 ▶sub load_plugins {
Subroutine load_plugins: not called CC=22 SCAR=62.3
181  
182 0 ▶ opendir( my $dh, $INC[0] . "/Comics/Plugin" )
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $INC[0] . '/Comics/Plugin' or die $INC[0] . "/Comics/Plugin: $!\n" 0 0 0
183 or die( $INC[0] . "/Comics/Plugin: $!\n");
184  
185 0 ▶ while ( my $m = readdir($dh) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $m = readdir $dh)) 0 0 0
186 0 ▶ next unless $m =~ /^[0-9A-Z].*\.pm$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $m =~ /^[0-9A-Z].*\.pm$/u 0 0 0
187 0 ▶ next if $m eq 'Base.pm';
Branch: 1 branch
BranchTrueFalseTotal
if $m eq "Base.pm" 0 0 0
188 0 $stats->{loaded}++;
189 0 ▶ $stats->{excluded}++, next unless $m =~ $pluginfilter;
Branch: 1 branch
BranchTrueFalseTotal
unless $m =~ /$pluginfilter/u 0 0 0
190  
191 0 debug("Loading $m...");
192 0 $m =~ s/\.pm$//;
193 # If the module is already loaded, remove it first.
194 # Otherwise the require won't produce the __PACKAGE__ result.
195 0 delete $INC{"Comics/Plugin/$m.pm"};
196 0 my $pkg = eval { require "Comics/Plugin/$m.pm" };
197 0 ▶ die("Comics/Plugin/$m.pm: $@\n") unless $pkg;
Branch: 1 branch
BranchTrueFalseTotal
unless $pkg 0 0 0
198 0 ▶ unless ( $pkg eq "Comics::Plugin::$m" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pkg eq "Comics::Plugin::$m") 0 0 0
199 0 warn("Skipped $m.pm (defines $pkg, should be Comics::Plugin::$m)\n");
200 0 next;
201 }
202 0 my $comic = $pkg->register;
203 0 ▶ next unless $comic;
Branch: 1 branch
BranchTrueFalseTotal
unless $comic 0 0 0
204  
205 0 push( @plugins, $comic );
206 0 my $tag = $comic->{tag};
207  
208 # 'disabled' means that this plugin is permanently disabled.
209 0 ▶ my $activate = $comic->{disabled} ? -1 : $activate;
Branch: 1 branch
BranchTrueFalseTotal
$comic->{'disabled'} ? : 0 0 0
210  
211 # 'ondemand' means that this plugin is initially disabled, but
212 # can be enabled if desired.
213 0 ▶ if ( !$activate && $comic->{ondemand}
Branch: 1 branch
BranchTrueFalseTotal
if (not $activate and $comic->{"ondemand"} and not exists $state->{"comics"}{$tag}) 0 0 0
Condition0%0/6
not $activate and $comic->{"ondemand"} and not exists $state->{"comics"}{$tag}
!ll&&!rl&&r
000
not $activate and $comic->{"ondemand"}
!ll&&!rl&&r
000
MC/DC0%0/3
not $activate and $comic->{"ondemand"} and not exists $state->{"comics"}{$tag}
not $activate $comic->{"ondemand"} not exists $state->{"comics"}{$tag}
Truth table0%0/4
not(A) and B and not(C)
A= not $activateB= $comic->{"ondemand"}C= not exists $state->{"comics"}{$tag}
ABCresult
0XX0
10X0
1100
1111
214 && !exists( $state->{comics}->{$tag} ) ) {
215 0 $activate = -1;
216 }
217  
218 0 ▶ if ( $activate > 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($activate > 0) { } 0 0 0
elsif ($activate < 0) { } 0 0 0
219 delete( $state->{comics}->{$tag}->{disabled} )
220 0 }
221 elsif ( $activate < 0 ) {
222 0 $state->{comics}->{$tag}->{disabled} = 1;
223 0 delete( $state->{comics}->{$tag}->{md5} );
224 0 for ( qw( html jpg png gif ) ) {
225 0 ▶ next unless unlink( spoolfile( $tag . "." . $_ ) );
Branch: 1 branch
BranchTrueFalseTotal
unless unlink spoolfile($tag . '.' . $_) 0 0 0
226 0 debug( "Removed: ", spoolfile( $tag . "." . $_ ) );
227 0 $rebuild++;
228 }
229 0 for ( $state->{comics}->{$tag}->{c_img} ) {
230 0 ▶ next unless defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 0 0
231 0 ▶ next unless unlink( spoolfile($_) );
Branch: 1 branch
BranchTrueFalseTotal
unless unlink spoolfile($_) 0 0 0
232 0 debug( "Removed: ", spoolfile($_) );
233 0 $rebuild++;
234 }
235 }
236  
237 0 ▶ if ( $state->{comics}->{$tag}->{disabled} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"comics"}{$tag}{"disabled"}) 0 0 0
238 0 $stats->{disabled}++;
239 0 debug("Comics::Plugin::$m: Disabled");
240 }
241  
242 }
243  
244 0 ▶ if ( $stats->{loaded} == $stats->{excluded} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::stats->{"loaded"} == $main::stats->{"excluded"}) 0 0 0
245 0 warn( "No matching plugins found\n" );
246 }
247 }
248  
249 0 ▶sub list_plugins {
Subroutine list_plugins: not called CC=11 SCAR=46.8
250  
251 0 my $lpl = length("Comics::Plugin::");
252 0 my $lft = length("Comics::Fetcher::");
253 0 my ( $l_name, $l_plugin, $l_fetcher ) = ( 0, 0, $lft+8 );
254  
255 0 my @tm;
256 @plugins =
257 sort { ($state->{comics}->{$a->{tag}}->{disabled} // 0) <=>
258 ($state->{comics}->{$b->{tag}}->{disabled} // 0) ||
259 $b->{update} <=> $a->{update} ||
260 $a->{name} cmp $b->{name}
261 0 ▶ }
Condition0%0/10
($state->{'comics'}{$main::a->{'tag'}}{'disabled'} // 0) <=> ($state->{'comics'}{$main::b->{'tag'}}{'disabled'} // 0) || $main::b->{'update'} <=> $main::a->{'update'} || $main::a->{'name'} cmp $main::b->{'name'}
l!l&&r!l&&!r
000
($state->{"comics"}{$main::a->{"tag"}}{"disabled"} // 0) <=> ($state->{"comics"}{$main::b->{"tag"}}{"disabled"} // 0) or $main::b->{"update"} <=> $main::a->{"update"}
l!l&&r!l&&!r
000
$state->{"comics"}{$main::a->{"tag"}}{"disabled"} // 0
l!l
00
$state->{"comics"}{$main::b->{"tag"}}{"disabled"} // 0
l!l
00
MC/DC0%0/3
($state->{'comics'}{$main::a->{'tag'}}{'disabled'} // 0) <=> ($state->{'comics'}{$main::b->{'tag'}}{'disabled'} // 0) || $main::b->{'update'} <=> $main::a->{'update'} || $main::a->{'name'} cmp $main::b->{'name'}
$state->{"comics"}{$main::a->{"tag"}}{"disabled"} $main::b->{"update"} <=> $main::a->{"update"} $main::a->{'name'} cmp $main::b->{'name'}
MC/DC0%0/1
$state->{"comics"}{$main::b->{"tag"}}{"disabled"} // 0
$state->{"comics"}{$main::b->{"tag"}}{"disabled"}
Truth table0%0/4
A or B || C
A= $state->{"comics"}{$main::a->{"tag"}}{"disabled"}B= $main::b->{"update"} <=> $main::a->{"update"}C= $main::a->{'name'} cmp $main::b->{'name'}
ABCresult
1XX1
01X1
0011
0000
Truth table0%0/2
A
A= $state->{"comics"}{$main::b->{"tag"}}{"disabled"}
Aresult
11
00
262 map {
263 0 ▶ $_->{update} = $state->{comics}->{ $_->{tag} }->{update} ||= 0;
Condition0%0/2
$state->{"comics"}{$_->{"tag"}}{"update"} ||= 0
l!l
00
MC/DC0%0/1
$state->{"comics"}{$_->{"tag"}}{"update"} ||= 0
$state->{"comics"}{$_->{"tag"}}{"update"}
Truth table0%0/2
A
A= $state->{"comics"}{$_->{"tag"}}{"update"}
Aresult
11
00
264 0 @tm = localtime($_->{update});
265 0 $_->{updated} = sprintf( "%04d-%02d-%02d %02d:%02d:%02d",
266 1900+$tm[5], 1+$tm[4], @tm[3,2,1,0] );
267 0 ▶ $l_name = length($_->{name}) if $l_name < length($_->{name});
Branch: 1 branch
BranchTrueFalseTotal
if $l_name < length $_->{"name"} 0 0 0
268 0 ▶ $l_plugin = length(ref($_)) if $l_plugin < length(ref($_));
Branch: 1 branch
BranchTrueFalseTotal
if $l_plugin < length ref $_ 0 0 0
269 0 $_;
270 } @plugins;
271  
272 0 $l_plugin -= $lpl;
273 0 $l_fetcher -= $lft;
274 0 my $fmt = "%-${l_name}s %-${l_plugin}s %-${l_fetcher}s %-8s %s\n";
275 0 foreach my $comic ( @plugins ) {
276  
277 0 my $st = $state->{comics}->{ $comic->{tag} };
278 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
279 printf( $fmt,
280 $comic->{name},
281 substr( ref($comic), $lpl ),
282 0 ▶ substr( ${ref($comic)."::"}{ISA}[0], $lft ),
Branch: 2 branches
BranchTrueFalseTotal
$st->{'disabled'} ? : 0 0 0
$comic->{'update'} ? : 0 0 0
283 $st->{disabled} ? "disabled" : "enabled",
284 0 $comic->{update} ? $comic->{updated} : "",
285 );
286 }
287  
288 }
289  
290 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
291  
292 1 our $ua;
293 1 our $uuid;
294  
295 0 ▶sub run_plugins {
Subroutine run_plugins: not called CC=8 SCAR=42.8
296  
297 0 ▶ unless ( $ua ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($main::ua) 0 0 0
298 0 $ua = LWP::UserAgent::Custom->new;
299 0 $uuid = uuid();
300 }
301  
302 0 foreach my $comic ( @plugins ) {
303 0 ▶ warn("Plugin: ", $comic->{name}, "\n") if $verbose > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose > 1 0 0 0
304  
305 # Force existence of this comic's state otherwise
306 # it will be autovivified within the fetch method
307 # and never get outside.
308 0 ▶ $state->{comics}->{$comic->{tag}} ||= {};
Condition0%0/2
$state->{"comics"}{$comic->{"tag"}} ||= {}
l!l
00
MC/DC0%0/1
$state->{"comics"}{$comic->{"tag"}} ||= {}
$state->{"comics"}{$comic->{"tag"}}
Truth table0%0/2
A
A= $state->{"comics"}{$comic->{"tag"}}
Aresult
11
00
309  
310 # Make the state accessible.
311 0 $comic->{state} = $state->{comics}->{$comic->{tag}};
312  
313 # Skip is disabled.
314 0 ▶ next if $comic->{state}->{disabled} && !$force;
Branch: 1 branch
BranchTrueFalseTotal
if $comic->{"state"}{"disabled"} and not $force 0 0 0
Condition0%0/3
$comic->{"state"}{"disabled"} and not $force
!ll&&!rl&&r
000
MC/DC0%0/2
$comic->{"state"}{"disabled"} and not $force
$comic->{"state"}{"disabled"} not $force
Truth table0%0/3
A and not(B)
A= $comic->{"state"}{"disabled"}B= not $force
ABresult
0X0
100
111
315  
316 # Run it, trapping errors.
317 0 $stats->{tally}++;
318 0 ▶ unless ( eval { $comic->fetch($reuse); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $comic->fetch($reuse); 1 } }) 0 0 0
319 0 $comic->{state}->{fail} = $@;
320 0 debug($comic->{state}->{fail});
321 0 push( @{ $stats->{fail} },
322 0 [ $comic->{name}, $comic->{state}->{fail} ] );
323 }
324 }
325 }
326  
327 ################ Index subroutines ################
328  
329 0 ▶sub build {
Subroutine build: not called CC=10 SCAR=47.0
330  
331 # Change to the spooldir and collect all HTML fragments.
332 0 ▶ chdir($spooldir) or die("$spooldir: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
chdir $spooldir or die "${spooldir}: $!\n" 0 0 0
333 0 opendir( my $dir, "." );
334 0 my @files = grep { /^[^._].+(?<!index)\.(?:html)$/ } readdir($dir);
335 0 close($dir);
336 0 ▶ warn("Number of images = ", scalar(@files), "\n") if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
337 0 ▶ $stats->{tally} = $stats->{uptodate} = @files if $rebuild;
Branch: 1 branch
BranchTrueFalseTotal
if $rebuild 0 0 0
338  
339 # Sort the fragments on last modification date.
340 @files =
341 0 map { $_->[0] }
342 0 sort { $b->[1] <=> $a->[1] }
343 0 ▶ grep { $force || ! $state->{comics}->{$_->[2]}->{disabled} }
Condition0%0/3
$force || !$state->{'comics'}{$_->[2]}{'disabled'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$force || !$state->{'comics'}{$_->[2]}{'disabled'}
$force !$state->{'comics'}{$_->[2]}{'disabled'}
Truth table0%0/3
A || not(B)
A= $forceB= !$state->{'comics'}{$_->[2]}{'disabled'}
ABresult
1X1
011
000
344 0 map { ( my $t = $_ ) =~ s/\.\w+$//;
345 0 [ $_, (stat($_))[9], $t ] }
346 @files;
347  
348 0 ▶ if ( $debug > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 1) 0 0 0
349 0 warn("Images (sorted):\n");
350 0 warn(" $_\n") for @files;
351 }
352  
353 # Creat icon.
354 0 ▶ unless ( -s "comics.png" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-s "comics.png") 0 0 0
355 0 require Comics::Utils::Icon;
356 0 open( my $fd, '>:raw', "comics.png" );
357 0 print $fd Comics::Utils::Icon::icon();
358 0 close($fd);
359 }
360  
361 # Create a new index.html.
362 0 open( my $fd, '>:utf8', "index.html" );
363 0 preamble($fd);
364 0 htmlstats($fd);
365 0 for ( @files ) {
366 0 ▶ open( my $hh, '<:utf8', $_ )
Branch: 1 branch
BranchTrueFalseTotal
open my $hh, "<:utf8", $_ or die "${_}: $!" 0 0 0
367 or die("$_: $!");
368 0 print { $fd } <$hh>;
369 0 close($hh);
370 }
371 0 postamble($fd);
372 0 close($fd);
373 }
374  
375 0 ▶sub preamble {
Subroutine preamble: not called CC=1 SCAR=6.9
376 0 my ( $fd ) = @_;
377 0 print $fd <<EOD;
378 <html>
379 <head>
380 <title>Comics!</title>
381 <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
382 <style type="text/css">
383 body {
384 font-family : Verdana, Arial, Helvetica, sans-serif;
385 text-align: center;
386 margin-top: 0px;
387 margin-right: 0px;
388 margin-bottom: 10px;
389 margin-left: 0px;
390 font-size:12pt;
391 }
392 .toontable {
393 background-color: #eee;
394 padding: 9px;
395 margin: 18px;
396 border: 1px solid #ddd;
397 }
398 .toonimage {
399 background-color: white;
400 border: 0px;
401 }
402 a {
403 text-decoration: none;
404 color: black;
405 }
406 </style>
407 </head>
408 <body bgcolor='#ffffff'>
409 <a name="top"></a>
410 <div align="center">
411 EOD
412 }
413  
414 0 ▶sub postamble {
Subroutine postamble: not called CC=1 SCAR=6.9
415 0 my ( $fd ) = @_;
416 0 print $fd <<EOD;
417 </div>
418 </body>
419 </html>
420 EOD
421 }
422  
423 0 ▶sub htmlstats {
Subroutine htmlstats: not called CC=1 SCAR=6.9
424 0 my ( $fd ) = @_;
425 0 print $fd <<EOD;
426 <table width="100%" class="toontable" cellpadding="5" cellspacing="0">
427 <tr><td nowrap align="center">
428 <p style="margin-left:5px"><a href="http://johan.vromans.org/software/sw_comics.html" target="_blank"><img src="comics.png" width="100" height="100" alt="[Comics]" align="middle"><font size="+4"><bold>Comics</bold></font></a><br>
429 0 <font size="-2">Comics $VERSION, last run: @{[ "".localtime() ]}<br>@{[ statmsg(1) ]}</font><br>
430 </p> </td>
431 </tr>
432 </table>
433 EOD
434 }
435  
436 ################ Statistics subroutines ################
437  
438 0 ▶sub statistics {
Subroutine statistics: not called CC=2 SCAR=17.9
439 0 ▶ return unless $verbose;
Branch: 1 branch
BranchTrueFalseTotal
unless $verbose 0 0 0
440 0 warn( statmsg(), "\n" );
441 }
442  
443 0 ▶sub statmsg {
Subroutine statmsg: not called CC=6 SCAR=37.4
444 0 my ( $html ) = @_;
445 0 my $loaded = $stats->{loaded};
446 0 my $tally = $stats->{tally};
447 0 my $uptodate = $stats->{uptodate};
448 0 my $fail = @{ $stats->{fail} };
449 0 my $disabled = $stats->{disabled};
450 0 my $excluded = $stats->{excluded};
451 0 my $new = $stats->{tally} - $stats->{uptodate} - $fail;
452 0 my $res = "Number of comics = $loaded (".
453 "$new new, " .
454 "$uptodate uptodate";
455 0 ▶ $res .= ", $disabled disabled" if $disabled;
Branch: 1 branch
BranchTrueFalseTotal
if $disabled 0 0 0
456 0 ▶ $res .= ", $excluded excluded" if $excluded;
Branch: 1 branch
BranchTrueFalseTotal
if $excluded 0 0 0
457 0 ▶ if ( $fail ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fail) 0 0 0
458 0 ▶ if ( $html ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html) { } 0 0 0
459 0 $res .= ", <span title=\"";
460 0 for ( @{ $stats->{fail} } ) {
461 0 my $t = $_->[1];
462 0 $t =~ s/ at .*//s;
463 0 $res .= $_->[0] . " ($t)&#10;";
464 }
465 0 $res .= "\">$fail fail</span>";
466 }
467 else {
468 0 $res .= ", $fail fail";
469 }
470 }
471 0 return "$res)";
472 }
473  
474 ################ Miscellaneous ################
475  
476 0 ▶sub spoolfile {
Subroutine spoolfile: not called CC=1 SCAR=6.9
477 0 my ( $file ) = @_;
478 0 File::Spec->catfile( $spooldir, $file );
479 }
480  
481 0 ▶sub uuid {
Subroutine uuid: not called CC=2 SCAR=17.9
482 0 my @chars = ( 'a'..'f', 0..9 );
483 0 my @string;
484 0 push( @string, $chars[int(rand(16))]) for (1..32);
485 0 splice( @string, 8, 0, '-');
486 0 splice( @string, 13, 0, '-');
487 0 splice( @string, 18, 0, '-');
488 0 splice( @string, 23, 0, '-');
489 0 return join('', @string);
490 }
491  
492 0 ▶sub debug {
Subroutine debug: not called CC=2 SCAR=17.9
493 0 ▶ return unless $debug;
Branch: 1 branch
BranchTrueFalseTotal
unless $debug 0 0 0
494 0 warn(@_,"\n");
495 }
496  
497 0 ▶sub debugging {
Subroutine debugging: not called CC=1 SCAR=6.9
498 0 $debug;
499 }
500  
501 ################ Command line handling ################
502  
503 0 ▶sub app_options {
Subroutine app_options: not called CC=10 SCAR=47.0
504 0 my $help = 0; # handled locally
505 0 my $ident = 0; # handled locally
506 0 my $man = 0; # handled locally
507  
508 0 ▶ my $pod2usage = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
509 # Load Pod::Usage only if needed.
510 0 require Pod::Usage;
511 0 Pod::Usage->import;
512 0 &pod2usage;
513 0 };
514  
515 # Process options.
516 0 ▶ if ( @ARGV > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV > 0) 0 0 0
517 GetOptions('spooldir=s' => \$spooldir,
518 'refresh' => \$refresh,
519 'rebuild' => \$rebuild,
520 'enable' => \$activate,
521 0 ▶ 'disable' => sub { $activate = -1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
522 'list' => \$list,
523 'force' => \$force,
524 'reuse' => \$reuse,
525 'ident' => \$ident,
526 'verbose+' => \$verbose,
527 0 ▶ 'quiet' => sub { $verbose = 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
528 0 ▶ 'trace' => \$trace,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("spooldir=s", \$spooldir, "refresh", \$refresh, "rebuild", \$rebuild, "enable", \$activate, "disable", sub { $activate = -1; } , "list", \$list, "force", \$force, "reuse", \$reuse, "ident", \$ident, "verbose+", \$verbose, "quiet", sub { $verbose = 0; } , "trace", \$trace, "help|?", \$help, "man", \$man, "debug", \$debug) or &$pod2usage(2) 0 0 0
529 'help|?' => \$help,
530 'man' => \$man,
531 'debug' => \$debug)
532 or $pod2usage->(2);
533 }
534 0 ▶ if ( $ident or $help or $man ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ident or $help or $man) 0 0 0
Condition0%0/6
$ident or $help or $man
l!l&&r!l&&!r
000
$ident or $help
l!l&&r!l&&!r
000
MC/DC0%0/3
$ident or $help or $man
$ident $help $man
Truth table0%0/4
A or B or C
A= $identB= $helpC= $man
ABCresult
1XX1
01X1
0011
0000
535 0 print STDERR ("This is $my_name version $VERSION\n");
536 }
537 0 ▶ if ( $man or $help ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($man or $help) 0 0 0
Condition0%0/3
$man or $help
l!l&&r!l&&!r
000
MC/DC0%0/2
$man or $help
$man $help
Truth table0%0/3
A or B
A= $manB= $help
ABresult
1X1
011
000
538 0 ▶ $pod2usage->(1) if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 0 0
539 0 ▶ $pod2usage->(VERBOSE => 2) if $man;
Branch: 1 branch
BranchTrueFalseTotal
if $man 0 0 0
540 }
541 }
542  
543 ################ Documentation ################
544  
545 =head1 NAME
546  
547 Comics - Comics aggregator in the style of Gotblah
548  
549 =head1 SYNOPSIS
550  
551 perl -MComics -e 'main()' -- [options] [plugin ...]
552  
553 or
554  
555 perl Comics.pm [options] [plugin ...]
556  
557 If the associated C<collect> tool has been installed properly:
558  
559 collect [options] [plugin ...]
560  
561 Options:
562 --spooldir=XXX where resultant images and index must be stored
563 --enable enables the plugins (no aggregation)
564 --disable disables the plugins (no aggregation)
565 --list lists the plugins (no aggregation)
566 --rebuild rebuild index.html, no fetching
567 --refresh consider all images as new
568 --ident shows identification
569 --help shows a brief help message and exits
570 --man shows full documentation and exits
571 --verbose provides more verbose information
572 --quiet provides no information unless failure
573  
574 =head1 OPTIONS
575  
576 =over 8
577  
578 =item B<--spooldir=>I<XXX>
579  
580 Designates the spool area. Downloaded comics and index files are
581 written here.
582  
583 =item B<--enable>
584  
585 The plugins that are named on the command line will be enabled for
586 future runs of the aggregator. Default is to enable all plugins.
587  
588 Note that when this command is used, the program exits after enabling
589 the plugins. No aggregation takes place.
590  
591 =item B<--disable>
592  
593 The plugins that are named on the command line will be disabled for
594 future runs of the aggregator. Default is to disable all plugins.
595  
596 Note that when this command is used, the program exits after disabling
597 the plugins. No aggregation takes place.
598  
599 =item B<--list>
600  
601 Provides information on the selected (default: all) plugins.
602  
603 Note that when this command is used, no aggregation takes place.
604  
605 =item B<--rebuild>
606  
607 Recreates index.html in the spooldir without fetching new comics.
608  
609 =item B<--help>
610  
611 Prints a brief help message and exits.
612  
613 =item B<--man>
614  
615 Prints the manual page and exits.
616  
617 =item B<--ident>
618  
619 Prints program identification.
620  
621 =item B<--verbose>
622  
623 Provides more verbose information. This option may be repeated for
624 even more verbose information.
625  
626 =item B<--quiet>
627  
628 Silences verbose information.
629  
630 =item I<plugin>
631  
632 If present, process only the specified plugins.
633  
634 This is used for disabling and enabling plugins, but it can also be
635 used to test individual plugins.
636  
637 =back
638  
639 =head1 DESCRIPTION
640  
641 The normal task of this program is to perform aggregation. it will
642 load the available plugins and run all of them.
643  
644 The plugins will examine the contents of comics sites and update the
645 'cartoon of the day' in the spool area.
646  
647 Upon completion, an index.html is generated in the spool area to view
648 the comics collection.
649  
650 It is best to run this program from the spool area itself.
651  
652 =head2 Special commands
653  
654 Note that no aggregation is performed when using any of these commands.
655  
656 With command line option B<--list> a listing of the plugins is produced.
657  
658 Plugins can be enabled and disabled with B<--enable> and B<--disable>
659 respectively.
660  
661 =head1 PLUGINS
662  
663 B<Important:> This program assumes that the plugins can be found in
664 C<../lib> relative to the location of the executable file.
665  
666 All suitable C<Comics::Plugin::>I<plugin>C<.pm> files are examined
667 and loaded.
668  
669 Plugins are derived from Fetcher classes, see below.
670  
671 See L<Comics::Plugin::Sigmund> for a fully commented plugin.
672  
673 =head1 FETCHERS
674  
675 Fetchers implement different fetch strategies. Currently provided are:
676  
677 L<Comics::Fetcher::Cascade> - fetch a comic by loading and examining a series of URLs.
678  
679 L<Comics::Fetcher::Direct> - fetch a comic by URL.
680  
681 L<Comics::Fetcher::Single> - fetch a comic by examining the comic's home page.
682  
683 L<Comics::Fetcher::GoComics> - fetch a comic from a GoComics site.
684  
685 =cut
686  
687 package LWP::UserAgent::Custom;
688 1 ▶use parent qw(LWP::UserAgent);
Subroutine BEGIN: called CC=1 SCAR=0.0
689  
690 1 ▶use HTTP::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
691 1 my $cookie_jar;
692  
693 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
694 0 my ( $pkg ) = @_;
695 0 my $self = $pkg->SUPER::new();
696 0 bless $self, $pkg;
697  
698 0 $self->agent('Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0');
699 0 $self->timeout(10);
700 0 ▶ $cookie_jar ||= HTTP::Cookies->new
Condition0%0/3
$cookie_jar ||= "HTTP::Cookies"->new("file", main::spoolfile(".lwp_cookies.dat"), "autosave", 1, "ignore_discard", 1)
l!l&&r!l&&!r
000
MC/DC0%0/2
$cookie_jar ||= "HTTP::Cookies"->new("file", main::spoolfile(".lwp_cookies.dat"), "autosave", 1, "ignore_discard", 1)
$cookie_jar "HTTP::Cookies"->new("file", main::spoolfile(".lwp_cookies.dat"), "autosave", 1, "ignore_discard", 1)
Truth table0%0/3
A ||= B
A= $cookie_jarB= "HTTP::Cookies"->new("file", main::spoolfile(".lwp_cookies.dat"), "autosave", 1, "ignore_discard", 1)
ABresult
1X1
011
000
701 (
702 file => ::spoolfile(".lwp_cookies.dat"),
703 autosave => 1,
704 ignore_discard => 1,
705 );
706 0 $self->cookie_jar($cookie_jar);
707  
708 0 return $self;
709 }
710  
711 0 ▶sub get {
Subroutine get: not called CC=6 SCAR=34.3
712 0 my ( $self, $url ) = @_;
713  
714 0 my $res;
715  
716 0 my $sleep = 1;
717 0 for ( 0..4 ) {
718 0 $res = $self->SUPER::get($url);
719 0 $cookie_jar->save;
720 0 ▶ last if $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $res->is_success 0 0 0
721 # Some sites block LWP queries. Show why.
722 0 ▶ if ( $res->status_line =~ /^403/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->status_line =~ /^403/u) 0 0 0
723 1 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
724 0 warn(Dumper($res));
725 0 exit;
726 }
727 0 ▶ last if $res->status_line !~ /^5/; # not temp fail
Branch: 1 branch
BranchTrueFalseTotal
unless $res->status_line =~ /^5/u 0 0 0
728 0 ▶ print STDERR "Retry..." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
729 0 sleep $sleep;
730 0 $sleep += $sleep;
731 }
732  
733 0 return $res;
734 }
735  
736 1 1;
737  
738 =head1 AUTHOR
739  
740 Johan Vromans, C<< <JV at CPAN dot org> >>
741  
742 =head1 SUPPORT
743  
744 Development of this module takes place on GitHub:
745 https://github.com/sciurius/comics .
746  
747 You can find documentation for this module with the perldoc command.
748  
749 perldoc Comics
750  
751 Please report any bugs or feature requests using the issue tracker on
752 GitHub.
753  
754 =head1 ACKNOWLEDGEMENTS
755  
756 The people behind Gotblah, for creating the original tool.
757  
758 =head1 LICENSE
759  
760 Copyright (C) 2016,2018 Johan Vromans,
761  
762 This module is free software. You can redistribute it and/or
763 modify it under the terms of the Artistic License 2.0.
764  
765 This program is distributed in the hope that it will be useful,
766 but without any warranty; without even the implied warranty of
767 merchantability or fitness for a particular purpose.
768  
769 =cut
770  
771 package main;
772  
773 1 ▶unless ( caller ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller) 0 1 1
774 0 main();
775 }
776  
777 1 1;
« lib/Comics/Fetcher/Cascade.pm ↑ Summary lib/Comics/Plugin/Base.pm »