Skip to content

Instantly share code, notes, and snippets.

@pn11
Last active June 14, 2016 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pn11/0f82687ee48e477a47f268071d6a4e36 to your computer and use it in GitHub Desktop.
Save pn11/0f82687ee48e477a47f268071d6a4e36 to your computer and use it in GitHub Desktop.
Evernote API を Ruby でいじる

2014年7月に以下のようなスクリプトを書いて、PDF を Evernote にアップロードしていた。 そのうち動かなくなって放置していたので直したい。 単に developper token が expire してただけだった。というわけでちゃんと動く。

pdf2evernote.rb

#!/usr/bin/ruby
# Load libraries required by the Evernote OAuth sample applications
require 'oauth'
require 'oauth/consumer'

# Load Thrift & Evernote Ruby libraries
require "evernote_oauth"

require "make_note.rb"

def developer_token 
 "hoge"  # this is developper token for real account
end


client = EvernoteOAuth::Client.new(
  token: developer_token, 
  sandbox: false
)

note_store = client.note_store
notebooks = note_store.listNotebooks()


notebooks.each do |notebook|
 puts "Notebook: #{notebook.name}" 
end

filename1 = ARGV[0]
file1 = File.open(filename1,"rb"){ |io| io.read }
data1 = Evernote::EDAM::Type::Data.new()
data1.body = file1
resource1 = Evernote::EDAM::Type::Resource.new()
resource1.mime = "application/pdf"
resource1.data = data1
resource1.attributes = Evernote::EDAM::Type::ResourceAttributes.new()
resource1.attributes.fileName = filename1

mdlsrslt1=`mdls -name kMDItemWhereFroms #{filename1}`
sourceURL1 = mdlsrslt1.split("\"")[1]

resources = [resource1]
sourceURLs = [sourceURL1]

comment=ARGV[1]

created = `mdls -name kMDItemContentCreationDate #{filename1}`
lastmod = `mdls -name kMDItemContentModificationDate #{filename1}`
lastused =  `mdls -name kMDItemLastUsedDate #{filename1}`

mdls = "This PDF was :<br /> created at #{created.split("=")[1]} <br /> modified at#{lastmod.split("=")[1]} <br /> Last used at#{lastused.split("=")[1]}<br /> on PC from mdls information."

make_note(note_store,filename1, comment, resources, sourceURLs, mdls)

ここで、呼び出している make_note.rb はこんな感じ。

def make_note(note_store, note_title, note_body, resources=[],  sourceURLs=[], mdls)
 parent_notebook=nil,
  #Create a Note instance with title and body 
  # Send Note object to user's account
 
  our_note = Evernote::EDAM::Type::Note.new
  our_note.title = note_title
 
  ## Build body of note
 
  n_body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  n_body += "<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">"
  n_body += "<en-note>#{note_body}"
  
  unless resources.empty?
    ### Add Resource objects to note body
    n_body += "<br /><br />"
    our_note.resources = resources
    resources.each do |resource|
      hash_func = Digest::MD5.new
      hexhash = hash_func.hexdigest(resource.data.body)
#      n_body += "Attachment with hash #{hexhash}: <br /><en-media type=\"#{resource.mime}\" hash=\"#{hexhash}\" /><br />"
      n_body += "<br /><en-media type=\"#{resource.mime}\" hash=\"#{hexhash}\" /><br />"
    end
  end

  n_body += "<br /><br />"
  n_body += mdls
  n_body += "</en-note>"
 
  our_note.content = n_body
  
  our_note.tagNames = ["PDFUploader"]
  our_note.attributes = Evernote::EDAM::Type::NoteAttributes.new()
  our_note.attributes.sourceURL = sourceURLs[0]

 
  ## Attempt to create note in Evernote account
  begin
    note = note_store.createNote(our_note)
  rescue Evernote::EDAM::Error::EDAMUserException => edue
    ## Something was wrong with the note data
    ## See EDAMErrorCode enumeration for error code explanation
    ## http://dev.evernote.com/documentation/reference/Errors.html#Enum_EDAMErrorCode
    puts "EDAMUserException: #{edue}"
  rescue Evernote::EDAM::Error::EDAMNotFoundException => ednfe
    ## Parent Notebook GUID doesn't correspond to an actual notebook
    puts "EDAMNotFoundException: Invalid parent notebook GUID"
  end
 
  ## Return created note object
  note
 
end

実行すると

/Library/Ruby/Gems/2.0.0/gems/evernote-thrift-1.25.2/lib/Evernote/EDAM/user_store.rb:212:in `recv_getNoteStoreUrl': Evernote::EDAM::Error::EDAMUserException
	from /Library/Ruby/Gems/2.0.0/gems/evernote-thrift-1.25.2/lib/Evernote/EDAM/user_store.rb:202:in `getNoteStoreUrl'
	from /Library/Ruby/Gems/2.0.0/gems/evernote_oauth-0.2.3/lib/evernote_oauth/thrift_client_delegation.rb:11:in `method_missing'
	from /Library/Ruby/Gems/2.0.0/gems/evernote_oauth-0.2.3/lib/evernote_oauth/note_store.rb:13:in `note_store'
	from pdf2evernote.rb:42:in `<main>'

と言われる。 と言われたら、developper token が期限切れの可能性がある。1年経ったら取り直さないといけない。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment