Vimで選択したテキストを Google Notebook に保存する


とりあえず WWW::Google::Notebook っていうのを作った。

WWW-Google-Notebook-0.01 - Perl interface for Google Notebook - metacpan.org


http://d.hatena.ne.jp/bonar/20061002/1159745024 で紹介されている PythonGoogle Notebook のライブラリを見て、Firebug で解析しながら作った。 Firebug サイコウ。


で、インストールして、こんなスクリプトをどっかにおいておく。

#!/usr/local/bin/perl
use strict;
use warnings;

use Encode;
use Encode::Guess qw(utf8 euc-jp shiftjis 7bit-jis);
use WWW::Google::Notebook;

my $username = 'username@gmail.com';
my $password = 'password';
my $title    = 'vim';

my $google = WWW::Google::Notebook->new(
    username => $username,
    password => $password,
);
$google->login;
my $notebooks = $google->notebooks;
my ($notebook) = grep { $_->title eq $title } @$notebooks;
$notebook = $google->add_notebook($title) unless $notebook;
my $content = do { local $/; <STDIN> };
$content = encode('utf8', decode('Guess', $content));
$notebook->add_note($content);
print $content;

追記

文字コード変換を追加。


あとはVimで v なり V で選択して

:'<,'>!/path/to/scriptname.pl

とかやると保存される。


.vimrcに

vnoremap <leader>gn :'<,'>!/path/to/scriptname.pl<CR>

とかやっとくと \gnでサクっと保存出来る。


対話型のスクリプトはこんな感じで。

#!/usr/local/bin/perl
use strict;
use warnings;

use Term::ReadLine;
use Term::ReadPassword;
use WWW::Google::Notebook;

my $term = Term::ReadLine->new;
my $username = $term->readline('username: ');
my $password = read_password('password: ');

my $google = WWW::Google::Notebook->new(
    username => $username,
    password => $password,
);
$google->login;
my $notebooks = $google->notebooks;
my $index = 0;
for my $notebook (@$notebooks) {
    printf '[%d] %s'."\n", $index++, $notebook->name;
}
my $selected = $term->readline('notebook: ');
die 'invalid notebook' if $selected !~ /^\d+$/ or $selected >= @$notebooks;
my $notebook = $notebooks->[$selected] or die 'invalid notebook';
my $content = join '', <STDIN>;
$notebook->add_note($content);
print "added your note!\n";