ruby evernote_to_quiver.rb

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby evernote_to_quiver.rb相关的知识,希望对你有一定的参考价值。

gem 'quiver_note'

class MyEvernote
  NOTE_NAME = 'public'
  USERNAME = 'kuboon'

   def client
     @client ||= EvernoteOAuth::Client.new(token: auth_token, sandbox: false)
   end

   def note_store
     @note_store ||= client.note_store
   end

   def notebooks
     @notebooks ||= note_store.listNotebooks(auth_token)
   end

  def notebook
    @notebook ||= notebooks.find{|nb| nb.name == NOTE_NAME}
  end

  def notes(slug = nil)
    @notes ||= begin
      filter = Evernote::EDAM::NoteStore::NoteFilter.new
      filter.order = Evernote::EDAM::Type::NoteSortOrder::UPDATED
      filter.notebookGuid = notebook.guid
      filter.words = "#{slug}" if slug
      notes_q = note_store.findNotes auth_token, filter, 0, 1000
      notes_q.notes
    end
  end

  def note(guid)
    note_store.getNote(auth_token, guid, true, false, false, false)
  end

  def user_info
    @user_info ||= client.user_store.getPublicUserInfo(USERNAME)
  end

  def note_resource(note_guid, hash)
    note_store.getResourceByHash(auth_token, note_guid, hash, true, false, false)
  end

  def resource_uri_base
    user_info.webApiUrlPrefix.gsub('https:', 'http:') + 'res/'
  end


  private

  def auth_token
     Rails.application.secrets.evernote_token
  end
end

def get_markdown(content, media_map)
  doc = Nokogiri::XML.parse(content)
  doc.css('en-media').each do |node|
    url = "https://hogehoge.cloudfront.net/#{media_map[node[:hash]]}"
    node.replace "![](#{url})"
  end
  doc.search('br').each do |br|
    br.replace("\n")
  end
  doc.search('div').each do |div|
    div.add_child(Nokogiri::XML::Text.new("\n", doc))
    div.content
  end
  doc.text
end

root = Quiver.local('/sync/Dropbox/アプリ/quiver_blog/Quiver.qvlibrary')
notebook = root.notebook('blog')

ev = MyEvernote.new
ev.notes.map do |note|
  media_map = {}
  note.resources.to_a.each do |resource|
    hash = resource.data.bodyHash.unpack('H*')[0]
    media_map[hash] = resource.guid
  end

  content = {title: note.title, cells: [{type: :markdown, data: get_markdown(note.content, media_map)}]}
  meta = {title: note.title, tags: note.tags.map(&:name), created_at: note.created/1000, updated_at: note.updated/1000, uuid: note.guid.upcase}
  note = Quiver::Note.new(content: content, meta: meta)
  notebook.add(note)
end

以上是关于ruby evernote_to_quiver.rb的主要内容,如果未能解决你的问题,请参考以下文章

Ruby 25 岁了!Ruby 之父说 Ruby 3 有望 3 倍提速

如何学习ruby?Ruby学习技巧分享

ruby Ruby脚本,看看是否用openssl编译了ruby

什么是ruby?

ruby和ruby ee

ruby入门知识:了解ruby历史及特性