File Coverage

blib/lib/WWW/ImagebinCa/Retrieve.pm
Criterion Covered Total %
statement 49 91 53.8
branch 7 36 19.4
condition 2 20 10.0
subroutine 9 10 90.0
pod 2 2 100.0
total 69 159 43.4


line stmt bran cond sub pod time code
1             package WWW::ImagebinCa::Retrieve;
2              
3 2     2   136270 use warnings;
  2         5  
  2         53  
4 2     2   10 use strict;
  2         3  
  2         75  
5              
6             our $VERSION = '0.01';
7              
8 2     2   8 use Carp;
  2         7  
  2         137  
9 2     2   877 use URI;
  2         6627  
  2         44  
10 2     2   969 use LWP::UserAgent;
  2         39414  
  2         67  
11             require File::Spec;
12 2     2   789 use HTML::TokeParser::Simple;
  2         28319  
  2         57  
13 2     2   13 use base qw(Class::Data::Accessor);
  2         4  
  2         1883  
14              
15             __PACKAGE__->mk_classaccessors( qw(
16             page_id image_uri page_uri description error
17             full_info what where do_download_image
18             )
19             );
20              
21             sub new {
22 2     2 1 341 my $class = shift;
23 2 50       9 croak "Must have even number of arguments to new()"
24             if @_ & 1;
25 2         9 my %args = @_;
26 2         18 $args{ +lc } = delete $args{ $_ } for keys %args;
27              
28 2   50     9 $args{timeout} ||= 30;
29              
30 2   33     34 $args{ua} ||= LWP::UserAgent->new(
31             timeout => $args{timeout},
32             agent => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US;'
33             . ' rv:1.8.1.12) Gecko/20080207 Ubuntu/7.10 (gutsy)'
34             . ' Firefox/2.0.0.12',
35             );
36              
37 2         6085 return bless \%args, $class;
38             }
39              
40             sub retrieve {
41 2     2 1 1173 my $self = shift;
42 2         3 my %args;
43 2 100       9 if ( @_ == 1 ) {
44 1         4 $args{what} = shift;
45             }
46             else {
47 1 50       6 croak "Must have even number or only 1 argument to retrieve()"
48             if @_ & 1;
49 1         4 %args = @_;
50 1         7 $args{ +lc } = delete $args{ $_ } for keys %args;
51             }
52 2         7 for (qw(page_id image_uri where page_uri description error full_info)){
53 14         109 $self->$_( undef );
54             }
55              
56 2 50       18 unless ( defined $args{do_download_image} ) {
57 2         15 $args{do_download_image} = 1;
58             }
59            
60 2 50       9 unless ( defined $args{what} ) {
61 0         0 $self->error('Undefined page ID was specified');
62 0         0 return;
63             }
64            
65 2         8 $self->what( $args{what} );
66 2         13 $args{what} =~ s{http://imagebin.ca/view/(\S+?).html}{$1}i;
67 2         8 $self->page_id( $args{what} );
68              
69 2         16 $self->do_download_image( $args{do_download_image} );
70              
71 2         23 my $page_uri = URI->new("http://imagebin.ca/view/$args{what}.html");
72 2         16988 my $response = $self->{ua}->get($page_uri);
73 2 50       650538 if ( $response->is_success ) {
74 0         0 $self->page_uri($page_uri);
75              
76 0         0 my $full_info_ref
77             = $self->_parse_response( $response->content, \%args );
78              
79 0         0 return $self->full_info( $full_info_ref );
80             }
81             else {
82 2         33 $self->error($response->status_line);
83 2         84 return;
84             }
85             }
86              
87             sub _parse_response {
88 0     0     my ( $self, $content, $args_ref ) = @_;
89              
90 0           my $parser = HTML::TokeParser::Simple->new( \$content );
91              
92 0           my ( $description, $image_uri );
93 0           my $get_description = 0;
94 0           while ( my $token = $parser->get_token ) {
95 0 0 0       if ( $token->is_start_tag('img')
    0 0        
    0 0        
      0        
      0        
96             and $token->get_attr('id')
97             and $token->get_attr('id') eq 'theimg'
98             ) {
99 0           $image_uri = URI->new($token->get_attr('src'));
100 0           $get_description = 1;
101             }
102             elsif ( $get_description == 1
103             and $token->is_start_tag('div')
104             and defined $token->get_attr('style')
105             ) {
106 0 0         if ( $token->get_attr('style') =~ /background/i ) {
107 0           $get_description = 2;
108             }
109             else {
110 0           last;
111             }
112             }
113             elsif ( $get_description == 2 and $token->is_text ) {
114 0           $description = $token->as_is;
115 0           last;
116             }
117             }
118 0 0         unless ( defined $image_uri ) {
119 0           $self->error(q|This page ID doesn't seem to exist|);
120 0           return;
121             }
122              
123 0 0         unless ( defined $description ) {
124 0           $description = 'N/A';
125             }
126            
127 0           $self->image_uri($image_uri);
128 0           $self->description($description);
129              
130 0 0         if ( $args_ref->{do_download_image} ) {
131 0           my $image_uri_filename = ($image_uri->path_segments)[-1];
132 0           my ( $extension ) = $image_uri_filename =~ /([.][^.]+$)/;
133              
134 0           my $save_as_where_file;
135 0 0         if ( defined $args_ref->{save_as} ) {
136 0           $save_as_where_file = File::Spec->catfile(
137             $args_ref->{where},
138             $args_ref->{save_as} . $extension
139             );
140             }
141              
142 0           my $where_file = File::Spec->catfile(
143             $args_ref->{where},
144             $image_uri_filename,
145             );
146              
147 0           my $local_name ;
148 0 0         if ( defined $args_ref->{save_as} ) {
149 0 0         $local_name = defined $args_ref->{where}
150             ? $save_as_where_file
151             : $args_ref->{save_as} . $extension;
152             }
153             else {
154 0 0         $local_name = defined $args_ref->{where}
155             ? $where_file
156             : $image_uri_filename;
157             }
158              
159 0           my $response = $self->{ua}->mirror( $image_uri, $local_name );
160 0 0         if ( $response->is_success ) {
161 0           $args_ref->{where} = $local_name;
162             }
163             else {
164 0           $self->error(
165             'Failed to download image: ' . $response->status_line
166             );
167 0           return;
168             }
169             }
170 0           return $self->full_info( {
171             page_id => $self->page_id,
172             page_uri => $self->page_uri,
173             image_uri => $image_uri,
174             description => $description,
175             where => $self->where( $args_ref->{where} ),
176             what => $self->what,
177             }
178             );
179             }
180              
181              
182             1;
183              
184             __END__