[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 { |
---|
[86] | 28 | my $src_file = shift; |
---|
[47] | 29 | $bookmarks->create_tables; |
---|
[86] | 30 | load_bookmarks($src_file) if $src_file; |
---|
[47] | 31 | }, |
---|
[83] | 32 | |
---|
[2] | 33 | get => sub { |
---|
| 34 | my $identifier = shift; |
---|
[85] | 35 | my $bookmark = find_bookmark($identifier); |
---|
| 36 | print $bookmark ? Dump($bookmark->TO_JSON) : "Not Found\n"; |
---|
[2] | 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 }); |
---|
[85] | 43 | print Dump($bookmark->TO_JSON); |
---|
[2] | 44 | }, |
---|
[83] | 45 | |
---|
[14] | 46 | list => sub { |
---|
[83] | 47 | my @tags = @_; |
---|
[88] | 48 | my $resources = $bookmarks->search({ |
---|
[85] | 49 | tags => \@tags |
---|
[20] | 50 | }); |
---|
[14] | 51 | # TODO: list by tags, date, etc. |
---|
| 52 | # TODO: coordinate this commandline script with the CGI app |
---|
[85] | 53 | print Dump([ map { $_->TO_JSON } @{ $resources->results } ]); |
---|
[15] | 54 | }, |
---|
[83] | 55 | |
---|
[85] | 56 | tag => sub { |
---|
| 57 | my ($identifier, @tags) = @_; |
---|
| 58 | my $bookmark = find_bookmark($identifier); |
---|
| 59 | if ($bookmark) { |
---|
| 60 | $bookmark->tags(\@tags); |
---|
| 61 | $bookmarks->update($bookmark); |
---|
| 62 | print Dump($bookmark->TO_JSON); |
---|
| 63 | } else { |
---|
| 64 | die "Not found\n"; |
---|
| 65 | } |
---|
| 66 | }, |
---|
| 67 | |
---|
| 68 | #TODO: interactive editing of a bookmark |
---|
| 69 | |
---|
[83] | 70 | # bulk loading |
---|
[15] | 71 | load => sub { |
---|
| 72 | my ($src_file) = @_; |
---|
[86] | 73 | load_bookmarks($src_file); |
---|
[15] | 74 | }, |
---|
[87] | 75 | |
---|
| 76 | # bulk dumping |
---|
| 77 | dump => sub { |
---|
| 78 | my ($dump_file) = @_; |
---|
[88] | 79 | my $dump = [ map { $_->TO_JSON } @{ $bookmarks->search->results } ]; |
---|
[87] | 80 | $dump_file ? YAML::DumpFile($dump_file, $dump) : print Dump($dump); |
---|
| 81 | }, |
---|
[2] | 82 | ); |
---|
| 83 | |
---|
| 84 | $action_for{$command}->(@ARGV); |
---|
| 85 | |
---|
[85] | 86 | sub find_bookmark { |
---|
| 87 | my $identifier = shift; |
---|
| 88 | my $query = $identifier =~ /^\d+$/ ? { id => $identifier } : { uri => $identifier }; |
---|
| 89 | return $bookmarks->get_bookmark($query); |
---|
| 90 | } |
---|
| 91 | |
---|
[83] | 92 | sub fetch_title { |
---|
| 93 | my $uri = shift; |
---|
| 94 | require WWW::Mechanize; |
---|
| 95 | my $mech = WWW::Mechanize->new; |
---|
| 96 | $mech->get($uri); |
---|
| 97 | return $mech->title || $uri; |
---|
| 98 | } |
---|
[86] | 99 | |
---|
| 100 | sub load_bookmarks { |
---|
| 101 | my $src_file = shift; |
---|
| 102 | my $src_bookmarks = YAML::LoadFile($src_file); |
---|
| 103 | for my $bookmark (@{ $src_bookmarks }) { |
---|
| 104 | $bookmarks->add($bookmark); |
---|
| 105 | } |
---|
| 106 | } |
---|