[2] | 1 | #!/usr/bin/perl -w |
---|
| 2 | use strict; |
---|
| 3 | |
---|
[83] | 4 | use FindBin; |
---|
| 5 | use lib "$FindBin::RealBin/lib"; |
---|
| 6 | |
---|
[2] | 7 | use YAML; |
---|
[15] | 8 | use Getopt::Long; |
---|
[2] | 9 | |
---|
[83] | 10 | use Bookmarks; |
---|
| 11 | |
---|
[15] | 12 | GetOptions( |
---|
| 13 | 'file|f=s' => \my $DBNAME, |
---|
[83] | 14 | 'title=s' => \my $TITLE, |
---|
[15] | 15 | ); |
---|
[2] | 16 | |
---|
[83] | 17 | my $dbname = $DBNAME || $ENV{BKMK_DBNAME}; |
---|
| 18 | die "Usage: $0 --file <dbname> <command>\n" unless $dbname; |
---|
[2] | 19 | |
---|
| 20 | my $bookmarks = Bookmarks->new({ |
---|
[83] | 21 | dbname => $dbname, |
---|
[2] | 22 | }); |
---|
| 23 | |
---|
| 24 | my $command = shift; |
---|
| 25 | |
---|
| 26 | my %action_for = ( |
---|
[47] | 27 | init => sub { |
---|
| 28 | $bookmarks->create_tables; |
---|
| 29 | }, |
---|
[83] | 30 | |
---|
[2] | 31 | get => sub { |
---|
| 32 | my $identifier = shift; |
---|
[85] | 33 | my $bookmark = find_bookmark($identifier); |
---|
| 34 | print $bookmark ? Dump($bookmark->TO_JSON) : "Not Found\n"; |
---|
[2] | 35 | }, |
---|
[83] | 36 | |
---|
[2] | 37 | add => sub { |
---|
[83] | 38 | my ($uri, @tags) = @_; |
---|
| 39 | my $title = defined $TITLE ? $TITLE : fetch_title($uri); |
---|
[2] | 40 | my $bookmark = $bookmarks->add({ uri => $uri, title => $title, tags => \@tags }); |
---|
[85] | 41 | print Dump($bookmark->TO_JSON); |
---|
[2] | 42 | }, |
---|
[83] | 43 | |
---|
[14] | 44 | list => sub { |
---|
[83] | 45 | my @tags = @_; |
---|
[85] | 46 | my $resources = $bookmarks->get_bookmarks({ |
---|
| 47 | tags => \@tags |
---|
[20] | 48 | }); |
---|
[14] | 49 | # TODO: list by tags, date, etc. |
---|
| 50 | # TODO: coordinate this commandline script with the CGI app |
---|
[85] | 51 | print Dump([ map { $_->TO_JSON } @{ $resources->results } ]); |
---|
[15] | 52 | }, |
---|
[83] | 53 | |
---|
[85] | 54 | tag => sub { |
---|
| 55 | my ($identifier, @tags) = @_; |
---|
| 56 | my $bookmark = find_bookmark($identifier); |
---|
| 57 | if ($bookmark) { |
---|
| 58 | $bookmark->tags(\@tags); |
---|
| 59 | $bookmarks->update($bookmark); |
---|
| 60 | print Dump($bookmark->TO_JSON); |
---|
| 61 | } else { |
---|
| 62 | die "Not found\n"; |
---|
| 63 | } |
---|
| 64 | }, |
---|
| 65 | |
---|
| 66 | #TODO: interactive editing of a bookmark |
---|
| 67 | |
---|
[83] | 68 | # bulk loading |
---|
[15] | 69 | load => sub { |
---|
| 70 | my ($src_file) = @_; |
---|
| 71 | my $src_bookmarks = YAML::LoadFile($src_file); |
---|
| 72 | for my $bookmark (@{ $src_bookmarks }) { |
---|
| 73 | $bookmarks->add($bookmark); |
---|
| 74 | } |
---|
| 75 | }, |
---|
[2] | 76 | ); |
---|
| 77 | |
---|
| 78 | $action_for{$command}->(@ARGV); |
---|
| 79 | |
---|
[85] | 80 | sub find_bookmark { |
---|
| 81 | my $identifier = shift; |
---|
| 82 | my $query = $identifier =~ /^\d+$/ ? { id => $identifier } : { uri => $identifier }; |
---|
| 83 | return $bookmarks->get_bookmark($query); |
---|
| 84 | } |
---|
| 85 | |
---|
[83] | 86 | sub fetch_title { |
---|
| 87 | my $uri = shift; |
---|
| 88 | require WWW::Mechanize; |
---|
| 89 | my $mech = WWW::Mechanize->new; |
---|
| 90 | $mech->get($uri); |
---|
| 91 | return $mech->title || $uri; |
---|
| 92 | } |
---|