読者です 読者をやめる 読者になる 読者になる

理系学生日記

おまえはいつまで学生気分なのか

忍者TOOLS

Pocket クライアントモジュールを API v3 に対応させました

対応させました。WebService::Pocket::Lite です。

Pocket の Consumer Key と Access Token を取得していることを前提に、以下のようなコードが書けます。

use WebService::Pocket::Lite;

my $lite = WebService::Pocket::Lite->new(
    access_token => 'your access token',
    consumer_key => 'consumer key',
);

# retrieve entries from your Pocket.
my $res = $lite->retrieve( state => 'unread', tag => 'perl' );

# add a entry to your Pocket.
$lite->add( url => 'http://www.cpan.org' );

# add some entries and change tags of another entry with 1 request.
$lite->push_add( url => 'http://metacpan.org/' );
$lite->push_add( url => 'http://cpants.cpanauthors.org/' );
$lite->push_tags_replace( item_id => 100, tags => [qw/tag1 tag2/] );
$lite->send;

これを利用すると、Livedoor Reader でピンを立てたエントリを Pocket に転送するというデイリータスクは以下のようなスクリプトで実現できます。

use Try::Tiny;
use WebService::LDR;
use Log::Minimal;

run;

sub run {
    my %param = @_;

    try {
        my $ldr    = create_ldr();
        my $pocket = create_pocket();  
        
        # max 100 entries
        my @entries = $ldr->login->get_pin_all();
        my $num = scalar(@entries);

        foreach my $entry (@entries) {
            $pocket->push_add({ 
                title => encode('utf8' => $entry->title),
                url => $entry->link->as_string 
            });
        }
        $pocket->send;

        $ldr->clear_pin;
    } catch {
        critff( $_ );
    } finally {
        infof("$0 ends.");
    };
}

sub create_ldr {
    my $conf = pit_get('reader.livedoor.com', require => {
        username => 'livedoor reader username',
        password => 'livedoor reader password',
    });

    my $ldr = WebService::LDR->new(
        user => $conf->{username},
        pass => $conf->{password},
    );
}

sub create_pocket {
    my $conf = pit_get('getpocket.com', require => {
        consumer_key => 'Pocket consumer key',
        access_token => 'Pocket access token',
    });
   
    my $pocket = WebService::Pocket::Lite->new(
        consumer_key => $conf->{consumer_key},
        access_token => $conf->{access_token},
    );
}

Pocket の未読数を GrowthForecast でグラフ化するスクリプトはこんな感じですね

my $pocket = create_pocket();
my $ua = LWP::UserAgent->new;

foreach my $state (qw/unread/) {
    my $res = $pocket->retrieve({ state => $state });
    my $num = scalar( keys %{$res->{list}} );

    $ua->post( "$growthforecast/api/kiririmode/pocket/$state", {
        number => $num
    });
}


sub create_pocket {
    my $conf = pit_get('getpocket.com', require => {
        consumer_key => 'Pocket consumer key',
        access_token => 'Pocket access token',
    });
   
    my $pocket = WebService::Pocket::Lite->new(
        consumer_key => $conf->{consumer_key},
        access_token => $conf->{access_token},
    );
}