File Coverage

lib/Comics/Fetcher/Base.pm
Criterion Covered Total %
statement 24 59 40.6
branch 0 12 0.0
condition 0 3 0.0
subroutine 8 14 57.1
pod 5 6 83.3
total 37 94 39.3


line stmt bran cond sub pod time code
1             #! perl
2              
3 1     1   1244 use strict;
  1         10  
  1         30  
4 1     1   6 use warnings;
  1         2  
  1         24  
5 1     1   5 use utf8;
  1         2  
  1         15  
6 1     1   25 use Carp;
  1         3  
  1         77  
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   6 use parent qw(Comics::Plugin::Base);
  1         26  
  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   97 use File::Spec;
  1         2  
  1         54  
41              
42             =head1 METHODS
43              
44             =head2 spoolfile($file)
45              
46             Returns the full name for the given i in the configured spool
47             directory.
48              
49             =cut
50              
51             sub spoolfile {
52 0     0 1   my ( $self, $file ) = @_;
53 0           ::spoolfile($file);
54             }
55              
56 1     1   5 use Digest::MD5 qw(md5_base64);
  1         2  
  1         56  
57              
58 1     1   626 use Image::Info qw(image_info);
  1         1720  
  1         626  
59              
60             =head2 urlabs($url, $path)
61              
62             Returns the full URL for the given i, possibly relative to I.
63              
64             =cut
65              
66             sub urlabs {
67 0     0 1   my ( $self, $url, $path ) = @_;
68 0 0         if ( $path =~ m;^/; ) {
    0          
69 0 0         if ( $path =~ m;^//; ) {
70 0           $path = "http:" . $path;
71             }
72             else {
73 0           $url =~ s;(^\w+://.*?)/.*;$1;;
74 0           $path = $url . $path;
75             }
76             }
77             elsif ( $path !~ m;^\w+://; ) {
78 0           $path = $url . "/" . $path;
79             }
80              
81 0           return $path;
82             }
83              
84             =head2 save_image($image, $dataref)
85              
86             Saves the contents of I to the spooldir, using I as
87             the name for the file.
88              
89             See also: B.
90              
91             =cut
92              
93             sub save_image {
94 0     0 1   my ( $self, $image, $data ) = @_;
95 0           my $f = $self->spoolfile($image);
96 0           open( my $fd, ">:raw", $f );
97 0           print $fd $$data;
98 0 0         close($fd) or warn("$f: $!\n");
99 0           ::debug("Wrote: $f");
100             }
101              
102             =head2 save_html($html)
103              
104             Generates and saves the HTML fragment for this comic to the spooldir,
105             using I as the name for the file.
106              
107             See also: B.
108              
109             =cut
110              
111             sub save_html {
112 0     0 1   my ( $self, $html, $data ) = @_;
113 0           my $f = $self->spoolfile($html);
114 0           open( my $fd, ">:utf8", $f );
115 0   0       print $fd ( $data // $self->html );
116 0 0         close($fd) or warn("$f: $!\n");
117 0           ::debug("Wrote: $f");
118             }
119              
120             =head2 load_html($html)
121              
122             Loads the HTML fragment for this comic from the spooldir,
123              
124             See also: B.
125              
126             =cut
127              
128             sub load_html {
129 0     0 1   my ( $self, $html ) = @_;
130 0           my $f = $self->spoolfile($html);
131 0           open( my $fd, "<:utf8", $f );
132 0 0         unless ( $fd ) {
133 0           ::debug("Cannot reuse $html: $!\n");
134 0           return;
135             }
136 0           my $data = do { local $/; <$fd> };
  0            
  0            
137 0           ::debug("Read: $f");
138 0           return $data;
139             }
140              
141             1;