source: bookmarks/trunk/bkmk @ 91

Last change on this file since 91 was 91, checked in by peter, 9 years ago

change TO_JSON() method name to to_hashref() to emphasize its multiple uses

  • Property svn:executable set to *
File size: 2.5 KB
Line 
1#!/usr/bin/perl -w
2use strict;
3
4use FindBin;
5use lib "$FindBin::RealBin/lib";
6
7use YAML;
8use Getopt::Long;
9
10use Bookmarks;
11
12GetOptions(
13    'file|f=s' => \my $DBNAME,
14    'title=s' => \my $TITLE,
15);
16
17my $dbname = $DBNAME || $ENV{BKMK_DBNAME};
18die "Usage: $0 --file <dbname> <command>\n" unless $dbname;
19
20my $bookmarks = Bookmarks->new({
21    dbname => $dbname,
22});
23
24my $command = shift;
25
26my %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
86sub find_bookmark {
87    my $identifier = shift;
88    my $query = $identifier =~ /^\d+$/ ? { id => $identifier } : { uri => $identifier };
89    return $bookmarks->get_bookmark($query);
90}
91
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}
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.