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


※上記の広告は60日以上更新のないWIKIに表示されています。更新することで広告が下部へ移動します。

EvernoteによくPDFを置いておくのだけど、Macに公式のclientを入れておくと全部同期するので容量取るのでじゃま。でもWeb clientは使いにくい。uploadだけでも簡単にやりたかったのでつくった。そのうち検索もつくりたい。

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

pedf2evernote.rb

  1. #!/usr/bin/ruby
  2. # Load libraries required by the Evernote OAuth sample applications
  3. require 'oauth'
  4. require 'oauth/consumer'
  5.  
  6. # Load Thrift & Evernote Ruby libraries
  7. require "evernote_oauth"
  8.  
  9. require "./make_note.rb"
  10.  
  11.  
  12. def developer_token
  13. YOUR_DEVELOPER_TOKEN
  14. end
  15.  
  16.  
  17. client = EvernoteOAuth::Client.new(
  18. token: developer_token,
  19. # sandbox: true
  20. sandbox: false
  21. )
  22.  
  23. #user_store = client.user_store
  24. #user_store.getUser(developer_token)
  25.  
  26. note_store = client.note_store
  27. notebooks = note_store.listNotebooks()
  28.  
  29.  
  30. notebooks.each do |notebook|
  31. puts "Notebook: #{notebook.name}"
  32. end
  33.  
  34. filename1 = ARGV[0]
  35. file1 = File.open(filename1,"rb"){ |io| io.read }
  36. data1 = Evernote::EDAM::Type::Data.new()
  37. data1.body = file1
  38. resource1 = Evernote::EDAM::Type::Resource.new()
  39. resource1.mime = "application/pdf"
  40. resource1.data = data1
  41. resource1.attributes = Evernote::EDAM::Type::ResourceAttributes.new()
  42. resource1.attributes.fileName = filename1
  43.  
  44. mdlsrslt1=`mdls -name kMDItemWhereFroms #{filename1}`
  45. sourceURL1 = mdlsrslt1.split("\"")[1]
  46.  
  47. resources = [resource1]
  48. sourceURLs = [sourceURL1]
  49.  
  50. comment=ARGV[1]
  51.  
  52. created = `mdls -name kMDItemContentCreationDate #{filename1}`
  53. lastmod = `mdls -name kMDItemContentModificationDate #{filename1}`
  54. lastused = `mdls -name kMDItemLastUsedDate #{filename1}`
  55.  
  56. 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."
  57.  
  58. make_note(note_store,filename1, comment, resources, sourceURLs, mdls)

make_note.rb

  1. def make_note(note_store, note_title, note_body, resources=[], sourceURLs=[], mdls)
  2. parent_notebook=nil,
  3. #Create a Note instance with title and body
  4. # Send Note object to user's account
  5.  
  6. our_note = Evernote::EDAM::Type::Note.new
  7. our_note.title = note_title
  8.  
  9. ## Build body of note
  10.  
  11. n_body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  12. n_body += "<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">"
  13. n_body += "<en-note>#{note_body}"
  14.  
  15. unless resources.empty?
  16. ### Add Resource objects to note body
  17. n_body += "<br /><br />"
  18. our_note.resources = resources
  19. resources.each do |resource|
  20. hash_func = Digest::MD5.new
  21. hexhash = hash_func.hexdigest(resource.data.body)
  22. # n_body += "Attachment with hash #{hexhash}: <br /><en-media type=\"#{resource.mime}\" hash=\"#{hexhash}\" /><br />"
  23. n_body += "<br /><en-media type=\"#{resource.mime}\" hash=\"#{hexhash}\" /><br />"
  24. end
  25. end
  26.  
  27. n_body += "<br /><br />"
  28. n_body += mdls
  29. n_body += "</en-note>"
  30.  
  31. our_note.content = n_body
  32.  
  33. our_note.tagNames = ["PDFUploader"]
  34. our_note.attributes = Evernote::EDAM::Type::NoteAttributes.new()
  35. our_note.attributes.sourceURL = sourceURLs[0]
  36.  
  37.  
  38. ## Attempt to create note in Evernote account
  39. begin
  40. note = note_store.createNote(our_note)
  41. rescue Evernote::EDAM::Error::EDAMUserException => edue
  42. ## Something was wrong with the note data
  43. ## See EDAMErrorCode enumeration for error code explanation
  44. ## http://dev.evernote.com/documentation/reference/Errors.html#Enum_EDAMErrorCode
  45. puts "EDAMUserException: #{edue}"
  46. rescue Evernote::EDAM::Error::EDAMNotFoundException => ednfe
  47. ## Parent Notebook GUID doesn't correspond to an actual notebook
  48. puts "EDAMNotFoundException: Invalid parent notebook GUID"
  49. end
  50.  
  51. ## Return created note object
  52. note
  53.  
  54. end

参考リンク


コメント

名前:
コメント:

すべてのコメントを見る