File Coverage

blib/lib/WWW/Favicon.pm
Criterion Covered Total %
statement 46 47 97.8
branch 4 6 66.6
condition n/a
subroutine 9 10 90.0
pod 3 3 100.0
total 62 66 93.9


line stmt bran cond sub pod time code
1             package WWW::Favicon;
2 3     3   1334 use strict;
  3         6  
  3         99  
3 3     3   17 use warnings;
  3         4  
  3         105  
4 3     3   15 use base qw/Class::Accessor::Fast Exporter/;
  3         6  
  3         1100  
5              
6 3     3   3824 use Carp;
  3         8  
  3         230  
7 3     3   3215 use LWP::UserAgent;
  3         153478  
  3         151  
8 3     3   9709 use HTML::TreeBuilder;
  3         108279  
  3         42  
9 3     3   3519 use HTML::ResolveLink;
  3         2775  
  3         1227  
10              
11             our $VERSION = '0.03001';
12             our @EXPORT_OK = qw/detect_favicon_url/;
13              
14             __PACKAGE__->mk_accessors(qw/ua/);
15              
16             sub new {
17 1     1 1 14 my $self = shift->SUPER::new(@_);
18              
19 1         11 $self->{ua} = do {
20 1         10 my $ua = LWP::UserAgent->new;
21 1         2788 $ua->timeout(10);
22 1         17 $ua->max_size(1024*1024);
23 1         11 $ua->env_proxy;
24 1         3978 $ua;
25             };
26              
27 1         6 $self;
28             }
29              
30             sub detect_favicon_url($) {
31 0     0 1 0 __PACKAGE__->detect(shift);
32             }
33              
34             sub detect {
35 7     7 1 38 my ($self, $url) = @_;
36 7 50       30 $self = $self->new unless ref $self;
37              
38 7         45 my $res = $self->ua->get($url);
39 7 50       1048 croak 'request failed: ' . $res->status_line unless $res->is_success;
40              
41 7         87 my $resolver = HTML::ResolveLink->new( base => $res->base );
42 7         13596 my $html = $resolver->resolve( $res->content );
43              
44 7         2353 my $tree = HTML::TreeBuilder->new;
45 7         1635 $tree->parse($html);
46 7         3037 $tree->eof;
47              
48 7         996 my ($icon_url) = grep {$_} map { $_->attr('href') } $tree->look_down(
  6         80  
  6         842  
49             _tag => 'link',
50             rel => qr/^(shortcut )?icon$/i,
51             );
52              
53 7 100       94 unless ($icon_url) {
54 1         5 $icon_url = $res->base->clone;
55 1         290 $icon_url->path('/favicon.ico');
56             }
57              
58 7         101 $tree->delete;
59              
60 7         579 "$icon_url";
61             }
62              
63             =head1 NAME
64              
65             WWW::Favicon - perl module to detect favicon url
66              
67             =head1 SYNOPSIS
68              
69             use WWW::Favicon qw/detect_favicon_url/;
70             my $favicon_url = detect_favicon_url('http://example.com/');
71            
72             # or OO way
73             use WWW::Favicon;
74             my $favicon = WWW::Favicon->new;
75             my $favicon_url = $favicon->detect('http://example.com/');
76              
77             =head1 DESCRIPTION
78              
79             This module provide simple interface to detect favicon url of specified url.
80              
81             =head1 METHODS
82              
83             =head2 new
84              
85             Create new WWW::Favicon object.
86              
87             =head2 detect($url)
88              
89             Detect favicon url of $url.
90              
91             =head1 EXPORT FUNCTIONS
92              
93             =head2 detect_favicon_url($url)
94              
95             Same as $self->detect described above.
96              
97             =head1 AUTHOR
98              
99             Daisuke Murase
100              
101             =head1 COPYRIGHT
102              
103             This program is free software; you can redistribute
104             it and/or modify it under the same terms as Perl itself.
105              
106             The full text of the license can be found in the
107             LICENSE file included with this module.
108              
109             =cut
110              
111             1;