File Coverage

lib/Comics/Fetcher/Base.pm
Criterion Covered Total %
statement 27 61 44.2
branch 0 14 0.0
condition 0 6 0.0
subroutine 9 15 60.0
pod 5 6 83.3
total 41 102 40.2


line stmt bran cond sub pod time code
1             #! perl
2              
3 1     1   1785 use strict;
  1         3  
  1         131  
4 1     1   8 use warnings;
  1         10  
  1         71  
5 1     1   8 use utf8;
  1         2  
  1         9  
6 1     1   59 use Carp;
  1         2  
  1         108  
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, which must
25             be implemented by the derived classes.
26              
27             =cut
28              
29 1     1   8 use parent qw(Comics::Plugin::Base);
  1         3  
  1         9  
30              
31             our $VERSION = "1.00";
32              
33             sub fetch {
34 0     0 0   my ( $self ) = @_;
35 0           die( ref($self), ": Method 'fetch' not defined\n" );
36             }
37              
38             ################ Subroutines ################
39              
40 1     1   168 use File::Spec;
  1         2  
  1         32  
41 1     1   6 use File::LoadLines;
  1         3  
  1         120  
42              
43             =head1 METHODS
44              
45             =head2 spoolfile($file)
46              
47             Returns the full name for the given i in the configured spool
48             directory.
49              
50             =cut
51              
52             sub spoolfile {
53 0     0 1   my ( $self, $file ) = @_;
54 0           ::spoolfile($file);
55             }
56              
57 1     1   8 use Digest::MD5 qw(md5_base64);
  1         2  
  1         112  
58              
59 1     1   878 use Image::Info qw(image_info);
  1         2726  
  1         1129  
60              
61             =head2 urlabs($url, $path)
62              
63             Returns the full URL for the given i, possibly relative to I.
64              
65             =cut
66              
67             sub urlabs {
68 0     0 1   my ( $self, $url, $path ) = @_;
69 0 0         if ( $path =~ m;^/; ) {
    0          
70 0 0         if ( $path =~ m;^//; ) {
    0          
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 to the spooldir, using I as
91             the name for the file.
92              
93             See also: B.
94              
95             =cut
96              
97             sub save_image {
98 0     0 1   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 0         close($fd) or warn("$f: $!\n");
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 as the name for the file.
110              
111             See also: B.
112              
113             =cut
114              
115             sub save_html {
116 0     0 1   my ( $self, $html, $data ) = @_;
117 0           my $f = $self->spoolfile($html);
118 0           open( my $fd, ">:utf8", $f );
119 0   0       print $fd ( $data // $self->html );
120 0 0         close($fd) or warn("$f: $!\n");
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.
129              
130             =cut
131              
132             sub load_html {
133 0     0 1   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 0 0       if ( $opts->{error} || !$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;