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 qw{GetOptions GetOptionsFromArray :config pass_through}; |
---|
9 | |
---|
10 | use Bookmarks; |
---|
11 | |
---|
12 | GetOptions( |
---|
13 | 'file|f=s' => \my $DBNAME, |
---|
14 | ); |
---|
15 | |
---|
16 | my $dbname = $DBNAME || $ENV{BKMK_DBNAME} || 'bookmarks.db'; |
---|
17 | die "Usage: $0 --file <dbname> <command>\n" unless $dbname; |
---|
18 | |
---|
19 | my $bookmarks = Bookmarks->new({ |
---|
20 | dbname => $dbname, |
---|
21 | }); |
---|
22 | |
---|
23 | my $command = shift; |
---|
24 | |
---|
25 | my %action_for = ( |
---|
26 | init => sub { |
---|
27 | my $src_file = shift; |
---|
28 | $bookmarks->create_tables; |
---|
29 | load_bookmarks($src_file) if $src_file; |
---|
30 | }, |
---|
31 | |
---|
32 | get => sub { |
---|
33 | my $identifier = shift; |
---|
34 | my $bookmark = find_bookmark($identifier); |
---|
35 | print $bookmark ? Dump($bookmark->to_hashref) : "Not Found\n"; |
---|
36 | }, |
---|
37 | |
---|
38 | add => sub { |
---|
39 | GetOptionsFromArray( |
---|
40 | \@_, |
---|
41 | 'title=s' => \my $TITLE, |
---|
42 | ); |
---|
43 | my ($uri, @tags) = @_; |
---|
44 | my $title = defined $TITLE ? $TITLE : fetch_title($uri); |
---|
45 | my $bookmark = $bookmarks->add({ uri => $uri, title => $title, tags => \@tags }); |
---|
46 | print Dump($bookmark->to_hashref); |
---|
47 | }, |
---|
48 | |
---|
49 | list => sub { |
---|
50 | GetOptionsFromArray( |
---|
51 | \@_, |
---|
52 | 'query|q=s' => \my $QUERY, |
---|
53 | ); |
---|
54 | my @tags = @_; |
---|
55 | my $resources = $bookmarks->search({ |
---|
56 | query => $QUERY, |
---|
57 | tags => \@tags |
---|
58 | }); |
---|
59 | # TODO: list by tags, date, etc. |
---|
60 | # TODO: coordinate this commandline script with the CGI app |
---|
61 | print Dump([ map { $_->to_hashref } @{ $resources->results } ]); |
---|
62 | }, |
---|
63 | |
---|
64 | tag => sub { |
---|
65 | my ($identifier, @tags) = @_; |
---|
66 | my $bookmark = find_bookmark($identifier); |
---|
67 | if ($bookmark) { |
---|
68 | $bookmark->tags(\@tags); |
---|
69 | $bookmarks->update($bookmark); |
---|
70 | print Dump($bookmark->to_hashref); |
---|
71 | } else { |
---|
72 | die "Not found\n"; |
---|
73 | } |
---|
74 | }, |
---|
75 | |
---|
76 | # interactive editing of a bookmark |
---|
77 | edit => sub { |
---|
78 | my ($identifier) = @_; |
---|
79 | my $bookmark = find_bookmark($identifier) or die "Not found\n"; |
---|
80 | my $editor = $ENV{VISUAL} || $ENV{EDITOR} || 'vi'; |
---|
81 | |
---|
82 | # create a temp file and open it in the user's preferred editor |
---|
83 | use File::Temp qw{tempfile}; |
---|
84 | my ($fh, $filename) = tempfile(UNLINK => 1); |
---|
85 | YAML::DumpFile($filename, $bookmark->to_hashref); |
---|
86 | system($editor, $filename); |
---|
87 | |
---|
88 | # abort on empty file |
---|
89 | die "Edit canceled\n" unless -s $filename; |
---|
90 | |
---|
91 | # update and save the bookmark |
---|
92 | $bookmark = Bookmark->new(YAML::LoadFile($filename)); |
---|
93 | $bookmarks->update($bookmark); |
---|
94 | print Dump($bookmark->to_hashref); |
---|
95 | }, |
---|
96 | |
---|
97 | # bulk loading |
---|
98 | load => sub { |
---|
99 | my ($src_file) = @_; |
---|
100 | load_bookmarks($src_file); |
---|
101 | }, |
---|
102 | |
---|
103 | # bulk dumping |
---|
104 | dump => sub { |
---|
105 | my ($dump_file) = @_; |
---|
106 | my $dump = [ map { $_->to_hashref } @{ $bookmarks->search->results } ]; |
---|
107 | $dump_file ? YAML::DumpFile($dump_file, $dump) : print Dump($dump); |
---|
108 | }, |
---|
109 | |
---|
110 | # scanning for current status |
---|
111 | scan => sub { |
---|
112 | GetOptionsFromArray( |
---|
113 | \@_, |
---|
114 | 'csv' => \my $CSV, |
---|
115 | 'timeout=i' => \my $TIMEOUT, |
---|
116 | ); |
---|
117 | |
---|
118 | require LWP::UserAgent; |
---|
119 | require Text::CSV; |
---|
120 | |
---|
121 | $TIMEOUT ||= 10; |
---|
122 | |
---|
123 | my $ua = LWP::UserAgent->new; |
---|
124 | $ua->timeout($TIMEOUT); |
---|
125 | |
---|
126 | my $csv = Text::CSV->new; |
---|
127 | |
---|
128 | for my $bookmark (@{ $bookmarks->search->results }) { |
---|
129 | printf "%3d %s\n", $bookmark->id, $bookmark->uri unless $CSV; |
---|
130 | my $response = $ua->head($bookmark->uri); |
---|
131 | printf " -> %s\n", $response->status_line unless $CSV; |
---|
132 | $csv->combine( |
---|
133 | $bookmark->id, |
---|
134 | $bookmark->uri, |
---|
135 | $response->code, |
---|
136 | $response->message, |
---|
137 | ); |
---|
138 | print $csv->string . "\n" if $CSV; |
---|
139 | } |
---|
140 | }, |
---|
141 | ); |
---|
142 | |
---|
143 | $action_for{$command}->(@ARGV); |
---|
144 | |
---|
145 | sub find_bookmark { |
---|
146 | my $identifier = shift; |
---|
147 | my $query = $identifier =~ /^\d+$/ ? { id => $identifier } : { uri => $identifier }; |
---|
148 | return $bookmarks->get_bookmark($query); |
---|
149 | } |
---|
150 | |
---|
151 | sub fetch_title { |
---|
152 | my $uri = shift; |
---|
153 | require WWW::Mechanize; |
---|
154 | my $mech = WWW::Mechanize->new; |
---|
155 | $mech->get($uri); |
---|
156 | return $mech->title || $uri; |
---|
157 | } |
---|
158 | |
---|
159 | sub load_bookmarks { |
---|
160 | my $src_file = shift; |
---|
161 | my $src_bookmarks = YAML::LoadFile($src_file); |
---|
162 | for my $bookmark (@{ $src_bookmarks }) { |
---|
163 | $bookmarks->add($bookmark); |
---|
164 | } |
---|
165 | } |
---|