| 1 | package Net::Evernote; | ||
| 2 | |||
| 3 | 4 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 4 | my $module_dir = $INC{'Net/Evernote.pm'}; | |
| 5 | 4 | $module_dir =~ s/Evernote\.pm$//; | |
| 6 | 4 | unshift @INC,$module_dir; | |
| 7 | } | ||
| 8 | |||
| 9 | 4 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 4 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 4 | ▶ | use Exception::Class ( |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 4 | 'Net::Evernote::EDAMTest::Exception::ExceptionWrapper', | |
| 13 | 'Net::Evernote::EDAMTest::Exception::FileIOError', | ||
| 14 | 4 | ); | |
| 15 | |||
| 16 | 4 | ▶ | use LWP::Protocol::https; # it is not needed to 'use' here, but it must be installed. |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | # if it is not installed, an error (Thrift::TException object) is to be thrown. | ||
| 18 | 4 | ▶ | use Thrift::HttpClient; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | 4 | ▶ | use Thrift::BinaryProtocol; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | 0 | ▶ | use Net::Evernote::EDAMTypes::Types; # you must do `use' Net::Evernote::EDAMTypes::Types and Net::Evernote::EDAMErrors::Types |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 21 | use Net::Evernote::EDAMErrors::Types; # before doing `use' Net::Evernote::EDAMUserStore::UserStore or Net::Evernote::EDAMNoteStore::NoteStore | ||
| 22 | use Net::Evernote::EDAMUserStore::UserStore; | ||
| 23 | use Net::Evernote::EDAMNoteStore::NoteStore; | ||
| 24 | use Net::Evernote::EDAMUserStore::Constants; | ||
| 25 | use Evernote::Note; | ||
| 26 | use Evernote::Tag; | ||
| 27 | use Evernote::Notebook; | ||
| 28 | our $VERSION = '0.081'; | ||
| 29 | |||
| 30 | sub new { | ||
| 31 | my ($class, $args) = @_; | ||
| 32 | my $authentication_token = $$args{authentication_token}; | ||
| 33 | my $debug = $ENV{DEBUG}; | ||
| 34 | my $evernote_host; | ||
| 35 | |||
| 36 | if ($$args{use_sandbox}) { | ||
| 37 | $evernote_host = 'sandbox.evernote.com'; | ||
| 38 | } else { | ||
| 39 | $evernote_host = 'www.evernote.com'; | ||
| 40 | } | ||
| 41 | |||
| 42 | my $user_store_url = 'https://' . $evernote_host . '/edam/user'; | ||
| 43 | my $result; | ||
| 44 | my $note_store; | ||
| 45 | |||
| 46 | eval { | ||
| 47 | |||
| 48 | local $SIG{__DIE__} = sub { | ||
| 49 | my ( $err ) = @_; | ||
| 50 | if ( not ( blessed $err && $err->isa('Exception::Class::Base') ) ) { | ||
| 51 | Net::Evernote::EDAMTest::Exception::ExceptionWrapper->throw( error => $err ); | ||
| 52 | } | ||
| 53 | }; | ||
| 54 | |||
| 55 | my $user_store_client = Thrift::HttpClient->new( $user_store_url ); | ||
| 56 | # default timeout value may be too short | ||
| 57 | $user_store_client->setSendTimeout( 2000 ); | ||
| 58 | $user_store_client->setRecvTimeout( 10000 ); | ||
| 59 | my $user_store_prot = Thrift::BinaryProtocol->new( $user_store_client ); | ||
| 60 | my $user_store = Net::Evernote::EDAMUserStore::UserStoreClient->new( $user_store_prot, $user_store_prot ); | ||
| 61 | |||
| 62 | my $version_ok = $user_store->checkVersion( 'Evernote Net::Evernote::EDAMTest (Perl)', | ||
| 63 | Net::Evernote::EDAMUserStore::Constants::EDAM_VERSION_MAJOR, | ||
| 64 | Net::Evernote::EDAMUserStore::Constants::EDAM_VERSION_MINOR ); | ||
| 65 | |||
| 66 | if ( not $version_ok ) { | ||
| 67 | printf "Evernote API version not up to date?\n"; | ||
| 68 | exit(1) | ||
| 69 | } | ||
| 70 | |||
| 71 | my $note_store_url = $user_store->getNoteStoreUrl( $authentication_token ); | ||
| 72 | |||
| 73 | warn "[INFO] note store url : $note_store_url \n" if $debug; | ||
| 74 | my $note_store_client = Thrift::HttpClient->new( $note_store_url ); | ||
| 75 | # default timeout value may be too short | ||
| 76 | $note_store_client->setSendTimeout( 2000 ); | ||
| 77 | $note_store_client->setRecvTimeout( 10000 ); | ||
| 78 | my $note_store_prot = Thrift::BinaryProtocol->new( $note_store_client ); | ||
| 79 | |||
| 80 | # search this class for API methods | ||
| 81 | $note_store = Net::Evernote::EDAMNoteStore::NoteStoreClient->new( $note_store_prot, $note_store_prot ); | ||
| 82 | }; | ||
| 83 | |||
| 84 | if ($@) { | ||
| 85 | my $err = $@; | ||
| 86 | die "Code: " . $$err{'code'} . ', ' . $$err{'message'}; | ||
| 87 | } | ||
| 88 | |||
| 89 | return bless { | ||
| 90 | debug => $debug, | ||
| 91 | _authentication_token => $authentication_token, | ||
| 92 | _notestore => $note_store, | ||
| 93 | _authenticated => 1, # safe to assume if we've gotten this far? | ||
| 94 | }, $class; | ||
| 95 | } | ||
| 96 | |||
| 97 | # TODO: make this is current | ||
| 98 | sub createNote { | ||
| 99 | my ($self, $args) = @_; | ||
| 100 | my $authentication_token = $self->{_authentication_token}; | ||
| 101 | my $client = $self->{_notestore}; | ||
| 102 | |||
| 103 | my $title = $$args{title}; | ||
| 104 | my $content = $$args{content}; | ||
| 105 | my $created = $$args{created}; | ||
| 106 | |||
| 107 | # support notebook name? | ||
| 108 | my $notebook_guid = $$args{notebook_guid}; | ||
| 109 | $content =~ s/\n/<br\/>/g; | ||
| 110 | |||
| 111 | my $cont_encoded =<<EOF; | ||
| 112 | <?xml version="1.0" encoding="UTF-8"?> | ||
| 113 | <!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"> | ||
| 114 | <en-note> | ||
| 115 | $content | ||
| 116 | </en-note> | ||
| 117 | EOF | ||
| 118 | |||
| 119 | my $note_args = { | ||
| 120 | title => $title, | ||
| 121 | content => $cont_encoded, | ||
| 122 | }; | ||
| 123 | |||
| 124 | $$note_args{created} = $created if $created; | ||
| 125 | $$note_args{notebookGuid} = $notebook_guid if $notebook_guid; | ||
| 126 | |||
| 127 | my $tags; | ||
| 128 | |||
| 129 | # if tag already exists, just uses it and doesn't overwrite it. | ||
| 130 | # Not sure if the eval is the best way to handle it but it seems | ||
| 131 | # to do the job | ||
| 132 | if (my $tag_args = $$args{tag_names}) { | ||
| 133 | # make sure this is an array ref | ||
| 134 | $tags = ref($tag_args) eq 'ARRAY' ? $tag_args: [$tag_args]; | ||
| 135 | map { | ||
| 136 | my $tag = Net::Evernote::EDAMTypes::Tag->new({ name => $_ }); | ||
| 137 | eval { | ||
| 138 | $client->createTag($authentication_token, $tag); | ||
| 139 | }; | ||
| 140 | |||
| 141 | if ($@) { | ||
| 142 | # dump 'em in a ditch | ||
| 143 | } | ||
| 144 | } @$tags; | ||
| 145 | |||
| 146 | $$note_args{tagNames} = $tags; | ||
| 147 | } | ||
| 148 | |||
| 149 | if (my $tag_guids = $$args{tag_guids}) { | ||
| 150 | my $guids = ref($tag_guids) eq 'ARRAY' ? $tag_guids : [$tag_guids]; | ||
| 151 | $$note_args{tagGuids} = $guids; | ||
| 152 | } | ||
| 153 | |||
| 154 | my $note = Net::Evernote::EDAMTypes::Note->new($note_args); | ||
| 155 | |||
| 156 | return Net::Evernote::Note->new({ | ||
| 157 | _obj => $client->createNote($authentication_token, $note), | ||
| 158 | _notestore => $self->{_notestore}, | ||
| 159 | _authentication_token => $authentication_token, | ||
| 160 | }); | ||
| 161 | } | ||
| 162 | |||
| 163 | sub deleteNote { | ||
| 164 | my ($self, $args) = @_; | ||
| 165 | my $guid = $$args{guid}; | ||
| 166 | |||
| 167 | my $authToken = $self->{_authentication_token}; | ||
| 168 | my $client = $self->{_notestore}; | ||
| 169 | |||
| 170 | $client->deleteNote($authToken,$guid); | ||
| 171 | } | ||
| 172 | |||
| 173 | sub getNote { | ||
| 174 | my ($self, $args) = @_; | ||
| 175 | my $guid = $$args{guid}; | ||
| 176 | |||
| 177 | my $client = $self->{_notestore}; | ||
| 178 | my $authentication_token = $self->{_authentication_token}; | ||
| 179 | |||
| 180 | return Net::Evernote::Note->new({ | ||
| 181 | _obj => $client->getNote($authentication_token, $guid, 1), | ||
| 182 | _notestore => $self->{_notestore}, | ||
| 183 | _authentication_token => $authentication_token, | ||
| 184 | }); | ||
| 185 | } | ||
| 186 | |||
| 187 | sub getNotebook { | ||
| 188 | my ($self, $args) = @_; | ||
| 189 | my $guid = $$args{guid}; | ||
| 190 | |||
| 191 | my $client = $self->{_notestore}; | ||
| 192 | my $authentication_token = $self->{_authentication_token}; | ||
| 193 | |||
| 194 | my $notebook; | ||
| 195 | eval { | ||
| 196 | $notebook = Net::Evernote::Notebook->new({ | ||
| 197 | _obj => $client->getNotebook($authentication_token, $guid, 1), | ||
| 198 | _notestore => $self->{_notestore}, | ||
| 199 | _authentication_token => $authentication_token, | ||
| 200 | }); | ||
| 201 | }; | ||
| 202 | |||
| 203 | if (my $error = $@) { | ||
| 204 | # notebook not found | ||
| 205 | if (ref($error) eq 'Net::Evernote::EDAMNotFoundException') { | ||
| 206 | return; | ||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | return $notebook; | ||
| 211 | } | ||
| 212 | |||
| 213 | sub findNotes { | ||
| 214 | my ($self, $args) = @_; | ||
| 215 | my $string = $$args{string}; | ||
| 216 | my $offset = $$args{offset} || 0; | ||
| 217 | my $maxNotes = $$args{maxCount} || 1; | ||
| 218 | |||
| 219 | my $authentication_token = $self->{_authentication_token}; | ||
| 220 | |||
| 221 | my $stru = Net::Evernote::EDAMNoteStore::NoteFilter->new({ words => $string }); | ||
| 222 | my $client = $self->{_notestore}; | ||
| 223 | |||
| 224 | return $client->findNotes($authentication_token,$stru,$offset,$maxNotes); | ||
| 225 | } | ||
| 226 | |||
| 227 | sub listNotebooks { | ||
| 228 | my $self = shift; | ||
| 229 | my $client = $self->{_notestore}; | ||
| 230 | return $client->listNotebooks($self->{_authentication_token}); | ||
| 231 | } | ||
| 232 | |||
| 233 | sub createNotebook { | ||
| 234 | my ($self, $args) = @_; | ||
| 235 | my $client = $self->{_notestore}; | ||
| 236 | my $notebook = Net::Evernote::EDAMTypes::Notebook->new({ | ||
| 237 | name => $$args{name}, | ||
| 238 | }); | ||
| 239 | |||
| 240 | return Net::Evernote::Notebook->new({ | ||
| 241 | _obj => $client->createNotebook($self->{_authentication_token}, $notebook), | ||
| 242 | _notestore => $self->{_notestore}, | ||
| 243 | }); | ||
| 244 | } | ||
| 245 | |||
| 246 | sub deleteNotebook { | ||
| 247 | my ($self, $args) = @_; | ||
| 248 | my $guid = $$args{guid}; | ||
| 249 | |||
| 250 | my $authToken = $self->{_authentication_token}; | ||
| 251 | my $client = $self->{_notestore}; | ||
| 252 | |||
| 253 | return $client->expungeNotebook($authToken,$guid); | ||
| 254 | } | ||
| 255 | |||
| 256 | sub authenticated { | ||
| 257 | my $self = shift; | ||
| 258 | return $self->{_authenticated}; | ||
| 259 | } | ||
| 260 | |||
| 261 | sub createTag { | ||
| 262 | my ($self, $args) = @_; | ||
| 263 | my $authentication_token = $self->{_authentication_token}; | ||
| 264 | my $client = $self->{_notestore}; | ||
| 265 | |||
| 266 | my $name = $$args{name}; | ||
| 267 | |||
| 268 | die "Name required to create tag\n" if !$name; | ||
| 269 | |||
| 270 | my $tag = Net::Evernote::EDAMTypes::Tag->new({ name => $name }); | ||
| 271 | |||
| 272 | return Net::Evernote::Tag->new({ | ||
| 273 | _obj => $client->createTag($authentication_token, $tag), | ||
| 274 | _notestore => $self->{_notestore}, | ||
| 275 | _authentication_token => $authentication_token, | ||
| 276 | }); | ||
| 277 | } | ||
| 278 | |||
| 279 | sub getTag { | ||
| 280 | my ($self, $args) = @_; | ||
| 281 | my $guid = $$args{guid}; | ||
| 282 | |||
| 283 | my $client = $self->{_notestore}; | ||
| 284 | my $authentication_token = $self->{_authentication_token}; | ||
| 285 | |||
| 286 | my $tag; | ||
| 287 | |||
| 288 | eval { | ||
| 289 | $tag = Net::Evernote::Tag->new({ | ||
| 290 | _obj => $client->getTag($authentication_token, $guid, 1), | ||
| 291 | _notestore => $self->{_notestore}, | ||
| 292 | _authentication_token => $authentication_token, | ||
| 293 | }); | ||
| 294 | }; | ||
| 295 | |||
| 296 | if (my $error = $@) { | ||
| 297 | # tag not found | ||
| 298 | if (ref($error) eq 'Net::Evernote::EDAMNotFoundException') { | ||
| 299 | return; | ||
| 300 | } | ||
| 301 | } | ||
| 302 | |||
| 303 | return $tag; | ||
| 304 | |||
| 305 | } | ||
| 306 | |||
| 307 | sub deleteTag { | ||
| 308 | my ($self, $args) = @_; | ||
| 309 | my $ns = $self->{_notestore}; | ||
| 310 | |||
| 311 | # FIXME: IS THIS EVEN POSSIBLE? | ||
| 312 | # I don't see any code for this yet in Net::Evernote::EDAMNoteStore::NoteStore.pm | ||
| 313 | |||
| 314 | } | ||
| 315 | |||
| 316 | 1; | ||
| 317 | |||
| 318 | =head1 NAME | ||
| 319 | |||
| 320 | Net::Evernote - Perl API for Evernote | ||
| 321 | |||
| 322 | =head1 VERSION | ||
| 323 | |||
| 324 | Version 0.06 | ||
| 325 | |||
| 326 | |||
| 327 | =head1 SYNOPSIS | ||
| 328 | |||
| 329 | use Net::Evernote; | ||
| 330 | |||
| 331 | my $evernote = Net::Evernote->new({ | ||
| 332 | authentication_token => $authentication_token | ||
| 333 | }); | ||
| 334 | |||
| 335 | # write a note | ||
| 336 | my $res = $evernote->createNote({ | ||
| 337 | title => $title, | ||
| 338 | content => $content | ||
| 339 | }); | ||
| 340 | |||
| 341 | my $guid = $res->guid; | ||
| 342 | |||
| 343 | # get the note | ||
| 344 | my $thisNote = $evernote->getNote({ guid => $guid }); | ||
| 345 | print $thisNote->title,"\n"; | ||
| 346 | print $thisNote->content,"\n"; | ||
| 347 | |||
| 348 | # delete the note | ||
| 349 | $evernote->deleteNote({ guid => $guid }); | ||
| 350 | |||
| 351 | # find notes | ||
| 352 | my $search = $evernote->findNotes({ keywords => $keywords, offset => $offset, max_notes => 5 }); | ||
| 353 | for my $thisNote ( @{$search->notes} ) { | ||
| 354 | print $thisNote->guid,"\n"; | ||
| 355 | print $thisNote->title,"\n"; | ||
| 356 | } | ||
| 357 | |||
| 358 | =head1 METHODS | ||
| 359 | |||
| 360 | More docs coming for this. | ||
| 361 | |||
| 362 | =head1 SEE ALSO | ||
| 363 | |||
| 364 | http://www.evernote.com/about/developer/api/ | ||
| 365 | |||
| 366 | |||
| 367 | =head1 AUTHOR | ||
| 368 | |||
| 369 | David Collins <davidcollins4481@gmail.com> | ||
| 370 | |||
| 371 | =head1 BUGS/LIMITATIONS | ||
| 372 | |||
| 373 | If you have found bugs, please send email to <davidcollins4481@gmail.com> | ||
| 374 | |||
| 375 | |||
| 376 | =head1 SUPPORT | ||
| 377 | |||
| 378 | You can find documentation for this module with the perldoc command. | ||
| 379 | |||
| 380 | perldoc Net::Evernote | ||
| 381 | |||
| 382 | |||
| 383 | =head1 COPYRIGHT & LICENSE | ||
| 384 | |||
| 385 | Copyright 2013 David Collins, all rights reserved. | ||
| 386 | |||
| 387 | This program is free software; you can redistribute it and/or modify | ||
| 388 | it under the same terms as Perl itself. | ||