Class: Ever2boost::EvernoteAuthorizer

Inherits:
Object
  • Object
show all
Defined in:
lib/ever2boost/evernote_authorizer.rb

Constant Summary collapse

EVERNOTE_HOST =
'www.evernote.com'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(developer_token) ⇒ EvernoteAuthorizer

Returns a new instance of EvernoteAuthorizer.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ever2boost/evernote_authorizer.rb', line 12

def initialize(developer_token)
  user_store_url = "https://#{EVERNOTE_HOST}/edam/user"
  user_store_transport = Thrift::HTTPClientTransport.new(user_store_url)
  user_store_protocol = Thrift::BinaryProtocol.new(user_store_transport)
  user_store = Evernote::EDAM::UserStore::UserStore::Client.new(user_store_protocol)
  note_store_url = user_store.getNoteStoreUrl(developer_token)
  note_store_transport = Thrift::HTTPClientTransport.new(note_store_url)
  note_store_protocol = Thrift::BinaryProtocol.new(note_store_transport)
  note_store = Evernote::EDAM::NoteStore::NoteStore::Client.new(note_store_protocol)
  @developer_token = developer_token
  @note_store = note_store
rescue => e
  abort_with_message e
end

Instance Attribute Details

#developer_tokenObject

Returns the value of attribute developer_token.



10
11
12
# File 'lib/ever2boost/evernote_authorizer.rb', line 10

def developer_token
  @developer_token
end

#note_storeObject

Returns the value of attribute note_store.



10
11
12
# File 'lib/ever2boost/evernote_authorizer.rb', line 10

def note_store
  @note_store
end

Instance Method Details

#abort_with_message(exception) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/ever2boost/evernote_authorizer.rb', line 85

def abort_with_message(exception)
  if exception.class == Evernote::EDAM::Error::EDAMUserException
    abort Util.red_output('Error! Confirm your developer token.')
  elsif exception.class == Evernote::EDAM::Error::EDAMSystemException
    abort Util.red_output("Error! You reached EvernoteAPI rate limitation.\nThe notes processed so far have been created successfully.\nMore information: https://github.com/BoostIO/ever2boost/tree/master/docs/api_error.md")
  else
    raise exception
  end
end

#download_image(en_note, output_dir) ⇒ Object

TODO: handle to not image file



102
103
104
105
106
107
108
109
110
111
# File 'lib/ever2boost/evernote_authorizer.rb', line 102

def download_image(en_note, output_dir)
  en_note.resources.each do |resource|
    imagename = resource.data.bodyHash.unpack("H*").first
    extension = resource.mime.gsub(/(.+?)\//, '')
    Util.make_images_dir(output_dir)
    File.open("#{output_dir}/images/#{imagename}.#{extension}", 'w+b' ) do |f|
      f.write note_store.getResourceData(developer_token, resource.guid)
    end
  end
end

#fetch_notebook_listObject



27
28
29
# File 'lib/ever2boost/evernote_authorizer.rb', line 27

def fetch_notebook_list
  note_store.listNotebooks(developer_token)
end

#fetch_notes(filter) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/ever2boost/evernote_authorizer.rb', line 45

def fetch_notes(filter)
  spec = Evernote::EDAM::NoteStore::NotesMetadataResultSpec.new(includeTitle: true, includeNotebookGuid: true)
  number_of_note = self.number_of_note(filter)

  warn Util.yellow_output("Ignore first #{(number_of_note - 250)} notes due to EvernoteAPI access limitation in this notebook.") if number_of_note > 250
  start_index = number_of_note > 250 ? number_of_note - 250 : 0
  note_store.(developer_token, filter, start_index, number_of_note, spec)
end

#find_notebook_by_guid_from_notebook_list(notebook_list, note) ⇒ Object



95
96
97
98
99
# File 'lib/ever2boost/evernote_authorizer.rb', line 95

def find_notebook_by_guid_from_notebook_list(notebook_list, note)
  notebook_list.map do |nl|
    nl if note.notebook_guid == nl.guid
  end.compact.first
end

#import(output_dir) ⇒ Object

Download the all of notes from Evernote and generate Boostnote storage from it TODO: move this method to CLI



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ever2boost/evernote_authorizer.rb', line 56

def import(output_dir)
  puts 'processing...'
  FileUtils.mkdir_p(output_dir) unless FileTest.exist?(output_dir)
  notebook_list = self.notebook_list

  Ever2boost::JsonGenerator.output(notebook_list, output_dir)

  notebook_guids.map do |notebook_guid|
    filter = Evernote::EDAM::NoteStore::NoteFilter.new(notebookGuid: notebook_guid)
    note_guids = fetch_notes(filter).notes.map(&:guid)
    puts "importing #{note_store.getNotebook(developer_token, notebook_guid).name}"
    # TODO: assign the booleans
    en_notes = note_guids.map { |note_guid| note_store.getNote(developer_token, note_guid, true, true, true, false) }
    en_notes.each do |en_note|
      download_image(en_note, output_dir) unless en_note.resources.nil?
      note = Note.new(title: en_note.title, content: en_note.content, notebook_guid: en_note.notebookGuid, output_dir: output_dir)
      # puts "importing #{find_notebook_by_guid_from_notebook_list(notebook_list, note).title}"
      notebook_list.each do |list|
        # TODO: break if note not found
        CsonGenerator.output(list.hash, note, output_dir) if list.guid == note.notebook_guid
      end
    end
  end
  puts Util.green_output('Successfully finished!')
  puts Util.green_output("Imported notes are located at #{output_dir}, mount it to Boostnote!")
rescue => e
  abort_with_message e
end

#notebook_guidsObject



31
32
33
# File 'lib/ever2boost/evernote_authorizer.rb', line 31

def notebook_guids
  fetch_notebook_list.map(&:guid)
end

#notebook_listObject



35
36
37
38
# File 'lib/ever2boost/evernote_authorizer.rb', line 35

def notebook_list
  guids = notebook_guids
  fetch_notebook_list.map { |nl| Ever2boost::NoteList.new(title: nl.name, guid: nl.guid) }
end

#number_of_note(filter) ⇒ Object



40
41
42
43
# File 'lib/ever2boost/evernote_authorizer.rb', line 40

def number_of_note(filter)
  note_counts_hash = note_store.findNoteCounts(developer_token, filter, true).notebookCounts
  note_counts_hash.nil? ? 0 : note_counts_hash.values.last
end