Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Like many, I use a password manager for all my web password. KeePass allows you to add custom icons to each entry, so I thought I would use the favicon that comes with each site. The problem is that, at least in FireFox, the icons are saved in the bookmarks file, base-64 encoded, so there is no easy way to get them. Also KeePass lists the names of the icon files, but doesn't seem to have a preview, so each icon needs to be properly named.

I use the code below to extract the icons, eliminate duplicates, and save each one to a properly named file in the current directory.

I apologize for using XML::Twig (again!), but that's the module I know best.

To get all the files, export your bookmarks in an empty directory, then run the code there.

#!/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; }

In reply to Extracting icons from a bookmark file by mirod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-04-23 10:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found