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