summaryrefslogtreecommitdiff
path: root/lib/Bookmarks/Utils.pm
blob: b9fec9c62e4a7000398ddc5377847c394a907a7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package Bookmarks::Utils; 
use strict;
use warnings;
use LWP::UserAgent;
use URI::URL;
 
my $ua=LWP::UserAgent->new();
 
sub check_link {
    my ($url)=@_;
    return 1 if $url!~/^http:/;
    #my ($type,$length,$update,$expires,$server)=head($url); 
    #return defined $type; 
    return $ua->head($url)->is_success();
}
 
sub get_site_icon {
    my ($url)=@_;
 
    return if $url!~/^http:/;
 
    print "Provo nella pagina\n";
    my $res=$ua->get($url);
    return unless $res->is_success();
    my $page=$res->decoded_content();
    my ($favicon)= grep {defined $_ and $_ ne ''}
        ($page =~
             m{<link
               \s+
               (?:
               rel="shortcuticon"
               \s+
               href="(.*?)"
           )|(?:
               href="(.*?)"
               \s+
               rel="shortcuticon"
           )}smx);
    if (!$favicon) {
        $favicon='/favicon.ico';
    }
    print "Trovato: '$favicon'";
    $favicon=URI::URL->new($favicon,$url)->abs->canonical->as_string;
    print ", ovvero '$favicon'\n";
    $res=$ua->get($favicon);
 
    if ($res->is_success() and $res->header('Content-type') !~ /^text/) {
        return ($res->decoded_content(),$res->header('Content-type'));
    }
    else {
        return;
    }
}
 
1;