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 | my $src_file = shift; |
---|
29 | $bookmarks->create_tables; |
---|
30 | load_bookmarks($src_file) if $src_file; |
---|
31 | }, |
---|
32 | |
---|
33 | get => sub { |
---|
34 | my $identifier = shift; |
---|
35 | my $bookmark = find_bookmark($identifier); |
---|
36 | print $bookmark ? Dump($bookmark->to_hashref) : "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->to_hashref); |
---|
44 | }, |
---|
45 | |
---|
46 | list => sub { |
---|
47 | my @tags = @_; |
---|
48 | my $resources = $bookmarks->search({ |
---|
49 | tags => \@tags |
---|
50 | }); |
---|
51 | # TODO: list by tags, date, etc. |
---|
52 | # TODO: coordinate this commandline script with the CGI app |
---|
53 | print Dump([ map { $_->to_hashref } @{ $resources->results } ]); |
---|
54 | }, |
---|
55 | |
---|
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_hashref); |
---|
63 | } else { |
---|
64 | die "Not found\n"; |
---|
65 | } |
---|
66 | }, |
---|
67 | |
---|
68 | #TODO: interactive editing of a bookmark |
---|
69 | |
---|
70 | # bulk loading |
---|
71 | load => sub { |
---|
72 | my ($src_file) = @_; |
---|
73 | load_bookmarks($src_file); |
---|
74 | }, |
---|
75 | |
---|
76 | # bulk dumping |
---|
77 | dump => sub { |
---|
78 | my ($dump_file) = @_; |
---|
79 | my $dump = [ map { $_->to_hashref } @{ $bookmarks->search->results } ]; |
---|
80 | $dump_file ? YAML::DumpFile($dump_file, $dump) : print Dump($dump); |
---|
81 | }, |
---|
82 | ); |
---|
83 | |
---|
84 | $action_for{$command}->(@ARGV); |
---|
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 | |
---|
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 | } |
---|
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 | } |
---|