source: bookmarks/trunk/bkmk @ 84

Last change on this file since 84 was 83, checked in by peter, 9 years ago

issue #9: minor improvements to the bkmk script

  • loads the Bookmarks library correctly
  • tags are given as positional arguments on the command line instead of switches
  • the db filename defaults to the value of the environment variable $BKMK_DBNAME
  • Property svn:executable set to *
File size: 1.6 KB
RevLine 
[2]1#!/usr/bin/perl -w
2use strict;
3
[83]4use FindBin;
5use lib "$FindBin::RealBin/lib";
6
[2]7use YAML;
[15]8use Getopt::Long;
[2]9
[83]10use Bookmarks;
11
[15]12GetOptions(
13    'file|f=s' => \my $DBNAME,
[83]14    'title=s' => \my $TITLE,
[15]15);
[2]16
[83]17my $dbname = $DBNAME || $ENV{BKMK_DBNAME};
18die "Usage: $0 --file <dbname> <command>\n" unless $dbname;
[2]19
20my $bookmarks = Bookmarks->new({
[83]21    dbname => $dbname,
[2]22});
23
24my $command = shift;
25
26my %action_for = (
[47]27    init => sub {
28        $bookmarks->create_tables;
29    },
[83]30   
[2]31    get => sub {
32        my $identifier = shift;
33        my $query = $identifier =~ /^\d+$/ ? { id => $identifier } : { uri => $identifier };
34        my $bookmark = $bookmarks->get_bookmark($query);
35
36        print $bookmark ? Dump($bookmark) : "Not Found\n";
37    },
[83]38   
[2]39    add => sub {
[83]40        my ($uri, @tags) = @_;
41        my $title = defined $TITLE ? $TITLE : fetch_title($uri);
[2]42        my $bookmark = $bookmarks->add({ uri => $uri, title => $title, tags => \@tags });
43        print Dump($bookmark);
44    },
[83]45
[14]46    list => sub {
[83]47        my @tags = @_;
[26]48        my @resources = $bookmarks->get_bookmarks({
[83]49            tag => \@tags
[20]50        });
[14]51        # TODO: list by tags, date, etc.
52        # TODO: coordinate this commandline script with the CGI app
53        print Dump(\@resources);
[15]54    },
[83]55
56    # bulk loading
[15]57    load => sub {
58        my ($src_file) = @_;
59        my $src_bookmarks = YAML::LoadFile($src_file);
60        for my $bookmark (@{ $src_bookmarks }) {
61            $bookmarks->add($bookmark);
62        }
63    },
[2]64);
65
66$action_for{$command}->(@ARGV);
67
[83]68sub fetch_title {
69    my $uri = shift;
70    require WWW::Mechanize;
71    my $mech = WWW::Mechanize->new;
72    $mech->get($uri);
73    return $mech->title || $uri;
74}
Note: See TracBrowser for help on using the repository browser.