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 |
---|
2 | use strict; |
---|
3 | |
---|
4 | use FindBin; |
---|
5 | use lib "$FindBin::RealBin/lib"; |
---|
6 | |
---|
7 | use YAML; |
---|
8 | use Getopt::Long; |
---|
9 | |
---|
10 | use Bookmarks; |
---|
11 | |
---|
12 | GetOptions( |
---|
13 | 'file|f=s' => \my $DBNAME, |
---|
14 | 'title=s' => \my $TITLE, |
---|
15 | ); |
---|
16 | |
---|
17 | my $dbname = $DBNAME || $ENV{BKMK_DBNAME}; |
---|
18 | die "Usage: $0 --file <dbname> <command>\n" unless $dbname; |
---|
19 | |
---|
20 | my $bookmarks = Bookmarks->new({ |
---|
21 | dbname => $dbname, |
---|
22 | }); |
---|
23 | |
---|
24 | my $command = shift; |
---|
25 | |
---|
26 | my %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 | |
---|
68 | sub 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.