tetsunosukeのnotebook

tetsunosukeのメモです

[perl] [note] Google Readerの☆をつけたものをGoogle BookmarksにPOSTする

use strict;
use warnings;
use WWW::Mechanize;
use WWW::Mechanize::DecodedContent;
use Encode;
use XML::XPath;


##
my $url = 'http://www.google.com/reader';
my $res;

my $mech = WWW::Mechanize->new;

# log in
$res = $mech->get($url) || die $!;
$res = $mech->submit_form(
    form_number => 1,
    fields => { 
        Email => '<username>',
        Passwd => '<password>'
    }   
);  
$res = $mech->get($res->request->uri);
my $refresh = $res->headers->header('Refresh');
$refresh =~ /url='(.*)'/;
$res = $mech->get($1);
$res = $mech->get($res->request->uri);
$res = $mech->get('http://www.google.com/reader/atom/user/-/state/com.google/starred');
print $mech->content;


my $xml = XML::XPath->new(xml => $mech->content);
my @nodes = $xml->findnodes('/feed/entry');
foreach my $n (@nodes) {
    my $p = XML::XPath->new(context => $n);
    my $title  = $p->findvalue('title');
    my $link = $p->findvalue('link/@href');

    my %data = (title => $title, hl => 'ja',
                bkmk  => $link, 
                op    => 'add',
                labels => 'gr');
    $res = $mech->post('http://www.google.com/bookmarks/mark', [%data]);
    $res = $mech->submit_form( form_name => 'add_bkmk_form');
}

どんだけリダイレクトさせんの。

前半部分はログインして☆の部分のデータをatomで拾うまでにリダイレクトされまくり。後半はXML::XPathatomフィードを解析して、そのデータをbookmarkにPOST。APIじゃないみたいで確認画面が出るので、その中で投稿のフォームをsubmitしている。日本語が多分通らないので、そこは$titleの部分はEncodeを適切にすれば良いはず。