部門 > Ruby > Evernote APIを使う > PDFから新規ノート作成

「部門/Ruby/Evernote APIを使う/PDFから新規ノート作成」の編集履歴(バックアップ)一覧に戻る

部門/Ruby/Evernote APIを使う/PDFから新規ノート作成 - (2014/06/01 (日) 15:34:51) のソース

解説はなしで。めもとして置いておく。ほとんどコピペ。Macのmdlsコマンドを使ってる。sandbox=trueかfalseをちゃんと指定しないと動かないっぽい。Oauth使うのめんどうだったのでdeveloper tokenを使った。

pedf2evernote.rb
#highlight(ruby,linenumber){{#!/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 
 YOUR_DEVELOPER_TOKEN
end


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

#user_store =  client.user_store
#user_store.getUser(developer_token)

note_store = client.note_store
#notebooks = note_store.listNotebooks(developer_token)
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
#highlight(ruby,linenumber){{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}}

*参考リンク


*コメント
#comment_num2()