#!/usr/bin/perl use strict; use warnings; use XML::Twig; use MIME::Base64; my $HEADER= "data:image/([^:]*);base64,"; # image header # if not listed here the type is used as extension (eg png, or jpeg) my %type2ext= ( 'x-icon' => 'ico'); my %seen; # used to avoid duplicates, both for sites and for icons XML::Twig->new( twig_handlers => { 'dt/a[@icon]' => \&save_icon }) ->parsefile_html( 'bookmarks.html'); sub save_icon { my( $t, $a)= @_; my $url= $a->att('href'); my $site= url2name( $url); return if( $seen{$site}); $seen{$site}=1; my $icon= $a->att( 'icon'); return unless $icon=~ s{^$HEADER}{}; # skip if icon is not base-64 encoded my $icon_type= $1; return if( $seen{$icon}); $seen{$icon}=1; my $ext= $type2ext{$icon_type} || $icon_type; my $file= join '.', $site, $ext; $icon= decode_base64( $icon); warn "adding icon for $site ($icon_type) to $file\n"; open( my $out, '>:raw', $file) or die "cannot create file $file: $!"; print {$out} $icon; } # takes a full url and returns a short version of the site name # eg http://http://perlmonks.org/?node=Newest%20Nodes => perlmonks sub url2name { my( $url)= @_; $url=~ s{^https?://}{}; # remove protocol $url=~ s{^www.}{}; # no need for the www. bit $url=~ s{/.*$}{}; # keep only the site name $url=~ s{\.[^.]*$}{}; # ermove the tld return $url; }