| 1 | #! perl | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | package Comics::Fetcher::Base; | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | Comics::Fetcher::Base -- Base class for Fetchers. | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | package Comics::Fetcher::Direct; | ||||||||||||||||||||||||||||
| 17 | use parent qw(Comics::Fetcher::Base); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | The Fetcher Base class provides tools for fetching URLs and saving | ||||||||||||||||||||||||||||
| 22 | data to disk. | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | The primary entry point for a Fetcher is method C<fetch>, which must | ||||||||||||||||||||||||||||
| 25 | be implemented by the derived classes. | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =cut | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | ▶ | use parent qw(Comics::Plugin::Base); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1 | our $VERSION = "1.00"; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub fetch { | ||||||||||||||||||||||||||
|
Subroutine
fetch: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 34 | 0 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 35 | 0 | die( ref($self), ": Method 'fetch' not defined\n" ); | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | ################ Subroutines ################ | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 1 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 1 | ▶ | use File::LoadLines; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =head1 METHODS | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =head2 spoolfile($file) | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | Returns the full name for the given i<file> in the configured spool | ||||||||||||||||||||||||||||
| 48 | directory. | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =cut | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | sub spoolfile { | ||||||||||||||||||||||||||
|
Subroutine
spoolfile: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 0 | my ( $self, $file ) = @_; | |||||||||||||||||||||||||||
| 54 | 0 | ::spoolfile($file); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 1 | ▶ | use Digest::MD5 qw(md5_base64); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 1 | ▶ | use Image::Info qw(image_info); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =head2 urlabs($url, $path) | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | Returns the full URL for the given i<path>, possibly relative to I<url>. | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =cut | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | sub urlabs { | ||||||||||||||||||||||||||
|
Subroutine
urlabs: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 0 | my ( $self, $url, $path ) = @_; | |||||||||||||||||||||||||||
| 69 | 0 | ▶ | if ( $path =~ m;^/; ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | if ( $path =~ m;^//; ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 71 | 0 | $path = "http:" . $path; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | elsif ( $path =~ m;^/static\.nrc\.nl/; ) { # TODO | ||||||||||||||||||||||||||||
| 74 | 0 | $path = "https:/" . $path; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | else { | ||||||||||||||||||||||||||||
| 77 | 0 | $url =~ s;(^\w+://.*?)/.*;$1;; | |||||||||||||||||||||||||||
| 78 | 0 | $path = $url . $path; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | elsif ( $path !~ m;^\w+://; ) { | ||||||||||||||||||||||||||||
| 82 | 0 | $path = $url . "/" . $path; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | return $path; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head2 save_image($image, $dataref) | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | Saves the contents of I<dataref> to the spooldir, using I<image> as | ||||||||||||||||||||||||||||
| 91 | the name for the file. | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | See also: B<spoolfile>. | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =cut | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | sub save_image { | ||||||||||||||||||||||||||
|
Subroutine
save_image: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | 0 | my ( $self, $image, $data ) = @_; | |||||||||||||||||||||||||||
| 99 | 0 | my $f = $self->spoolfile($image); | |||||||||||||||||||||||||||
| 100 | 0 | open( my $fd, ">:raw", $f ); | |||||||||||||||||||||||||||
| 101 | 0 | print $fd $$data; | |||||||||||||||||||||||||||
| 102 | 0 | ▶ | close($fd) or warn("$f: $!\n"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | ::debug("Wrote: $f"); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head2 save_html($html) | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | Generates and saves the HTML fragment for this comic to the spooldir, | ||||||||||||||||||||||||||||
| 109 | using I<html> as the name for the file. | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | See also: B<spoolfile>. | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =cut | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | sub save_html { | ||||||||||||||||||||||||||
|
Subroutine
save_html: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 116 | 0 | my ( $self, $html, $data ) = @_; | |||||||||||||||||||||||||||
| 117 | 0 | my $f = $self->spoolfile($html); | |||||||||||||||||||||||||||
| 118 | 0 | open( my $fd, ">:utf8", $f ); | |||||||||||||||||||||||||||
| 119 | 0 | ▶ | print $fd ( $data // $self->html ); | ||||||||||||||||||||||||||
|
Condition0%0/3
$data // $self->html
MC/DC0%0/2
$data // $self->html
$data
$self->html
Truth table0%0/3
A // B
A= $dataB= $self->html
| |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | close($fd) or warn("$f: $!\n"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 0 | ::debug("Wrote: $f"); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =head2 load_html($html) | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | Loads the HTML fragment for this comic from the spooldir, | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | See also: B<spoolfile>. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =cut | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | sub load_html { | ||||||||||||||||||||||||||
|
Subroutine
load_html: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 133 | 0 | my ( $self, $html ) = @_; | |||||||||||||||||||||||||||
| 134 | 0 | my $f = $self->spoolfile($html); | |||||||||||||||||||||||||||
| 135 | 0 | my $opts = { split => 0, fail => "soft" }; | |||||||||||||||||||||||||||
| 136 | 0 | my $data = loadlines( $f, $opts ); | |||||||||||||||||||||||||||
| 137 | 0 | ▶ | if ( $opts->{error} || !$data ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$opts->{"error"} or not $data
MC/DC0%0/2
$opts->{"error"} or not $data
$opts->{"error"}
not $data
Truth table0%0/3
A or not(B)
A= $opts->{"error"}B= not $data
| |||||||||||||||||||||||||||||
| 138 | 0 | ::debug("Cannot reuse $html: ", $opts->{error}, "\n"); | |||||||||||||||||||||||||||
| 139 | 0 | return; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | 0 | ::debug("Read: $f"); | |||||||||||||||||||||||||||
| 142 | 0 | return $data; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 1 | 1; | |||||||||||||||||||||||||||