box.matto.nl
Enjoying Open Source Software

Perl script to fetch bookmarks from Linkding REST API

Linkding is a awesome self-hosted bookmark manager.

I comes with a REST API, so you can add some fun stuff.

Here follows a small Perl script that uses the API to get the bookmarks that have been tagged with a certain tag.

Perl REST Client module and Perl JSON module

The script uses two Perl modules:

  • REST::Client: To talk to the Linkding
  • JSON to decode the JSON encoded data from the Linkding REST API

Small Perl script

I have put the part which queries the API, in a separate sub routine, so that it is easy to paste it in other Perl scripts.

I created the Perl script on a FreeBSD machine, if you use another operating system, you probably have to adopt the she-bang line.

#!/usr/local/bin/perl 

use strict;
use warnings;
use REST::Client;
use JSON;

# copy the token from the Settings page here:
my $token = 'abcdefghijklmnopqrstuvwxyz82cd98ede63036';

# tag for which to retreive the bookmarks
my $select_tag="my_special_tag";

# your Linkding host
my $linkding_host='http://192.168.0.105:9090';

my $client = REST::Client->new();
$client->setHost($linkding_host);
$client->addHeader ('Authorization' , "Token $token" );
&get_bookmarks( $select_tag );

sub get_bookmarks()
{
    my $select_tag=$_[0];

    # search for the tag, and override default
    # limit of 100 bookmark
    # %23 = '#'
    $client->GET("/api/bookmarks/?q=%23$select_tag&limit=999" );
    my $response = $client->responseContent();

    # sort the bookmarks from newest to oldest
    my @bookmarks=sort {$b->{date_modified} cmp $a->{date_modified}}
        @{decode_json($response)->{results}};

    foreach my $bookmark (@bookmarks)
    {
        # remove newlines from title field
        $bookmark->{title} =~ s/[\r\n]+//g;

        # if a title is missing, use the URL for that
        if ( $bookmark->{title} eq "" ) 
        {
            $bookmark->{title} = $bookmark->{url}; 
        }

        # do something with the retreived bookmarks
        print $bookmark->{url} . "\t" . $bookmark->{title} . "\n";
    }
}

Adopt to your needs and have fun!

Tags:

⇽ Tag and kill several Tmux windows Script to convert OPML file to GNU recutils database ⇾