| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WebService::HtmlKitCom::FavIconFromImage; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 173632 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 24 |  | 
| 4 | 1 |  |  | 1 |  | 4 | use strict; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 30 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | our $VERSION = '1.001001'; # VERSION | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 1 |  |  | 1 |  | 3 | use Carp; | 
|  | 1 |  |  |  |  | 0 |  | 
|  | 1 |  |  |  |  | 39 |  | 
| 9 | 1 |  |  | 1 |  | 4 | use WWW::Mechanize; | 
|  | 1 |  |  |  |  | 0 |  | 
|  | 1 |  |  |  |  | 13 |  | 
| 10 | 1 |  |  | 1 |  | 3 | use Devel::TakeHashArgs; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 45 |  | 
| 11 | 1 |  |  | 1 |  | 4 | use base 'Class::Accessor::Grouped'; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 440 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | __PACKAGE__->mk_group_accessors( simple => qw( | 
| 14 |  |  |  |  |  |  | error | 
| 15 |  |  |  |  |  |  | mech | 
| 16 |  |  |  |  |  |  | response | 
| 17 |  |  |  |  |  |  | )); | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | sub new { | 
| 21 | 1 |  |  | 1 | 1 | 213 | my $self = bless {}, shift; | 
| 22 |  |  |  |  |  |  |  | 
| 23 | 1 | 50 |  |  |  | 14 | get_args_as_hash( \@_, \ my %args, { timeout => 180 } ) | 
| 24 |  |  |  |  |  |  | or croak $@; | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 1 |  | 33 |  |  | 46 | $args{mech} ||= WWW::Mechanize->new( | 
| 27 |  |  |  |  |  |  | autocheck => 0, | 
| 28 |  |  |  |  |  |  | timeout => $args{timeout}, | 
| 29 |  |  |  |  |  |  | agent   => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1.12)' | 
| 30 |  |  |  |  |  |  | .' Gecko/20080207 Ubuntu/7.10 (gutsy) Firefox/2.0.0.12', | 
| 31 |  |  |  |  |  |  | ); | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 1 |  |  |  |  | 14135 | $self->mech( $args{mech} ); | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 1 |  |  |  |  | 188 | return $self; | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | sub favicon { | 
| 39 | 1 |  |  | 1 | 1 | 1239 | my $self = shift; | 
| 40 |  |  |  |  |  |  |  | 
| 41 | 1 |  |  |  |  | 5 | $self->$_(undef) for qw(error response); | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 1 |  |  |  |  | 257 | my $image = shift; | 
| 44 | 1 | 50 |  |  |  | 6 | get_args_as_hash( \@_, \ my %args, { # also used: `file` | 
| 45 |  |  |  |  |  |  | image   => $image, | 
| 46 |  |  |  |  |  |  | }, | 
| 47 |  |  |  |  |  |  | ) or croak $@; | 
| 48 |  |  |  |  |  |  |  | 
| 49 | 1 | 50 |  |  |  | 31 | -e $args{image} | 
| 50 |  |  |  |  |  |  | or return $self->_set_error("File `$args{image}` does not exist"); | 
| 51 |  |  |  |  |  |  |  | 
| 52 | 1 |  |  |  |  | 3 | my $mech = $self->mech; | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 1 | 50 |  |  |  | 4 | $mech->get('http://www.html-kit.com/favicon/')->is_success | 
| 55 |  |  |  |  |  |  | or return $self->_set_error( $mech, 'net' ); | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 1 | 50 |  |  |  | 1045640 | $mech->form_number(1) | 
| 58 |  |  |  |  |  |  | or return $self->_set_error('Failed to find favicon form'); | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 1 |  |  |  |  | 5966 | $mech->set_visible( | 
| 61 |  |  |  |  |  |  | $args{image}, | 
| 62 |  |  |  |  |  |  | ); | 
| 63 |  |  |  |  |  |  |  | 
| 64 | 1 | 50 |  |  |  | 36 | $mech->click->is_success | 
| 65 |  |  |  |  |  |  | or return $self->_set_error( $mech, 'net' ); | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | #         use Data::Dumper; | 
| 68 |  |  |  |  |  |  | #     print $mech->res->decoded_content; | 
| 69 |  |  |  |  |  |  | #     exit; | 
| 70 | 1 | 50 |  |  |  | 3297932 | my $response = $mech->follow_link( | 
| 71 |  |  |  |  |  |  | url_regex => qr|^\Qhttp://favicon.htmlkit.com/favicon/download/| | 
| 72 |  |  |  |  |  |  | ) or return $self->_set_error( | 
| 73 |  |  |  |  |  |  | 'Failed to create favicon. Check your args' | 
| 74 |  |  |  |  |  |  | ); | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 1 | 50 |  |  |  | 184306 | $response->is_success | 
| 77 |  |  |  |  |  |  | or return $self->_set_error( $mech, 'net' ); | 
| 78 |  |  |  |  |  |  |  | 
| 79 | 1 | 50 |  |  |  | 8 | if ( $args{file} ) { | 
| 80 | 0 | 0 |  |  |  | 0 | open my $fh, '>', $args{file} | 
| 81 |  |  |  |  |  |  | or return $self->_set_error( | 
| 82 |  |  |  |  |  |  | "Failed to open `$args{file}` for writing ($!)" | 
| 83 |  |  |  |  |  |  | ); | 
| 84 | 0 |  |  |  |  | 0 | binmode $fh; | 
| 85 | 0 |  |  |  |  | 0 | print $fh $response->content; | 
| 86 | 0 |  |  |  |  | 0 | close $fh; | 
| 87 |  |  |  |  |  |  | } | 
| 88 | 1 |  |  |  |  | 12 | return $self->response($response); | 
| 89 |  |  |  |  |  |  | } | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | sub _set_error { | 
| 92 | 0 |  |  | 0 |  |  | my ( $self, $mech_or_message, $type ) = @_; | 
| 93 | 0 | 0 |  |  |  |  | if ( $type ) { | 
| 94 | 0 |  |  |  |  |  | $self->error( | 
| 95 |  |  |  |  |  |  | 'Network error: ' . $mech_or_message->res->status_line | 
| 96 |  |  |  |  |  |  | ); | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  | else { | 
| 99 | 0 |  |  |  |  |  | $self->error( $mech_or_message ); | 
| 100 |  |  |  |  |  |  | } | 
| 101 | 0 |  |  |  |  |  | return; | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | 1; | 
| 106 |  |  |  |  |  |  | __END__ |