Changeset 120 in bookmarks for trunk


Ignore:
Timestamp:
02/25/16 17:17:13 (8 years ago)
Author:
peter
Message:

Moved bookmark scanning functionality into the bkmk script.

Location:
trunk
Files:
1 deleted
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bin/bkmk

    r103 r120  
    66 
    77use YAML; 
    8 use Getopt::Long; 
     8use Getopt::Long qw{GetOptions GetOptionsFromArray :config pass_through}; 
    99 
    1010use Bookmarks; 
     
    1212GetOptions( 
    1313    'file|f=s' => \my $DBNAME, 
    14     'title=s' => \my $TITLE, 
    1514); 
    1615 
     
    1918 
    2019my $bookmarks = Bookmarks->new({ 
    21     dbname => $dbname, 
    22 }); 
     20        dbname => $dbname, 
     21    }); 
    2322 
    2423my $command = shift; 
     
    3029        load_bookmarks($src_file) if $src_file; 
    3130    }, 
    32      
     31 
    3332    get => sub { 
    3433        my $identifier = shift; 
     
    3635        print $bookmark ? Dump($bookmark->to_hashref) : "Not Found\n"; 
    3736    }, 
    38      
     37 
    3938    add => sub { 
     39        GetOptionsFromArray( 
     40            \@_, 
     41            'title=s' => \my $TITLE, 
     42        ); 
    4043        my ($uri, @tags) = @_; 
    4144        my $title = defined $TITLE ? $TITLE : fetch_title($uri); 
     
    4750        my @tags = @_; 
    4851        my $resources = $bookmarks->search({ 
    49             tags => \@tags 
    50         }); 
     52                tags => \@tags 
     53            }); 
    5154        # TODO: list by tags, date, etc. 
    5255        # TODO: coordinate this commandline script with the CGI app 
     
    8083        $dump_file ? YAML::DumpFile($dump_file, $dump) : print Dump($dump); 
    8184    }, 
     85 
     86    # scanning for current status 
     87    scan => sub { 
     88        GetOptionsFromArray( 
     89            \@_, 
     90            'csv'       => \my $CSV, 
     91            'timeout=i' => \my $TIMEOUT, 
     92        ); 
     93 
     94        require LWP::UserAgent; 
     95        require Text::CSV; 
     96 
     97        $TIMEOUT ||= 10; 
     98 
     99        my $ua = LWP::UserAgent->new; 
     100        $ua->timeout($TIMEOUT); 
     101 
     102        my $csv = Text::CSV->new; 
     103 
     104        for my $bookmark (@{ $bookmarks->search->results }) { 
     105            printf "%3d %s\n", $bookmark->id, $bookmark->uri unless $CSV; 
     106            my $response = $ua->head($bookmark->uri); 
     107            printf "   -> %s\n", $response->status_line unless $CSV; 
     108            $csv->combine( 
     109                $bookmark->id, 
     110                $bookmark->uri, 
     111                $response->code, 
     112                $response->message, 
     113            ); 
     114            print $csv->string . "\n" if $CSV; 
     115        } 
     116    }, 
    82117); 
    83118 
Note: See TracChangeset for help on using the changeset viewer.