Skip to content

Commit 5b86567

Browse files
committed
Modify to download notes in all of the notebooks
1 parent 3a9eecb commit 5b86567

File tree

4 files changed

+52
-37
lines changed

4 files changed

+52
-37
lines changed

lib/ever2boost/cli.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@
33

44
module Ever2boost
55
class CLI < Thor
6+
DEFAULT_OUTPUT_DIR = "#{ENV['HOME']}/ever2boost_store"
7+
68
desc "import", "import from evernote"
79
def import
8-
developer_token = ask('DEVELOPER_TOKEN: ')
9-
EvernoteAuthorizer.new(developer_token).fetch_notes(100)
10+
developer_token = ask('DEVELOPER_TOKEN:')
11+
EvernoteAuthorizer.new(developer_token).fetch_notes(DEFAULT_OUTPUT_DIR)
12+
end
13+
14+
class << self
15+
def tell(message)
16+
tell(message)
17+
end
1018
end
1119
end
1220
end

lib/ever2boost/cson_generator.rb

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
module Ever2boost
22
class CsonGenerator
33
class << self
4-
DEFAULT_DIR_PATH = "#{ENV['HOME']}/ever2boost_store"
5-
6-
def generate(folder_hash, note)
4+
def generate(folder_hash, note, output_dir)
75
timestamp = self.timestamp
86
md_note_content = MdConverter.convert(note.content)
97
cson = <<-EOS
10-
type: "MARKDOWN_NOTE"
11-
folder: "#{folder_hash}"
12-
title: "#{note.title}"
13-
content:
14-
'''
15-
#{md_note_content}
16-
'''
17-
tags: []
18-
isStarred: false
19-
createdAt: "#{timestamp}"
20-
updatedAt: "#{timestamp}"
8+
type: "MARKDOWN_NOTE"
9+
folder: "#{folder_hash}"
10+
title: "#{note.title}"
11+
content: '''
12+
# #{note.title}
13+
#{md_note_content}
14+
'''
15+
tags: []
16+
isStarred: false
17+
createdAt: "#{timestamp}"
18+
updatedAt: "#{timestamp}"
2119
EOS
2220

23-
FileUtils.mkdir_p("#{DEFAULT_DIR_PATH}/notes") unless FileTest.exist?("#{DEFAULT_DIR_PATH}/notes")
24-
File.open("#{DEFAULT_DIR_PATH}/notes/#{note.file_name}.cson", "w") do |f|
21+
FileUtils.mkdir_p("#{output_dir}/notes") unless FileTest.exist?("#{output_dir}/notes")
22+
File.open("#{output_dir}/notes/#{note.file_name}.cson", "w") do |f|
2523
f.write(cson)
2624
end
2725
end

lib/ever2boost/evernote_authorizer.rb

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
module Ever2boost
77
class EvernoteAuthorizer
88
EVERNOTE_HOST = "www.evernote.com"
9-
DEFAULT_DIR_PATH = "#{ENV['HOME']}/ever2boost_store"
109

1110
attr_accessor :developer_token, :note_store
1211

@@ -23,27 +22,37 @@ def initialize(developer_token)
2322
@note_store = note_store
2423
end
2524

26-
def fetch_notes(number_of_notes)
27-
FileUtils.mkdir_p(DEFAULT_DIR_PATH) unless FileTest.exist?(DEFAULT_DIR_PATH)
25+
def fetch_notes(output_dir)
26+
FileUtils.mkdir_p(output_dir) unless FileTest.exist?(output_dir)
2827

29-
start_index = 0
3028
note_store = self.note_store
3129
notebook_list = note_store.listNotebooks(self.developer_token)
3230
notebook_guids = notebook_list.map(&:guid)
3331
lists = notebook_list.map { |nl| Ever2boost::NoteList.new(title: nl.name, hash: hash, guid: nl.guid) }
34-
Ever2boost::JsonGenerator.generate_boostnote_json(lists)
35-
filter = Evernote::EDAM::NoteStore::NoteFilter.new
36-
filter.notebookGuid = notebook_guids.first
37-
note_list = note_store.findNotes(self.developer_token, filter, start_index, number_of_notes)
38-
# TODO: note_listのtitleからboostnote.jsonを作成する。
39-
note_guids = note_list.notes.map(&:guid)
40-
# TODO: assign the booleans
41-
en_notes = note_guids.map {|note_guid| note_store.getNote(self.developer_token, note_guid, true, true, false, false)}
42-
notes = en_notes.map {|note| Note.new(title: note.title, content: note.content, notebook_guid: note.notebookGuid)}
43-
notes.each do |note|
44-
lists.each do |list|
45-
# TODO: break if note found
46-
CsonGenerator.generate(list.hash, note) if list.guid == note.notebook_guid
32+
Ever2boost::JsonGenerator.generate_boostnote_json(lists, output_dir)
33+
34+
notebook_guids.each do |notebook_guid|
35+
filter = Evernote::EDAM::NoteStore::NoteFilter.new
36+
filter.notebookGuid = notebook_guid
37+
38+
spec = Evernote::EDAM::NoteStore::NotesMetadataResultSpec.new(includeTitle: true, includeNotebookGuid: true)
39+
40+
# get latest 250 notes
41+
note_counts_hash = note_store.findNoteCounts(self.developer_token, filter, true).notebookCounts
42+
number_of_notes = note_counts_hash.values.last || 0
43+
# TODO: display message like "ignored first #{(number_of_notes - 250).to_s} notes due to EvernoteAPI access limitation" if number_of_notes > 250
44+
start_index = number_of_notes > 250 ? number_of_notes - 250 : 0
45+
note_list = note_store.findNotesMetadata(self.developer_token, filter, start_index, 15, spec)
46+
47+
note_guids = note_list.notes.map(&:guid)
48+
# TODO: assign the booleans
49+
en_notes = note_guids.map {|note_guid| note_store.getNote(self.developer_token, note_guid, true, true, false, false)}
50+
notes = en_notes.map {|note| Note.new(title: note.title, content: note.content, notebook_guid: note.notebookGuid)}
51+
notes.each do |note|
52+
lists.each do |list|
53+
# TODO: break if note found
54+
CsonGenerator.generate(list.hash, note, output_dir) if list.guid == note.notebook_guid
55+
end
4756
end
4857
end
4958
end

lib/ever2boost/json_generator.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class JsonGenerator
1111
].freeze
1212

1313
class << self
14-
def generate_boostnote_json(note_list)
14+
def generate_boostnote_json(note_list, output_dir)
1515
folders = <<-EOS
1616
EOS
1717

@@ -36,7 +36,7 @@ def generate_boostnote_json(note_list)
3636
}
3737
EOS
3838

39-
File.open("#{ENV['HOME']}/ever2boost_store/boostnote.json","w") do |f|
39+
File.open("#{output_dir}/boostnote.json","w") do |f|
4040
f.write(json)
4141
end
4242
end

0 commit comments

Comments
 (0)