compare_bookmarks.pl


Menlo | FiraCode
#!/usr/bin/env perl
use Modern::Perl '2020';
use open qw(:std :utf8);
use Mojo::DOM58;
use Path::Tiny;
use Set::Tiny 'set';

# get two most recent bookmarks
my ( $oldp, $newp );
{
    my @paths = path('bookmarks/')->children(qr/\.html$/);
    @paths = sort @paths;
    $oldp  = $paths[ -2 ];
    $newp  = $paths[ -1 ];
}

my $data = {};
foreach my $file ( [ 'o', $oldp ], [ 'n', $newp ] ) {
    my ( $type, $path ) = @$file;
    my $html = path($path)->slurp_utf8();
    my $dom  = Mojo::DOM58->new($html);
    $dom->at('ul.bookmarks')->children('li')->map( sub {
        my $bookmark_id = $_->attr('id');
        if ( my $heading = $_->at('h4.heading') ) {
            $data->{$type}->{$bookmark_id} = $heading->all_text();
        }
    } );
}

my $oldk = set( keys %{ $data->{'o'} } );
my $newk = set( keys %{ $data->{'n'} } );

my $dele = $oldk->difference($newk);
my $plus = $newk->difference($oldk);

my @r = sort map { $data->{'o'}->{$_}; } $dele->members();
my @a = sort map { $data->{'n'}->{$_}; } $plus->members();

say "New bookmarks:";
say "\t" . join( "\n\t" => @a );
print "\n";
say "Deleted bookmarks:";
say "\t" . join( "\n\t" => @r );