| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
############################################################ |
|
2
|
|
|
|
|
|
|
# |
|
3
|
|
|
|
|
|
|
# $Id: Dilbert.pm,v 1.19 2006/01/12 22:30:11 nicolaw Exp $ |
|
4
|
|
|
|
|
|
|
# WWW::Dilbert - Retrieve Dilbert of the day comic strip images |
|
5
|
|
|
|
|
|
|
# |
|
6
|
|
|
|
|
|
|
# Copyright 2004,2005,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::Dilbert; |
|
23
|
|
|
|
|
|
|
# vim:ts=4:sw=4:tw=78 |
|
24
|
|
|
|
|
|
|
|
|
25
|
3
|
|
|
3
|
|
38659
|
use strict; |
|
|
3
|
|
|
|
|
11
|
|
|
|
3
|
|
|
|
|
139
|
|
|
26
|
3
|
|
|
3
|
|
17
|
use Exporter; |
|
|
3
|
|
|
|
|
8
|
|
|
|
3
|
|
|
|
|
148
|
|
|
27
|
3
|
|
|
3
|
|
4509
|
use LWP::UserAgent qw(); |
|
|
3
|
|
|
|
|
184929
|
|
|
|
3
|
|
|
|
|
78
|
|
|
28
|
3
|
|
|
3
|
|
33
|
use HTTP::Request qw(); |
|
|
3
|
|
|
|
|
4
|
|
|
|
3
|
|
|
|
|
63
|
|
|
29
|
3
|
|
|
3
|
|
17
|
use Carp qw(carp croak); |
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
235
|
|
|
30
|
3
|
|
|
3
|
|
20
|
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); |
|
|
3
|
|
|
|
|
4
|
|
|
|
3
|
|
|
|
|
3475
|
|
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
$VERSION = 1.19 || sprintf('%d.%02d', q$Revision$ =~ /(\d+)/g); |
|
33
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
|
34
|
|
|
|
|
|
|
@EXPORT = (); |
|
35
|
|
|
|
|
|
|
@EXPORT_OK = qw(&get_strip &strip_url &mirror_strip); |
|
36
|
|
|
|
|
|
|
%EXPORT_TAGS = ( all => \@EXPORT_OK ); |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub mirror_strip { |
|
39
|
2
|
|
|
2
|
1
|
5
|
my $filename = shift; |
|
40
|
2
|
|
33
|
|
|
17
|
my $url = shift || strip_url(); |
|
41
|
|
|
|
|
|
|
|
|
42
|
2
|
|
|
|
|
11
|
my $blob = get_strip($url); |
|
43
|
2
|
50
|
|
|
|
25
|
return undef if !defined($blob); |
|
44
|
|
|
|
|
|
|
|
|
45
|
0
|
0
|
0
|
|
|
0
|
if ((!defined($filename) || !length($filename)) && defined($url)) { |
|
|
|
|
0
|
|
|
|
|
|
46
|
0
|
|
|
|
|
0
|
($filename = $url) =~ s#.*/##; |
|
47
|
|
|
|
|
|
|
} |
|
48
|
0
|
|
|
|
|
0
|
my $ext = _image_format($blob); |
|
49
|
0
|
|
|
|
|
0
|
$filename =~ s/(\.(jpe?g|gif))?$/.$ext/i; |
|
50
|
|
|
|
|
|
|
|
|
51
|
0
|
0
|
|
|
|
0
|
open(FH,">$filename") || |
|
52
|
|
|
|
|
|
|
croak "Unable to open file handle FH for file '$filename': $!"; |
|
53
|
0
|
|
|
|
|
0
|
binmode FH; |
|
54
|
0
|
|
|
|
|
0
|
print FH $blob; |
|
55
|
0
|
0
|
|
|
|
0
|
close(FH) || |
|
56
|
|
|
|
|
|
|
carp "Unable to close file handle FH for file '$filename': $!"; |
|
57
|
0
|
|
|
|
|
0
|
return $filename; |
|
58
|
|
|
|
|
|
|
} |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub get_strip { |
|
61
|
6
|
|
100
|
6
|
1
|
45
|
my $url = shift || strip_url() || ''; |
|
62
|
|
|
|
|
|
|
|
|
63
|
6
|
100
|
|
|
|
32
|
if ($url =~ /^(?:dilbert)?(\d+(\.(jpg|gif))?)$/i) { |
|
64
|
1
|
|
|
|
|
5
|
$url = "http://www.dilbert.com/comics/dilbert/". |
|
65
|
|
|
|
|
|
|
"archive/images/dilbert$1"; |
|
66
|
1
|
50
|
|
|
|
9
|
$url .= '.gif' unless $url =~ /\.(jpg|gif)$/i; |
|
67
|
|
|
|
|
|
|
} |
|
68
|
|
|
|
|
|
|
|
|
69
|
6
|
|
|
|
|
23
|
my $ua = _new_agent(); |
|
70
|
6
|
|
|
|
|
47
|
my $req = HTTP::Request->new(GET => $url); |
|
71
|
6
|
|
|
|
|
759
|
$req->referer('http://www.dilbert.com/'); |
|
72
|
6
|
|
|
|
|
310
|
my $response = $ua->request($req); |
|
73
|
|
|
|
|
|
|
|
|
74
|
6
|
|
|
|
|
492109
|
my $status; |
|
75
|
6
|
100
|
|
|
|
23
|
unless ($response->is_success) { |
|
76
|
5
|
|
|
|
|
58
|
$status = $response->status_line; |
|
77
|
5
|
50
|
|
|
|
85
|
unless ($url =~ s/\.gif$/.jpg/i) { $url =~ s/\.jpg$/.gif/i; } |
|
|
5
|
|
|
|
|
12
|
|
|
78
|
5
|
|
|
|
|
21
|
$req = HTTP::Request->new(GET => $url); |
|
79
|
5
|
|
|
|
|
372
|
$req->referer('http://www.dilbert.com/'); |
|
80
|
5
|
|
|
|
|
195
|
$response = $ua->request($req); |
|
81
|
|
|
|
|
|
|
} |
|
82
|
|
|
|
|
|
|
|
|
83
|
6
|
100
|
|
|
|
6488
|
if ($response->is_success) { |
|
|
|
50
|
|
|
|
|
|
|
84
|
1
|
50
|
|
|
|
11
|
unless (_image_format($response->content)) { |
|
85
|
1
|
50
|
|
|
|
10
|
carp('Unrecognised image format') if $^W; |
|
86
|
1
|
|
|
|
|
170
|
return undef; |
|
87
|
|
|
|
|
|
|
} |
|
88
|
0
|
|
|
|
|
0
|
return $response->content; |
|
89
|
|
|
|
|
|
|
} elsif ($^W) { |
|
90
|
0
|
|
|
|
|
0
|
carp($status); |
|
91
|
|
|
|
|
|
|
} |
|
92
|
5
|
|
|
|
|
174
|
return undef; |
|
93
|
|
|
|
|
|
|
} |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub strip_url { |
|
96
|
9
|
|
|
9
|
1
|
83
|
my $ua = _new_agent(); |
|
97
|
|
|
|
|
|
|
|
|
98
|
9
|
|
|
|
|
38
|
my $response = $ua->get('http://www.dilbert.com'); |
|
99
|
9
|
50
|
|
|
|
5773445
|
if ($response->is_success) { |
|
|
|
0
|
|
|
|
|
|
|
100
|
9
|
|
|
|
|
163
|
my $html = $response->content; |
|
101
|
9
|
50
|
|
|
|
3146
|
if ($html =~ m#
|
|
102
|
|
|
|
|
|
|
/comics/dilbert/archive/images/dilbert.+?)"#imsx) { |
|
103
|
0
|
|
|
|
|
0
|
my $url = $1; |
|
104
|
0
|
0
|
|
|
|
0
|
$url = "http://www.dilbert.com$1" unless $url =~ /^https?:\/\//i; |
|
105
|
0
|
|
|
|
|
0
|
return $url; |
|
106
|
|
|
|
|
|
|
} |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
} elsif ($^W) { |
|
109
|
0
|
|
|
|
|
0
|
carp($response->status_line); |
|
110
|
|
|
|
|
|
|
} |
|
111
|
|
|
|
|
|
|
|
|
112
|
9
|
|
|
|
|
565
|
return undef; |
|
113
|
|
|
|
|
|
|
} |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub _image_format { |
|
116
|
1
|
|
50
|
1
|
|
37
|
local $_ = shift || ''; |
|
117
|
1
|
50
|
|
|
|
7
|
return 'gif' if /^GIF8[79]a/; |
|
118
|
1
|
50
|
|
|
|
5
|
return 'jpg' if /^\xFF\xD8/; |
|
119
|
1
|
50
|
|
|
|
6
|
return 'png' if /^\x89PNG\x0d\x0a\x1a\x0a/; |
|
120
|
1
|
|
|
|
|
6
|
return undef; |
|
121
|
|
|
|
|
|
|
} |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub _new_agent { |
|
124
|
15
|
|
|
15
|
|
166
|
my $ua = LWP::UserAgent->new( |
|
125
|
|
|
|
|
|
|
agent => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.8) '. |
|
126
|
|
|
|
|
|
|
'Gecko/20050718 Firefox/1.0.4 (Debian package 1.0.4-2sarge1)', |
|
127
|
|
|
|
|
|
|
timeout => 20 |
|
128
|
|
|
|
|
|
|
); |
|
129
|
15
|
|
|
|
|
7643
|
$ua->env_proxy; |
|
130
|
15
|
|
|
|
|
18954
|
$ua->max_size(1024*250); |
|
131
|
15
|
|
|
|
|
207
|
return $ua; |
|
132
|
|
|
|
|
|
|
} |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
1; |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=pod |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head1 NAME |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
WWW::Dilbert - Retrieve Dilbert of the day comic strip images |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
use WWW::Dilbert qw(get_strip mirror_strip strip_url); |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
# Get the URL for todays strip |
|
147
|
|
|
|
|
|
|
my $image_url = strip_url(); |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
# Get todays strip |
|
150
|
|
|
|
|
|
|
my $image_blob = get_strip(); |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
# Get a specific strip by specifying the ID |
|
153
|
|
|
|
|
|
|
my $ethical_garbage_man = get_strip("2666040051128"); |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
# Write todays strip to local_filename.gif on disk |
|
156
|
|
|
|
|
|
|
my $filename_written = mirror_strip("local_filename.gif"); |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
# Write a specific strip to mystrip.gif on disk |
|
159
|
|
|
|
|
|
|
my $filename_written = mirror_strip("mystrip.gif","2666040051128"); |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
This module will download the latest Dilbert of the Day cartoon strip |
|
164
|
|
|
|
|
|
|
from the Dilbert website and return a binary blob of the image, or |
|
165
|
|
|
|
|
|
|
write it to disk. |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head1 EXPORTS |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
The following functions can be exported with the C<:all> export |
|
170
|
|
|
|
|
|
|
tag, or individually as is show in the above example. |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head2 strip_url |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
# Return todays strip URL |
|
175
|
|
|
|
|
|
|
my $url = strip_url(); |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
# Return the strip matching ID 200512287225 |
|
178
|
|
|
|
|
|
|
$url = strip_url("200512287225"); |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
Accepts an optional argument strip ID argument. |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head2 get_strip |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
# Get todays comic strip image |
|
185
|
|
|
|
|
|
|
my $image_blob = get_strip(); |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
Accepts an optional argument strip ID argument. |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=head2 mirror_strip |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
# Write todays comic strip to "mystrip.gif" on disk |
|
192
|
|
|
|
|
|
|
my $filename_written = mirror_strip("mystrip.gif"); |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
Accepts two optional arguments. The first is the filename that |
|
195
|
|
|
|
|
|
|
the comic strip should be written to on disk. The second specifies |
|
196
|
|
|
|
|
|
|
the strip ID. |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
Returns the name of the file that was written to disk. |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=head1 VERSION |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
$Id: Dilbert.pm,v 1.19 2006/01/12 22:30:11 nicolaw Exp $ |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=head1 AUTHOR |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
Nicola Worthington |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
L |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=head1 COPYRIGHT |
|
211
|
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
Copyright 2004,2005,2006 Nicola Worthington. |
|
213
|
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
This software is licensed under The Apache Software License, Version 2.0. |
|
215
|
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
L |
|
217
|
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=cut |
|
219
|
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
|