source: bookmarks/trunk/bkmk @ 83

Last change on this file since 83 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
Line 
1#!/usr/bin/perl -w
2use strict;
3
4use FindBin;
5use lib "$FindBin::RealBin/lib";
6
7use YAML;
8use Getopt::Long;
9
10use Bookmarks;
11
12GetOptions(
13    'file|f=s' => \my $DBNAME,
14    'title=s' => \my $TITLE,
15);
16
17my $dbname = $DBNAME || $ENV{BKMK_DBNAME};
18die "Usage: $0 --file <dbname> <command>\n" unless $dbname;
19
20my $bookmarks = Bookmarks->new({
21    dbname => $dbname,
22});
23
24my $command = shift;
25
26my %action_for = (
27    init => sub {
28        $bookmarks->create_tables;
29    },
30   
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    },
38   
39    add => sub {
40        my ($uri, @tags) = @_;
41        my $title = defined $TITLE ? $TITLE : fetch_title($uri);
42        my $bookmark = $bookmarks->add({ uri => $uri, title => $title, tags => \@tags });
43        print Dump($bookmark);
44    },
45
46    list => sub {
47        my @tags = @_;
48        my @resources = $bookmarks->get_bookmarks({
49            tag => \@tags
50        });
51        # TODO: list by tags, date, etc.
52        # TODO: coordinate this commandline script with the CGI app
53        print Dump(\@resources);
54    },
55
56    # bulk loading
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    },
64);
65
66$action_for{$command}->(@ARGV);
67
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.