| 1 | ########################################### | ||
| 2 | package Net::Evernote::Simple; | ||
| 3 | ########################################### | ||
| 4 | |||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 1 | ▶ | use Net::Evernote::Simple::EDAMUserStore::Constants; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use Net::Evernote::Simple::EDAMUserStore::Types; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use Net::Evernote::Simple::EDAMUserStore::UserStore; | ||
| 11 | use Net::Evernote::Simple::EDAMNoteStore::NoteStore; | ||
| 12 | use Net::Evernote::Simple::EDAMNoteStore::Types; | ||
| 13 | use Net::Evernote::Simple::EDAMErrors::Types; | ||
| 14 | use Net::Evernote::Simple::EDAMTypes::Types; | ||
| 15 | use Log::Log4perl qw(:easy); | ||
| 16 | use YAML qw( LoadFile ); | ||
| 17 | use File::Basename; | ||
| 18 | use File::Temp qw( tempfile ); | ||
| 19 | use Thrift; | ||
| 20 | use Thrift::HttpClient; | ||
| 21 | use Thrift::BinaryProtocol; | ||
| 22 | use Data::Dumper; | ||
| 23 | |||
| 24 | our $VERSION = "0.06"; | ||
| 25 | |||
| 26 | our $EN_DEV_TOKEN_PAGE = | ||
| 27 | "http://dev.evernote.com/documentation/cloud/chapters/" . | ||
| 28 | "Authentication.php#devtoken"; | ||
| 29 | |||
| 30 | ########################################### | ||
| 31 | sub new { | ||
| 32 | ########################################### | ||
| 33 | my($class, %options) = @_; | ||
| 34 | |||
| 35 | my $self = { | ||
| 36 | evernote_host => "www.evernote.com", | ||
| 37 | dev_token => undef, | ||
| 38 | config_file => undef, | ||
| 39 | consumer_key => undef, | ||
| 40 | thrift_send_timeout => 10000, | ||
| 41 | thrift_recv_timeout => 75000, | ||
| 42 | %options, | ||
| 43 | }; | ||
| 44 | |||
| 45 | bless $self, $class; | ||
| 46 | |||
| 47 | if( !defined $self->{ consumer_key } ) { | ||
| 48 | ( my $dashed_pkg = __PACKAGE__ ) =~ s/::/-/g; | ||
| 49 | $self->{ consumer_key } = lc $dashed_pkg; | ||
| 50 | } | ||
| 51 | |||
| 52 | if( ! defined $self->{ config_file } ) { | ||
| 53 | my( $home ) = glob "~"; | ||
| 54 | $self->{ config_file } = "$home/.evernote.yml"; | ||
| 55 | } | ||
| 56 | |||
| 57 | if( !defined $self->{ dev_token } ) { | ||
| 58 | if( -f $self->{ config_file } ) { | ||
| 59 | my $data = LoadFile $self->{ config_file }; | ||
| 60 | if( exists $data->{ dev_token } ) { | ||
| 61 | $self->{ dev_token } = $data->{ dev_token }; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | my $user_store_uri = | ||
| 67 | "https://$self->{ evernote_host }/edam/user"; | ||
| 68 | |||
| 69 | my $http_client = $self->thrift_http_client( $user_store_uri ); | ||
| 70 | my $protocol = | ||
| 71 | Thrift::BinaryProtocol->new( $http_client ); | ||
| 72 | |||
| 73 | $self->{ client } = | ||
| 74 | Net::Evernote::Simple::EDAMUserStore::UserStoreClient->new( $protocol ); | ||
| 75 | |||
| 76 | return $self; | ||
| 77 | } | ||
| 78 | |||
| 79 | ########################################### | ||
| 80 | sub init { | ||
| 81 | ########################################### | ||
| 82 | my( $self ) = @_; | ||
| 83 | |||
| 84 | if( $self->{ init_done } ) { | ||
| 85 | return 1; | ||
| 86 | } | ||
| 87 | |||
| 88 | if( !defined $self->{ dev_token } ) { | ||
| 89 | LOGDIE "Developer token argument 'dev_token' missing. ", | ||
| 90 | "Check $EN_DEV_TOKEN_PAGE on how to obtain one."; | ||
| 91 | } | ||
| 92 | |||
| 93 | if( ! $self->version_check() ) { | ||
| 94 | LOGDIE "Version check failed"; | ||
| 95 | } | ||
| 96 | |||
| 97 | $self->{ init_done } = 1; | ||
| 98 | } | ||
| 99 | |||
| 100 | ########################################### | ||
| 101 | sub dev_token { | ||
| 102 | ########################################### | ||
| 103 | my( $self ) = @_; | ||
| 104 | |||
| 105 | return $self->{ dev_token }; | ||
| 106 | } | ||
| 107 | |||
| 108 | ########################################### | ||
| 109 | sub thrift_http_client { | ||
| 110 | ########################################### | ||
| 111 | my( $self, $uri ) = @_; | ||
| 112 | |||
| 113 | my $client = Thrift::HttpClient->new( $uri ); | ||
| 114 | |||
| 115 | # Timeouts can't be passed into Thrift::HttpClient's constructor, | ||
| 116 | # so we set them manually here. Thrift's default values | ||
| 117 | # are in the millisecond range and therefore completely out | ||
| 118 | # of whack unless you have the Evernote server on your LAN. | ||
| 119 | $client->setSendTimeout( $self->{ thrift_send_timeout } ); | ||
| 120 | $client->setRecvTimeout( $self->{ thrift_recv_timeout } ); | ||
| 121 | |||
| 122 | return $client; | ||
| 123 | } | ||
| 124 | |||
| 125 | ########################################### | ||
| 126 | sub note_store { | ||
| 127 | ########################################### | ||
| 128 | my( $self ) = @_; | ||
| 129 | |||
| 130 | $self->init(); | ||
| 131 | |||
| 132 | my $note_store_uri; | ||
| 133 | |||
| 134 | eval { | ||
| 135 | $note_store_uri = | ||
| 136 | $self->{ client }->getNoteStoreUrl( $self->{ dev_token } ); | ||
| 137 | }; | ||
| 138 | |||
| 139 | if( $@ ) { | ||
| 140 | ERROR Dumper( $@ ); | ||
| 141 | return undef; | ||
| 142 | } | ||
| 143 | |||
| 144 | my $note_store_client = $self->thrift_http_client( $note_store_uri ); | ||
| 145 | |||
| 146 | my $note_store_protocol = Thrift::BinaryProtocol->new( | ||
| 147 | $note_store_client ); | ||
| 148 | |||
| 149 | my $note_store = | ||
| 150 | Net::Evernote::Simple::EDAMNoteStore::NoteStoreClient->new( | ||
| 151 | $note_store_protocol ); | ||
| 152 | |||
| 153 | return $note_store; | ||
| 154 | } | ||
| 155 | |||
| 156 | ########################################### | ||
| 157 | sub version_check { | ||
| 158 | ########################################### | ||
| 159 | my( $self ) = @_; | ||
| 160 | |||
| 161 | eval { | ||
| 162 | my $version_ok = | ||
| 163 | $self->{ client }->checkVersion( $self->{ consumer_key }, | ||
| 164 | Net::Evernote::Simple::EDAMUserStore::Constants::EDAM_VERSION_MAJOR, | ||
| 165 | Net::Evernote::Simple::EDAMUserStore::Constants::EDAM_VERSION_MINOR, | ||
| 166 | ); | ||
| 167 | |||
| 168 | INFO "Version check returned: $version_ok"; | ||
| 169 | }; | ||
| 170 | |||
| 171 | if( $@ ) { | ||
| 172 | LOGWARN Dumper( $@ ); | ||
| 173 | $self->horrid_thrift_client_error_diagnostics( $self->{client} ); | ||
| 174 | return 0; | ||
| 175 | } | ||
| 176 | |||
| 177 | return 1; | ||
| 178 | } | ||
| 179 | |||
| 180 | ########################################### | ||
| 181 | sub horrid_thrift_client_error_diagnostics { | ||
| 182 | ########################################### | ||
| 183 | my( $self, $client ) = @_; | ||
| 184 | |||
| 185 | # Apparently, there's no way to figure out what went wrong at the | ||
| 186 | # http level once a thrift call fails, except poking around in thrift's | ||
| 187 | # internal structures. Oh, the humanity! | ||
| 188 | |||
| 189 | eval { | ||
| 190 | my $in = $client->{input}->{trans}->{in}; | ||
| 191 | $in->setpos(0); | ||
| 192 | LOGWARN join '', <$in>; | ||
| 193 | }; | ||
| 194 | |||
| 195 | if( $@ ) { | ||
| 196 | LOGWARN "Unable to diagnose underlying error"; | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | 1; | ||
| 201 | |||
| 202 | __END__ | ||
| 203 | |||
| 204 | =head1 NAME | ||
| 205 | |||
| 206 | Net::Evernote::Simple - Simple interface to the Evernote API | ||
| 207 | |||
| 208 | =head1 SYNOPSIS | ||
| 209 | |||
| 210 | use Net::Evernote::Simple; | ||
| 211 | |||
| 212 | my $evernote = Net::Evernote::Simple->new( | ||
| 213 | # Obtain a developer token from Evernote and put it here | ||
| 214 | dev_token => "XXX", | ||
| 215 | ); | ||
| 216 | |||
| 217 | # check if our client API version still works | ||
| 218 | if( ! $evernote->version_check() ) { | ||
| 219 | print "Evernote API version out of date!\n"; | ||
| 220 | } | ||
| 221 | |||
| 222 | my $note_store = $evernote->note_store(); | ||
| 223 | |||
| 224 | if( !$note_store ) { | ||
| 225 | die "getting notestore failed: $@"; | ||
| 226 | } | ||
| 227 | |||
| 228 | # retrieve all of our notebooks | ||
| 229 | my $notebooks = | ||
| 230 | $note_store->listNotebooks( $evernote->dev_token() ); | ||
| 231 | |||
| 232 | for my $notebook ( @$notebooks ) { | ||
| 233 | print $notebook->name(), "\n"; | ||
| 234 | } | ||
| 235 | |||
| 236 | =head1 DESCRIPTION | ||
| 237 | |||
| 238 | Net::Evernote::Simple enables easy access to the Evernote API with developer | ||
| 239 | tokens. | ||
| 240 | |||
| 241 | Developer tokens allow read/write access to a user's Evernote data. | ||
| 242 | If you don't have a developer token yet, you can obtain one here: | ||
| 243 | |||
| 244 | http://dev.evernote.com/documentation/cloud/chapters/Authentication.php#devtoken | ||
| 245 | |||
| 246 | Net::Evernote::Simple then lets you obtain a note_store object which can | ||
| 247 | then be used with a variety of functions of the Evernote API described | ||
| 248 | here: | ||
| 249 | |||
| 250 | http://dev.evernote.com/documentation/cloud/chapters/ | ||
| 251 | |||
| 252 | =head1 METHODS | ||
| 253 | |||
| 254 | =over 4 | ||
| 255 | |||
| 256 | =item C<new()> | ||
| 257 | |||
| 258 | Constructor, creates a helper object to retrieve a note store object | ||
| 259 | later. To access Evernote data, you need a developer token and specify it | ||
| 260 | either with the C<dev_token> parameter in the constructor call: | ||
| 261 | |||
| 262 | my $evernote = Net::Evernote::Simple->new( | ||
| 263 | dev_token => "XXX", | ||
| 264 | ); | ||
| 265 | |||
| 266 | You can omit the C<dev_token> parameter and let Net::Evernote::Simple search | ||
| 267 | for a configuration file named C<~/.evernote.yml> containing the developer | ||
| 268 | token like | ||
| 269 | |||
| 270 | dev_token: XXX | ||
| 271 | |||
| 272 | within the YAML data. To specify an alternative YAML file, use | ||
| 273 | |||
| 274 | my $evernote = Net::Evernote::Simple->new( | ||
| 275 | config_file => "/path/to/evernote.yml", | ||
| 276 | ); | ||
| 277 | |||
| 278 | The object points to the Evernote production server by default. If you | ||
| 279 | want to use the sandbox instead, use | ||
| 280 | |||
| 281 | my $evernote = Net::Evernote::Simple->new( | ||
| 282 | evernote_host => "sandbox.evernote.com", | ||
| 283 | ); | ||
| 284 | |||
| 285 | =item C<version_check()> | ||
| 286 | |||
| 287 | Contact the Evernote API server and verify if the client API version | ||
| 288 | we're using is still supported. If this fails, please contact the author | ||
| 289 | of this module and ask to update the distribution on CPAN. | ||
| 290 | |||
| 291 | To make things easier for users of this module, the Evernote | ||
| 292 | |||
| 293 | =item C<dev_token()> | ||
| 294 | |||
| 295 | Return the value of the developer token. Many Evernote API functions need | ||
| 296 | the value of the token (like C<listNotebooks()> or C<createNote()>). | ||
| 297 | |||
| 298 | =item C<note_store()> | ||
| 299 | |||
| 300 | Obtain a note_store object from Evernote SDK, which allows you call | ||
| 301 | all sorts of Evernote data manipulation functions, like | ||
| 302 | C<listNotebooks()> or C<createNote()>. | ||
| 303 | |||
| 304 | =back | ||
| 305 | |||
| 306 | One thing to keep in mind when using this library is that the original | ||
| 307 | C<EDAMxxx> namespace of the Evernote API has been converted to | ||
| 308 | C<Net::Evernote::Simple::EDAMxxx> to avoid collisions. See the example | ||
| 309 | below to get an idea on how to use it. Consult the official Evernote | ||
| 310 | API documentation at the link listed above for details on data structures | ||
| 311 | used and parameters needed. | ||
| 312 | |||
| 313 | =head1 EXAMPLE | ||
| 314 | |||
| 315 | # add a note with a JPG image to notebook "Recipes" | ||
| 316 | |||
| 317 | #!/usr/local/bin/perl | ||
| 318 | use strict; | ||
| 319 | use warnings; | ||
| 320 | |||
| 321 | use Net::Evernote::Simple; | ||
| 322 | use Sysadm::Install qw( slurp ); | ||
| 323 | use Digest::MD5 qw( md5_hex ); | ||
| 324 | |||
| 325 | my( $jpg_file ) = @ARGV; | ||
| 326 | |||
| 327 | if( !defined $jpg_file ) { | ||
| 328 | die "usage: $0 jpg_file"; | ||
| 329 | } | ||
| 330 | |||
| 331 | my $evernote = Net::Evernote::Simple->new( | ||
| 332 | # Obtain a developer token from Evernote and put it here | ||
| 333 | # or use a ~/.evernote.yml file with a "dev_token" entry | ||
| 334 | # dev_token => "XXX", | ||
| 335 | ); | ||
| 336 | |||
| 337 | # check if our client API version still works | ||
| 338 | if( ! $evernote->version_check() ) { | ||
| 339 | die "Evernote API version out of date!"; | ||
| 340 | } | ||
| 341 | |||
| 342 | my $note_store = $evernote->note_store(); | ||
| 343 | |||
| 344 | if( !$note_store ) { | ||
| 345 | die "getting notestore failed: $@"; | ||
| 346 | } | ||
| 347 | |||
| 348 | # retrieve all of our notebooks | ||
| 349 | my $notebooks = | ||
| 350 | $note_store->listNotebooks( $evernote->dev_token() ); | ||
| 351 | |||
| 352 | my $notebook_guid; | ||
| 353 | |||
| 354 | # see if we can find one named "Recipes" | ||
| 355 | for my $notebook ( @$notebooks ) { | ||
| 356 | if( $notebook->name() eq "Recipes" ) { | ||
| 357 | $notebook_guid = $notebook->guid(); | ||
| 358 | last; | ||
| 359 | } | ||
| 360 | } | ||
| 361 | |||
| 362 | if( !defined $notebook_guid ) { | ||
| 363 | die "Notebook 'Recipes' not found"; | ||
| 364 | } | ||
| 365 | |||
| 366 | my $data = Net::Evernote::Simple::EDAMTypes::Data->new(); | ||
| 367 | |||
| 368 | my $content = slurp $jpg_file; | ||
| 369 | $data->body( $content ); | ||
| 370 | my $hash = md5_hex( $content ); | ||
| 371 | $data->bodyHash( $hash ); | ||
| 372 | $data->size( length $content ); | ||
| 373 | |||
| 374 | my $r = Net::Evernote::Simple::EDAMTypes::Resource->new(); | ||
| 375 | $r->data( $data ); | ||
| 376 | $r->mime( "image/jpeg" ); | ||
| 377 | $r->noteGuid( "" ); | ||
| 378 | |||
| 379 | my $note = Net::Evernote::Simple::EDAMTypes::Note->new(); | ||
| 380 | $note->title( "Our note title" ); | ||
| 381 | $note->resources( [$r] ); | ||
| 382 | $note->notebookGuid( $notebook_guid ); | ||
| 383 | |||
| 384 | my $enml = <<EOT; | ||
| 385 | <?xml version="1.0" encoding="UTF-8"?> | ||
| 386 | <!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"> | ||
| 387 | <en-note> | ||
| 388 | <en-media type="image/jpeg" hash="$hash"/> | ||
| 389 | </en-note> | ||
| 390 | EOT | ||
| 391 | |||
| 392 | $note->content( $enml ); | ||
| 393 | |||
| 394 | $note_store->createNote( | ||
| 395 | $evernote->dev_token(), $note ); | ||
| 396 | |||
| 397 | =head1 LEGALESE | ||
| 398 | |||
| 399 | Copyright 2012 by Mike Schilli, all rights reserved. | ||
| 400 | This program is free software, you can redistribute it and/or | ||
| 401 | modify it under the same terms as Perl itself. | ||
| 402 | |||
| 403 | =head1 AUTHOR | ||
| 404 | |||
| 405 | 2012, Mike Schilli <cpan@perlmeister.com> | ||