Subscribed unsubscribe Subscribe Subscribe

evernoteapiのperlのサンプルがなかったので

こんな感じで書いてみたのだけど、あってるのかな。ヒアドキュメントで書いたやつをそのまま関数には渡せないのかな。他のサンプル通り文字と画像のアップロードが出来るようになりました。あとなんか二ファイルぐらい、useしてないところがあって怒られたので、適宜useするようにした。

use strict;
use warnings;

use lib qw{../../lib/perl};
use Thrift::BinaryProtocol;
use Thrift::HttpClient;
use EDAMUserStore::UserStore;
use EDAMNoteStore::NoteStore;

use Try::Tiny;
use Data::Dumper;
use Perl6::Say;
use Digest::MD5 qw/md5_hex/;
use Path::Class qw/file/;
use DateTime;
use Config::Pit;

my %account = %{pit_get('evernote-account')};
my %consumer = %{pit_get('evernote-consumer')};

my $evernote_host = "sandbox.evernote.com";
my $user_store_url = "https://$evernote_host/edam/user";
my $note_store_url_base = "https://$evernote_host/edam/note/";

my $user_store_transport = Thrift::HttpClient->new( $user_store_url );
my $user_store_protocol = Thrift::BinaryProtocol->new( $user_store_transport );
my $user_store = EDAMUserStore::UserStoreClient->new( $user_store_protocol );

my $auth_result;
try {
    $auth_result = $user_store->authenticate( 
        $account{username}, 
        $account{password}, 
        $consumer{consumer_key}, 
        $consumer{consumer_secret} 
    );
}
catch {
    print Dumper $_;
    exit;
};

my $user = $auth_result->user;
my $auth_token = $auth_result->authenticationToken;

say sprintf "Authentication was successful for %s", $user->username;
say sprintf "Authentication token = %s", $auth_token;

my $note_store_url = $note_store_url_base . $user->shardId;
my $note_store_tranport = Thrift::HttpClient->new( $note_store_url );
my $note_store_protocol = Thrift::BinaryProtocol->new( $note_store_tranport );
my $note_store = EDAMNoteStore::NoteStoreClient->new( $note_store_protocol );

my $notebooks = $note_store->listNotebooks( $auth_token );
say sprintf "Found %d notebooks;", scalar @$notebooks;
my $default_notebook = $notebooks->[0];
for my $notebook (@$notebooks) {
    say sprintf " * %s", $notebook->name;
    if ( $notebook->defaultNotebook ) {
        $default_notebook = $notebook;
    }
}

say sprintf "\nCreating a new note in the default notebook: %s \n", $default_notebook->name;

my $file = file('enlogo.png');

my $data = EDAMTypes::Data->new;
my $body = $file->slurp;
$data->size( $file->lstat->size );
$data->body( $body );
$data->bodyHash( md5_hex $body );

my $resource = EDAMTypes::Resource->new;
$resource->mime( "image/png" );
$resource->data( $data );

my $note = EDAMTypes::Note->new;
$note->notebookGuid( $default_notebook->guid );
$note->title( "Test note from perl" );
my $xml = <<XML;
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml.dtd">
<en-note>Here is the Evernote logo:<br/>
<en-media type="image/png" hash="@{[ $data->bodyHash ]}"/>
</en-note>
XML
$note->content( $xml );

$note->created( DateTime->now->set_time_zone('Asia/Tokyo')->epoch * 1000);
$note->updated( $note->created );
$note->resources( [ $resource ] );

try {
    my $created_note = $note_store->createNote( $auth_token, $note );
    say sprintf "Note waas created, GUID = %s", $created_note->guid;
}
catch {
    print Dumper $_;
};