source: bookmarks/trunk/bin/bkmk @ 107

Last change on this file since 107 was 103, checked in by peter, 9 years ago
  • moved scripts into the bin directory
  • combined start and stop in bkmkd
  • Property svn:executable set to *
File size: 2.5 KB
RevLine 
[2]1#!/usr/bin/perl -w
2use strict;
3
[83]4use FindBin;
[103]5use lib "$FindBin::RealBin/../lib";
[83]6
[2]7use YAML;
[15]8use Getopt::Long;
[2]9
[83]10use Bookmarks;
11
[15]12GetOptions(
13    'file|f=s' => \my $DBNAME,
[83]14    'title=s' => \my $TITLE,
[15]15);
[2]16
[83]17my $dbname = $DBNAME || $ENV{BKMK_DBNAME};
18die "Usage: $0 --file <dbname> <command>\n" unless $dbname;
[2]19
20my $bookmarks = Bookmarks->new({
[83]21    dbname => $dbname,
[2]22});
23
24my $command = shift;
25
26my %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);
[91]36        print $bookmark ? Dump($bookmark->to_hashref) : "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 });
[91]43        print Dump($bookmark->to_hashref);
[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
[91]53        print Dump([ map { $_->to_hashref } @{ $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);
[91]62            print Dump($bookmark->to_hashref);
[85]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) = @_;
[91]79        my $dump = [ map { $_->to_hashref } @{ $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]86sub find_bookmark {
87    my $identifier = shift;
88    my $query = $identifier =~ /^\d+$/ ? { id => $identifier } : { uri => $identifier };
89    return $bookmarks->get_bookmark($query);
90}
91
[83]92sub 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
100sub 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}
Note: See TracBrowser for help on using the repository browser.