blib/lib/WWW/Pastebin/PastebinCom/Retrieve.pm

filter: Statementstmt 94.2% 65 / 69 Branchbran 71.8% 23 / 32 Conditioncond 74.3% 29 / 39 MC/DCmcdc 54.5% 12 / 22 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 80.9% 140 / 173 CC 32 SCAR 25.2
CC
32
Cov
84%
_get_metadata
20.2
retrieve
9.3
_make_uri_and_id
4.6
CRAP 12.4
↑ Summary
1 package WWW::Pastebin::PastebinCom::Retrieve;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '0.004';
7  
8 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use HTML::TokeParser::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use base 'WWW::Pastebin::Base::Retrieve';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶sub retrieve {
Subroutine retrieve: called CC=7 SCAR=22.3
Pod documented
14 1 my $self = shift;
15 1 my $id = shift;
16  
17 1 $self->$_(undef) for qw(error uri id results);
18  
19 1 ▶ return $self->_set_error('Missing or empty paste ID or URI')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id and length $id 0 1 1
Condition33%1/3
defined $id and length $id
!ll&&!rl&&r
001
MC/DC0%0/2
defined $id and length $id
defined $id length $id
Truth table33%1/3
A and B
A= defined $idB= length $id
ABresult
0X0
100
111
20 unless defined $id and length $id;
21  
22 1 ▶ ( my $uri, $id ) = $self->_make_uri_and_id( $id, @_ )
Branch: 1 branch
BranchTrueFalseTotal
(my $uri, $id) = $self->_make_uri_and_id($id, @_) or return 1 0 1
23 or return;
24  
25 1 $self->id( $id );
26 1 $self->uri( $uri );
27  
28 # Fetch the raw paste body directly. pastebin.com no longer exposes the
29 # paste content in a form we can reliably scrape out of the HTML page, but
30 # it still serves the exact, unmodified paste text at /raw/<id>.
31 1 my $ua = $self->ua;
32 1 my $raw_uri = URI->new( 'https://pastebin.com/raw/' . $id );
33 1 my $response = $ua->get($raw_uri);
34  
35 1 ▶ if ( $response->is_success ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($response->is_success) { } 1 0 1
elsif ($response->code == 404) { } 0 0 0
36 1 return $self->_get_was_successful( $response->decoded_content );
37 }
38 elsif ( $response->code == 404 ) {
39 0 return $self->_set_error('This paste does not seem to exist');
40 }
41 else {
42 0 return $self->_set_error('Network error: ' . $response->status_line);
43 }
44 }
45  
46 1 ▶sub _make_uri_and_id {
Subroutine _make_uri_and_id: called CC=4 SCAR=15.2
47 1 my ( $self, $what ) = @_;
48  
49 1 my ( $private, $id ) = $what =~ m{
50 (?:https?://)?
51 (?:www\.)?
52 (.*?) # "private paste" subdomain
53 pastebin\.com/
54 (?:raw/)? # optional /raw/ prefix if a raw URI was passed in
55 (\w+) # paste ID
56 }xi;
57  
58 1 ▶ $id = $what
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id and length $id 0 1 1
Condition33%1/3
defined $id and length $id
!ll&&!rl&&r
001
MC/DC0%0/2
defined $id and length $id
defined $id length $id
Truth table33%1/3
A and B
A= defined $idB= length $id
ABresult
0X0
100
111
59 unless defined $id and length $id;
60  
61 1 ▶ $private = ''
Branch: 1 branch
BranchTrueFalseTotal
unless defined $private 0 1 1
62 unless defined $private;
63  
64 1 return ( URI->new("http://${private}pastebin.com/$id"), $id );
65 }
66  
67 1 ▶sub _get_was_successful {
Subroutine _get_was_successful: called CC=4 SCAR=14.8
68 1 my ( $self, $raw_content ) = @_;
69  
70 # The raw body IS the paste content -- store it verbatim.
71 1 $self->content($raw_content);
72  
73 1 my %data = ( content => $raw_content );
74  
75 # Best-effort scrape of the paste's metadata (name/posted_on/lang) from
76 # the human-facing HTML page. The paste content itself never depends on
77 # this succeeding, so any failure here degrades gracefully to 'N/A'.
78 1 @data{ qw(name posted_on lang) }
79 = $self->_get_metadata( $self->uri );
80  
81 1 for my $key ( qw(name posted_on lang) ) {
82 3 ▶ unless ( defined $data{$key} and length $data{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data{$key} and length $data{$key}) 0 3 3
Condition33%1/3
defined $data{$key} and length $data{$key}
!ll&&!rl&&r
003
MC/DC0%0/2
defined $data{$key} and length $data{$key}
defined $data{$key} length $data{$key}
Truth table33%1/3
A and B
A= defined $data{$key}B= length $data{$key}
ABresult
0X0
100
111
83 0 $data{$key} = 'N/A';
84 0 next;
85 }
86 3 decode_entities $data{$key};
87 3 $data{$key} =~ s/\240/ /g;
88 3 $data{$key} =~ s/^\s+|\s+$//g;
89 }
90  
91 1 return $self->results( \%data );
92 }
93  
94 1 ▶sub _get_metadata {
Subroutine _get_metadata: called CC=20 SCAR=30.0
95 1 my ( $self, $uri ) = @_;
96  
97 1 my $response = $self->ua->get($uri);
98 return
99 1 ▶ unless $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 0 1 1
100  
101 1 my $content = $response->decoded_content;
102 1 my $parser = HTML::TokeParser::Simple->new( \$content );
103  
104 1 my ( %meta, %nav );
105 1 ▶ while ( my $t = $parser->get_token ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $parser->get_token) 1077 1 1078
106 # Author name lives in an <h1> inside the paste's info bar.
107 1077 ▶ if ( $t->is_start_tag('h1') ) {
Branch: 7 branches
BranchTrueFalseTotal
if ($t->is_start_tag('h1')) { } 1 1076 1077
elsif ($nav{'get_name'} and $t->is_text) { } 1 1075 1076
elsif (not defined $meta{'posted_on'} and $t->is_start_tag('div') and defined $t->get_attr('class') and $t->get_attr('class') =~ /\bdate\b/) { } 1 1074 1075
elsif ($nav{'in_date'} and $t->is_start_tag('span')) { } 1 1073 1074
elsif ($nav{'get_date'} and $t->is_text) { } 1 1072 1073
elsif (not defined $meta{'lang'} and $t->is_start_tag('a') and defined $t->get_attr('href') and $t->get_attr('href') =~ m[^/archive/]) { } 1 1071 1072
elsif ($nav{'get_lang'} and $t->is_text) { } 1 2140 2141
108 1 $nav{get_name} = 1;
109 }
110 ▶ elsif ( $nav{get_name} and $t->is_text ) {
Condition66%2/3
$nav{"get_name"} and $t->is_text
!ll&&!rl&&r
107501
MC/DC50%1/2
$nav{"get_name"} and $t->is_text
$nav{"get_name"} $t->is_text
Truth table66%2/3
A and B
A= $nav{"get_name"}B= $t->is_text
ABresult
0X0
100
111
111 1 $meta{name} = $t->as_is;
112 1 $nav{get_name} = 0;
113 }
114  
115 # Post date lives in <div class="date"><span title="...">Mar 22nd,
116 # 2008</span>. Grab the first such span's text.
117 ▶ elsif ( not defined $meta{posted_on}
Condition100%9/9
not defined $meta{"posted_on"} and $t->is_start_tag("div") and defined $t->get_attr("class") and $t->get_attr("class") =~ /\bdate\b/
!ll&&!rl&&r
1052221
not defined $meta{"posted_on"} and $t->is_start_tag("div") and defined $t->get_attr("class")
!ll&&!rl&&r
1051123
not defined $meta{"posted_on"} and $t->is_start_tag("div")
!ll&&!rl&&r
84420724
MC/DC100%4/4
not defined $meta{"posted_on"} and $t->is_start_tag("div") and defined $t->get_attr("class") and $t->get_attr("class") =~ /\bdate\b/
not defined $meta{"posted_on"} $t->is_start_tag("div") defined $t->get_attr("class") $t->get_attr("class") =~ /\bdate\b/
Truth table100%5/5
not(A) and B and C and D
A= not defined $meta{"posted_on"}B= $t->is_start_tag("div")C= defined $t->get_attr("class")D= $t->get_attr("class") =~ /\bdate\b/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
118 and $t->is_start_tag('div')
119 and defined $t->get_attr('class')
120 and $t->get_attr('class') =~ /\bdate\b/
121 ) {
122 1 $nav{in_date} = 1;
123 }
124 ▶ elsif ( $nav{in_date} and $t->is_start_tag('span') ) {
Condition100%3/3
$nav{"in_date"} and $t->is_start_tag("span")
!ll&&!rl&&r
107121
MC/DC100%2/2
$nav{"in_date"} and $t->is_start_tag("span")
$nav{"in_date"} $t->is_start_tag("span")
Truth table100%3/3
A and B
A= $nav{"in_date"}B= $t->is_start_tag("span")
ABresult
0X0
100
111
125 1 $nav{get_date} = 1;
126 }
127 ▶ elsif ( $nav{get_date} and $t->is_text ) {
Condition66%2/3
$nav{"get_date"} and $t->is_text
!ll&&!rl&&r
107201
MC/DC50%1/2
$nav{"get_date"} and $t->is_text
$nav{"get_date"} $t->is_text
Truth table66%2/3
A and B
A= $nav{"get_date"}B= $t->is_text
ABresult
0X0
100
111
128 1 $meta{posted_on} = $t->as_is;
129 1 @nav{ qw(get_date in_date) } = (0, 0);
130 }
131  
132 # Language is the first link into the /archive/<lang> section.
133 ▶ elsif ( not defined $meta{lang}
Condition88%8/9
not defined $meta{"lang"} and $t->is_start_tag("a") and defined $t->get_attr("href") and $t->get_attr("href") =~ m[^/archive/]
!ll&&!rl&&r
106291
not defined $meta{"lang"} and $t->is_start_tag("a") and defined $t->get_attr("href")
!ll&&!rl&&r
1062010
not defined $meta{"lang"} and $t->is_start_tag("a")
!ll&&!rl&&r
78228010
MC/DC75%3/4
not defined $meta{"lang"} and $t->is_start_tag("a") and defined $t->get_attr("href") and $t->get_attr("href") =~ m[^/archive/]
not defined $meta{"lang"} $t->is_start_tag("a") defined $t->get_attr("href") $t->get_attr("href") =~ m[^/archive/]
Truth table80%4/5
not(A) and B and C and D
A= not defined $meta{"lang"}B= $t->is_start_tag("a")C= defined $t->get_attr("href")D= $t->get_attr("href") =~ m[^/archive/]
ABCDresult
0XXX0
10XX0
110X0
11100
11111
134 and $t->is_start_tag('a')
135 and defined $t->get_attr('href')
136 and $t->get_attr('href') =~ m{^/archive/}
137 ) {
138 1 $nav{get_lang} = 1;
139 }
140 ▶ elsif ( $nav{get_lang} and $t->is_text ) {
Condition66%2/3
$nav{"get_lang"} and $t->is_text
!ll&&!rl&&r
107001
MC/DC50%1/2
$nav{"get_lang"} and $t->is_text
$nav{"get_lang"} $t->is_text
Truth table66%2/3
A and B
A= $nav{"get_lang"}B= $t->is_text
ABresult
0X0
100
111
141 1 $meta{lang} = $t->as_is;
142 1 $nav{get_lang} = 0;
143 }
144 }
145  
146 1 return @meta{ qw(name posted_on lang) };
147 }
148  
149  
150 =head1 NAME
151  
152 WWW::Pastebin::PastebinCom::Retrieve - retrieve pastes from http://pastebin.com/ website
153  
154 =head1 SYNOPSIS
155  
156 use strict;
157 use warnings;
158  
159 use lib '../lib';
160 use WWW::Pastebin::PastebinCom::Retrieve;
161  
162 die "Usage: perl retrieve.pl <paste_ID_or_URI>\n"
163 unless @ARGV;
164  
165 my $Paste = shift;
166  
167 my $paster = WWW::Pastebin::PastebinCom::Retrieve->new;
168  
169 my $results_ref = $paster->retrieve( $Paste )
170 or die $paster->error;
171  
172 printf "Paste content is:\n%s\nPasted by %s on %s\n",
173 @$results_ref{ qw(content name posted_on) };
174  
175 =head1 DESCRIPTION
176  
177 The module provides interface to retrieve pastes from
178 L<http://pastebin.com/> website via Perl.
179  
180 =head1 CONSTRUCTOR
181  
182 =head2 C<new>
183  
184 my $paster = WWW::Pastebin::PastebinCom::Retrieve->new;
185  
186 my $paster = WWW::Pastebin::PastebinCom::Retrieve->new(
187 timeout => 10,
188 );
189  
190 my $paster = WWW::Pastebin::PastebinCom::Retrieve->new(
191 ua => LWP::UserAgent->new(
192 timeout => 10,
193 agent => 'PasterUA',
194 ),
195 );
196  
197 Constructs and returns a brand new juicy
198 WWW::Pastebin::PastebinCom::Retrieve
199 object. Takes two arguments, both are I<optional>. Possible arguments are
200 as follows:
201  
202 =head3 C<timeout>
203  
204 ->new( timeout => 10 );
205  
206 B<Optional>. Specifies the C<timeout> argument of L<LWP::UserAgent>'s
207 constructor, which is used for retrieving. B<Defaults to:> C<30> seconds.
208  
209 =head3 C<ua>
210  
211 ->new( ua => LWP::UserAgent->new( agent => 'Foos!' ) );
212  
213 B<Optional>. If the C<timeout> argument is not enough for your needs
214 of mutilating the L<LWP::UserAgent> object used for retrieving, feel free
215 to specify the C<ua> argument which takes an L<LWP::UserAgent> object
216 as a value. B<Note:> the C<timeout> argument to the constructor will
217 not do anything if you specify the C<ua> argument as well. B<Defaults to:>
218 plain boring default L<LWP::UserAgent> object with C<timeout> argument
219 set to whatever C<WWW::Pastebin::PastebinCom::Retrieve>'s C<timeout>
220 argument is
221 set to as well as C<agent> argument is set to mimic Firefox.
222  
223 =head1 METHODS
224  
225 =head2 C<retrieve>
226  
227 my $results_ref = $paster->retrieve('http://pastebin.com/f525c4cec')
228 or die $paster->error;
229  
230 my $results_ref = $paster->retrieve('f525c4cec')
231 or die $paster->error;
232  
233 Instructs the object to retrieve a paste specified in the argument. Takes
234 one mandatory argument which can be either a full URI to the paste you
235 want to retrieve or just its ID.
236 On failure returns either C<undef> or an empty list depending on the context
237 and the reason for the error will be available via C<error()> method.
238 On success returns a hashref with the following keys/values:
239  
240 $VAR1 = {
241 'lang' => 'Perl',
242 'posted_on' => 'Sat 22 Mar 16:07',
243 'content' => 'blah blah content of the paste',
244 'name' => 'Zoffix'
245 };
246  
247 =head3 content
248  
249 { 'content' => 'blah blah content of the paste', }
250  
251 The C<content> key will contain the actual content of the paste. See also
252 C<content()> method which is overloaded for this class.
253  
254 =head3 lang
255  
256 { 'lang' => 'Perl' }
257  
258 The C<lang> key will contain the (computer) language of the paste
259 (as specified by the person who pasted it)
260  
261 =head3 posted_on
262  
263 { 'posted_on' => 'Sat 22 Mar 16:07', }
264  
265 The C<posted_on> key will contain the date/time when the paste was created.
266  
267 =head3 name
268  
269 { 'name' => 'Zoffix' }
270  
271 The C<name> key will contain the name of the person who created the paste.
272  
273  
274 =head2 C<error>
275  
276 $paster->retrieve('http://pastebin.com/f525c4cec')
277 or die $paster->error;
278  
279 On failure C<retrieve()> returns either C<undef> or an empty list depending
280 on the context and the reason for the error will be available via C<error()>
281 method. Takes no arguments, returns an error message explaining the failure.
282  
283 =head2 C<id>
284  
285 my $paste_id = $paster->id;
286  
287 Must be called after a successful call to C<retrieve()>. Takes no arguments,
288 returns a paste ID number of the last retrieved paste irrelevant of whether
289 an ID or a URI was given to C<retrieve()>
290  
291 =head2 C<uri>
292  
293 my $paste_uri = $paster->uri;
294  
295 Must be called after a successful call to C<retrieve()>. Takes no arguments,
296 returns a L<URI> object with the URI pointing to the last retrieved paste
297 irrelevant of whether an ID or a URI was given to C<retrieve()>
298  
299 =head2 C<results>
300  
301 my $last_results_ref = $paster->results;
302  
303 Must be called after a successful call to C<retrieve()>. Takes no arguments,
304 returns the exact same hashref the last call to C<retrieve()> returned.
305 See C<retrieve()> method for more information.
306  
307 =head2 C<content>
308  
309 my $paste_content = $paster->content;
310  
311 print "Paste content is:\n$paster\n";
312  
313 Must be called after a successful call to C<retrieve()>. Takes no arguments,
314 returns the actual content of the paste. B<Note:> this method is overloaded
315 for this module for interpolation. Thus you can simply interpolate the
316 object in a string to get the contents of the paste.
317  
318 =head2 C<ua>
319  
320 my $old_LWP_UA_obj = $paster->ua;
321  
322 $paster->ua( LWP::UserAgent->new( timeout => 10, agent => 'foos' );
323  
324 Returns a currently used L<LWP::UserAgent> object used for retrieving
325 pastes. Takes one optional argument which must be an L<LWP::UserAgent>
326 object, and the object you specify will be used in any subsequent calls
327 to C<retrieve()>.
328  
329 =head1 SEE ALSO
330  
331 L<LWP::UserAgent>, L<URI>
332  
333 =head1 AUTHOR
334  
335 Zoffix Znet, C<< <zoffix at cpan.org> >>
336 (L<http://zoffix.com>, L<http://haslayout.net>)
337  
338 =head1 BUGS
339  
340 Please report any bugs or feature requests to C<bug-www-pastebin-pastebincom-retrieve at rt.cpan.org>, or through
341 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-Pastebin-PastebinCom-Retrieve>. I will be notified, and then you'll
342 automatically be notified of progress on your bug as I make changes.
343  
344 =head1 SUPPORT
345  
346 You can find documentation for this module with the perldoc command.
347  
348 perldoc WWW::Pastebin::PastebinCom::Retrieve
349  
350 You can also look for information at:
351  
352 =over 4
353  
354 =item * RT: CPAN's request tracker
355  
356 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-Pastebin-PastebinCom-Retrieve>
357  
358 =item * AnnoCPAN: Annotated CPAN documentation
359  
360 L<http://annocpan.org/dist/WWW-Pastebin-PastebinCom-Retrieve>
361  
362 =item * CPAN Ratings
363  
364 L<http://cpanratings.perl.org/d/WWW-Pastebin-PastebinCom-Retrieve>
365  
366 =item * Search CPAN
367  
368 L<http://search.cpan.org/dist/WWW-Pastebin-PastebinCom-Retrieve>
369  
370 =back
371  
372 =head1 COPYRIGHT & LICENSE
373  
374 Copyright 2008 Zoffix Znet, all rights reserved.
375  
376 This program is free software; you can redistribute it and/or modify it
377 under the same terms as Perl itself.
378  
379 =cut
380  
↑ Summary