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_JSON) : "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_JSON); |
---|
44 | }, |
---|
45 | |
---|
46 | list => sub { |
---|
47 | my @tags = @_; |
---|
48 | my $resources = $bookmarks->get_bookmarks({ |
---|
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_JSON } @{ $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_JSON); |
---|
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 | |
---|
77 | $action_for{$command}->(@ARGV); |
---|
78 | |
---|
79 | sub find_bookmark { |
---|
80 | my $identifier = shift; |
---|
81 | my $query = $identifier =~ /^\d+$/ ? { id => $identifier } : { uri => $identifier }; |
---|
82 | return $bookmarks->get_bookmark($query); |
---|
83 | } |
---|
84 | |
---|
85 | sub fetch_title { |
---|
86 | my $uri = shift; |
---|
87 | require WWW::Mechanize; |
---|
88 | my $mech = WWW::Mechanize->new; |
---|
89 | $mech->get($uri); |
---|
90 | return $mech->title || $uri; |
---|
91 | } |
---|
92 | |
---|
93 | sub load_bookmarks { |
---|
94 | my $src_file = shift; |
---|
95 | my $src_bookmarks = YAML::LoadFile($src_file); |
---|
96 | for my $bookmark (@{ $src_bookmarks }) { |
---|
97 | $bookmarks->add($bookmark); |
---|
98 | } |
---|
99 | } |
---|