File Coverage

lib/WWW/Comic/Plugin/uComics.pm
Criterion Covered Total %
statement 18 65 27.6
branch 3 38 7.8
condition 2 24 8.3
subroutine 5 8 62.5
pod 5 5 100.0
total 33 140 23.5


line stmt bran cond sub pod time code
1             ############################################################
2             #
3             # $Id: uComics.pm,v 1.3 2006/01/10 15:45:58 nicolaw Exp $
4             # WWW::Comic::Plugin::uComics - uComics plugin for WWW::Comic
5             #
6             # Copyright 2006 Nicola Worthington
7             #
8             # Licensed under the Apache License, Version 2.0 (the "License");
9             # you may not use this file except in compliance with the License.
10             # You may obtain a copy of the License at
11             #
12             # http://www.apache.org/licenses/LICENSE-2.0
13             #
14             # Unless required by applicable law or agreed to in writing, software
15             # distributed under the License is distributed on an "AS IS" BASIS,
16             # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17             # See the License for the specific language governing permissions and
18             # limitations under the License.
19             #
20             ############################################################
21              
22             package WWW::Comic::Plugin::uComics;
23             # vim:ts=4:sw=4:tw=78
24              
25 2     2   4870 use strict;
  2         5  
  2         99  
26 2     2   12 use Carp qw(carp croak);
  2         26  
  2         189  
27              
28 2     2   12 use vars qw($VERSION @ISA %COMICS $HAVE_PROBED);
  2         4  
  2         2604  
29             $VERSION = sprintf('%d.%02d', q$Revision: 1.3 $ =~ /(\d+)/g);
30             @ISA = qw(WWW::Comic::Plugin);
31             $HAVE_PROBED = 0;
32             %COMICS = (
33             garfield => 'Garfield',
34             );
35              
36             sub new {
37 1     1 1 21 my $class = shift;
38 1         4 my $self = { homepage => 'http://www.ucomics.com' };
39 1         3 bless $self, $class;
40 1         3 return $self;
41             }
42              
43             sub strip_url {
44 0     0 1 0 my $self = shift;
45 0         0 my %param = @_;
46              
47             # If we don't know about this comic and we've not probed before,
48             # then go and probe for the first time
49 0 0 0     0 if (!exists($COMICS{$param{comic}}) && !$HAVE_PROBED) {
50 0         0 $self->comics(probe => 1);
51             }
52              
53             # If we've probed and we still do not know about this comic then
54             # return undef and complain if perl warnings are turned on
55 0 0 0     0 if ($HAVE_PROBED && !exists($COMICS{$param{comic}})) {
56 0 0       0 carp "I do not know how to handle comic '$param{comic}'" if $^W;
57 0         0 return undef;
58             }
59              
60 0         0 my $url = "$self->{homepage}/$param{comic}/";
61 0 0       0 if (exists $param{id}) {
62 0 0       0 if (my ($yy,$mm,$dd) = $param{id} =~ m/^(\d\d)(\d\d)(\d\d)$/) {
    0          
63 0 0       0 if ($yy =~ /^9/) { $yy = "19$yy"; }
  0         0  
64 0         0 else { $yy = "20$yy"; }
65 0         0 $url .= "$yy/$mm/$dd/";
66             } elsif ($param{id} =~ m/^\d{4}\/\d{2}\/\d{2}$/) {
67 0         0 $url .= "$param{id}";
68             }
69             }
70              
71 0   0     0 $self->{ua} ||= $self->_new_agent();
72 0         0 my $response = $self->{ua}->get($url);
73 0 0       0 if ($response->is_success) {
    0          
74 0         0 my $html = $response->content;
75 0 0       0 if ($html =~ m#
76             /comics/[a-z0-9\_\-/]+?/[a-z0-9]+\.(gif|jpg|png))"#imsx) {
77 0         0 my $url = $1;
78 0 0       0 $url = "$self->{homepage}$1" unless $url =~ /^https?:\/\//i;
79 0         0 return $url;
80             }
81              
82             } elsif ($^W) {
83 0         0 carp($response->status_line);
84             }
85              
86 0         0 return undef;
87             }
88              
89             sub comics {
90 25     25 1 141 my $self = shift;
91 25 50       199 my %param = @_ % 2 ? (@_,1) : @_;
92              
93             # If we have comic information then return it
94 25 50       92 if (keys(%COMICS)) {
95             # Unless we've never probed before and we're being asked to probe
96 25 50 33     168 unless (!$HAVE_PROBED && exists $param{probe} && $param{probe}) {
      33        
97 25         143 return (keys(%COMICS));
98             }
99             }
100              
101             # Only continue if we've never probed before and we're
102             # being asked to probe
103 0 0 0       unless (!$HAVE_PROBED && exists $param{probe} && $param{probe}) {
      0        
104 0           return (keys(%COMICS));
105             }
106              
107 0           $HAVE_PROBED = 1;
108 0   0       $self->{ua} ||= $self->_new_agent;
109 0           my $response = $self->{ua}->get($self->{homepage});
110 0 0         if ($response->is_success) {
    0          
111 0           my $html = $response->content;
112 0           while (my ($str,$comic,$title) = $html =~
113             m#((.+?))#ims) {
114 0           $COMICS{$comic} = $title;
115 0           $html =~ s#/$comic/##ig;
116             }
117              
118             } elsif ($^W) {
119 0           carp "Failed to retrieve $self->{homepage}: ".$response->status_line;
120             }
121              
122 0           return (keys(%COMICS));
123             }
124              
125             sub title {
126 0     0 1   my $self = shift;
127 0           my %param = @_;
128 0 0         if (exists $COMICS{$param{comic}}) {
129 0           return $COMICS{$param{comic}};
130             }
131 0           return undef;
132             }
133              
134             sub homepage {
135 0     0 1   my $self = shift;
136 0           my %param = @_;
137 0 0         if (exists $COMICS{$param{comic}}) {
138 0           return "$self->{homepage}/$param{comic}}/";
139             }
140 0           return undef;
141             }
142              
143             1;
144              
145             =pod
146              
147             =head1 NAME
148              
149             WWW::Comic::Plugin::uComics - uComics plugin for WWW::Comic
150              
151             =head1 SYNOPSIS
152              
153             # Actively probe www.comics.com to return (and cache)
154             # a list of supported comics
155             my @comics = $plugin->comics(probe => 1);
156            
157             # Return a list of supported comics that has already
158             # been cached in memory
159             @comics = $plugin->comics;
160            
161             # Return the comic homepage URL
162             my $url = $plugin->homepage(comic => "peanuts");
163              
164             See L.
165              
166             =head1 METHODS
167              
168             See L and L for a list of standard
169             methods that this module supports.
170              
171             Additional methods are:
172              
173             =over 4
174              
175             =item title
176              
177             Returns the full title of comic strip.
178              
179             =item homepage
180              
181             Returns the comic homepage URL.
182              
183             =back
184              
185             =head1 VERSION
186              
187             $Id: uComics.pm,v 1.3 2006/01/10 15:45:58 nicolaw Exp $
188              
189             =head1 AUTHOR
190              
191             Nicola Worthington
192              
193             L
194              
195             =head1 COPYRIGHT
196              
197             Copyright 2006 Nicola Worthington.
198              
199             This software is licensed under The Apache Software License, Version 2.0.
200              
201             L
202              
203             =cut
204