k2e 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 78cad70f7337eb6274941d2f74b0f9790838ee8d455a2aefd101aa0eedf7d121
4
+ data.tar.gz: '079f2cab20e444ac29959ec546fc0dd2f429b4a952a64407f71d14dc478ee0d3'
5
+ SHA512:
6
+ metadata.gz: 1864e78cc5812ec68181d7dcbdfcbcf46486e808fb4ff35708093be64e74c8f20585a88ddffde0e42b7b53d1042d34bee33bea7fcc8439728484741091727125
7
+ data.tar.gz: 9122d1bdf2c0778d859ec63a3800464e5409831e175051a0830395add20a730064236b385319d4a9489ba3ea2096ddb2605a97842451c7e9f685ff009cbd680f
data/.env_example ADDED
@@ -0,0 +1,3 @@
1
+ NOTEBOOK="Kindle"
2
+ EVERNOTE_DEV_TOKEN="EVERNOTE_DEV_TOKEN"
3
+ K2E_DATA_LOCATION="/Users/ll/Dropbox/App/k2e/last_syn_date.txt"
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ k2e-*.gem
10
+ .env
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in k2e.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,36 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ k2e (0.1.0)
5
+ dotenv (~> 2.6.0, >= 2.6.0)
6
+ evernote_oauth (~> 0.2.3)
7
+ kindleclippings (~> 1.4, >= 1.4.0)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ coderay (1.1.2)
13
+ dotenv (2.6.0)
14
+ evernote-thrift (1.25.2)
15
+ evernote_oauth (0.2.3)
16
+ evernote-thrift
17
+ oauth (>= 0.4.1)
18
+ kindleclippings (1.4.0)
19
+ method_source (0.9.2)
20
+ oauth (0.5.4)
21
+ pry (0.12.2)
22
+ coderay (~> 1.1.0)
23
+ method_source (~> 0.9.0)
24
+ rake (10.5.0)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ bundler (~> 1.16)
31
+ k2e!
32
+ pry (~> 0.12.2)
33
+ rake (~> 10.0)
34
+
35
+ BUNDLED WITH
36
+ 1.16.6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 ttuan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # Kindle to Evernote
2
+
3
+ A tool allows you sync Kindle highlights to Evernote
4
+
5
+ ## Installation
6
+
7
+ ```ruby
8
+ gem install k2e
9
+ ```
10
+
11
+ ## Setup
12
+
13
+ To start, you need to config below environment variables to your shell config
14
+ file (`~/.zshrc`, `~/.bashrc`, ..):
15
+
16
+ ```
17
+ # Notebook name, which stores Kindle highlights
18
+ NOTEBOOK="Kindle"
19
+ # Evernote dev token, get it here: https://www.evernote.com/api/DeveloperToken.action
20
+ EVERNOTE_DEV_TOKEN="EVERNOTE_DEV_TOKEN"
21
+ # Optional.
22
+ # We need to store last sync date, to avoid duplicated data. Storing this date allow you use synchronize command anywhere :D
23
+ # Default path is "~/.k2e/last_syn_date.txt
24
+ K2E_DATA_LOCATION="/Users/ll/Dropbox/App/k2e/last_syn_date.txt"
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ 1. Plug Kindle to your computer.
30
+ 2. Open terminal, run this command: `k2e`
31
+
32
+ If you are using Mac OS, you can follow [this
33
+ guide](http://www.jamierubin.net/2014/03/19/going-paperless-prototype-automatically-send-kindle-notes-and-highlights-to-evernote/), using Keyboard Maestro to trigger Kindle pluging action.
34
+
35
+
36
+ ## Contributing
37
+
38
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ttuan/k2e.
39
+
40
+ ## License
41
+
42
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/k2e ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "k2e"
5
+
6
+ if ENV["NOTEBOOK"].nil? || ENV["EVERNOTE_DEV_TOKEN"].nil?
7
+ puts "Please config ENV variables!"
8
+ else
9
+ K2e.sync
10
+ end
data/k2e.gemspec ADDED
@@ -0,0 +1,43 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require "k2e/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "k2e"
8
+ spec.version = K2e::VERSION
9
+ spec.authors = ["ttuan"]
10
+ spec.email = ["tuantv.nhnd@gmail.com"]
11
+
12
+ spec.summary = %q{A tool to synchronize Kindle highlights to Evernote.}
13
+ spec.homepage = "https://github.com/ttuan/k2e"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ # if spec.respond_to?(:metadata)
19
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
20
+
21
+ # spec.metadata["homepage_uri"] = spec.homepage
22
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
23
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
24
+ # else
25
+ # raise "RubyGems 2.0 or newer is required to protect against " \
26
+ # "public gem pushes."
27
+ # end
28
+
29
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
30
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
31
+ end
32
+ spec.executables = `git ls-files -- bin/*`.split("\n").map { |f| ::File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+ spec.required_ruby_version = ">= 2.0.0"
35
+
36
+ spec.add_dependency "kindleclippings", "~> 1.4", ">= 1.4.0"
37
+ spec.add_dependency "evernote_oauth", "~> 0.2.3"
38
+ spec.add_dependency "dotenv", "~> 2.6.0", ">= 2.6.0"
39
+
40
+ spec.add_development_dependency "bundler", "~> 1.16"
41
+ spec.add_development_dependency "rake", "~> 10.0"
42
+ spec.add_development_dependency "pry", "~> 0.12.2"
43
+ end
data/lib/k2e.rb ADDED
@@ -0,0 +1,25 @@
1
+ require "k2e/date_supporter.rb"
2
+ require "k2e/evernote.rb"
3
+ require "k2e/kindle.rb"
4
+
5
+ require "k2e/version"
6
+
7
+ module K2e
8
+ extend K2e::DateSupporter
9
+
10
+ def self.sync
11
+ kindle = Kindle.new
12
+
13
+ kindle.books_highlights.each do |book_title, highlights_content|
14
+ note = K2e::Note.new(
15
+ title: book_title,
16
+ content: highlights_content,
17
+ notebook_name: ENV["NOTEBOOK"]
18
+ )
19
+
20
+ note.update
21
+ end
22
+
23
+ update_last_sync_date
24
+ end
25
+ end
@@ -0,0 +1,22 @@
1
+ module K2e
2
+ module DateSupporter
3
+ def last_sync_date
4
+ unless File.file?(file_name)
5
+ dir_name = File.dirname file_name
6
+ Dir.mkdir(dir_name) unless File.exists?(dir_name)
7
+ File.open(file_name, "w") {|file| file.write("1970-01-01T00:00:00+07:00")}
8
+ end
9
+
10
+ @sync_date ||= DateTime.parse File.read(file_name)
11
+ end
12
+
13
+ def update_last_sync_date
14
+ File.open(file_name, "w") {|file| file.write(DateTime.now)}
15
+ end
16
+
17
+ private
18
+ def file_name
19
+ @file_name ||= ENV["K2E_DATA_LOCATION"] || "#{ENV["HOME"]}/.k2e/last_sync_date.txt"
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,67 @@
1
+ # Inspired from https://github.com/zdogma/kindle-highlights/blob/master/lib/evernote.rb
2
+
3
+ require "dotenv/load"
4
+ require "evernote_oauth"
5
+
6
+ module K2e
7
+ class EvernoteClient
8
+ attr_reader :token, :note_store
9
+
10
+ def initialize
11
+ @token = ENV["EVERNOTE_DEV_TOKEN"]
12
+ @note_store = ::EvernoteOAuth::Client.new(token: @token, sandbox: false).note_store
13
+ end
14
+ end
15
+
16
+ class Note
17
+ attr_reader :note_store, :title, :content, :notebook
18
+
19
+ OFFSET = 0
20
+ MAX_RESULT_NUM = 1
21
+
22
+ def initialize title:, content:, notebook_name:
23
+ @note_store = K2e::EvernoteClient.new.note_store
24
+ @title = title
25
+ @content = content
26
+ @notebook = find_notebook notebook_name
27
+ end
28
+
29
+ def update
30
+ new_note = Evernote::EDAM::Type::Note.new
31
+
32
+ new_note.title = title
33
+
34
+ filter = Evernote::EDAM::NoteStore::NoteFilter.new words: title
35
+ filter.notebookGuid = notebook.guid
36
+ found_note = note_store.findNotes(K2e::EvernoteClient.new.token, filter, OFFSET, MAX_RESULT_NUM).notes.first
37
+
38
+ if found_note&.title == new_note.title
39
+ full_note_content = note_store.getNote(K2e::EvernoteClient.new.token, found_note.guid, true, true, false, false)
40
+ new_note.guid = found_note.guid
41
+ new_note.content = full_note_content.content.gsub /<\/en-note>/ , "#{content}</en-note>"
42
+ note_store.updateNote(K2e::EvernoteClient.new.token, new_note)
43
+ else
44
+ new_note.content = format_xml content
45
+ new_note.notebookGuid = notebook.guid
46
+ note_store.createNote new_note
47
+ end
48
+
49
+ new_note
50
+ rescue Evernote::EDAM::Error::EDAMUserException => e
51
+ puts "EDAMUserException: #{e}"
52
+ end
53
+
54
+ private
55
+ def find_notebook notebook_name
56
+ note_store.listNotebooks.find {|notebook| notebook.name == notebook_name}
57
+ end
58
+
59
+ def format_xml content
60
+ <<-HEADER
61
+ <?xml version=\"1.0\" encoding=\"UTF-8\"?>
62
+ <!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">
63
+ <en-note>#{content}</en-note>
64
+ HEADER
65
+ end
66
+ end
67
+ end
data/lib/k2e/kindle.rb ADDED
@@ -0,0 +1,54 @@
1
+ require "kindleclippings"
2
+ require "time"
3
+
4
+ module K2e
5
+ class Kindle
6
+ include ::K2e::DateSupporter
7
+
8
+ attr_reader :parser
9
+
10
+ def initialize
11
+ @parser = KindleClippings::Parser.new
12
+ end
13
+
14
+ def books_highlights
15
+ clippings = parser.parse_file(clippings_file_path)
16
+
17
+ @result = {}
18
+
19
+ highlights = clippings.highlights.delete_if{|highlight| highlight.added_on < last_sync_date}
20
+ highlights.group_by(&:book_title).each do |book_title, book_highlights|
21
+ next if book_highlights.empty?
22
+ note_content = ""
23
+
24
+ book_highlights.each do |highlight|
25
+ str_date = Time.parse highlight.added_on.to_s
26
+ note_content += <<-CONTENT
27
+ <div>A highlight is created on #{str_date}, starting at location #{highlight.location} </div>
28
+
29
+ <blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;">
30
+ <div>
31
+ <i style="background-color:rgb(255, 250, 165);-evernote-highlight:true;">
32
+ #{highlight.content}
33
+ </i>
34
+ </div>
35
+ </blockquote>
36
+ <i><br/></i>
37
+ CONTENT
38
+ end
39
+ @result[book_title] = note_content
40
+ end
41
+
42
+ @result
43
+ end
44
+
45
+ private
46
+ def clippings_file_path
47
+ # TODO: Check with other platform
48
+ case Gem::Platform.local.os
49
+ when "darwin"
50
+ "/Volumes/Kindle/documents/My\ Clippings.txt"
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,3 @@
1
+ module K2e
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: k2e
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - ttuan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-03-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: kindleclippings
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.4'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.4.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.4'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.4.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: evernote_oauth
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 0.2.3
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: 0.2.3
47
+ - !ruby/object:Gem::Dependency
48
+ name: dotenv
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: 2.6.0
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 2.6.0
57
+ type: :runtime
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: 2.6.0
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 2.6.0
67
+ - !ruby/object:Gem::Dependency
68
+ name: bundler
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: '1.16'
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: '1.16'
81
+ - !ruby/object:Gem::Dependency
82
+ name: rake
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '10.0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: '10.0'
95
+ - !ruby/object:Gem::Dependency
96
+ name: pry
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: 0.12.2
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - "~>"
107
+ - !ruby/object:Gem::Version
108
+ version: 0.12.2
109
+ description:
110
+ email:
111
+ - tuantv.nhnd@gmail.com
112
+ executables:
113
+ - k2e
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - ".env"
118
+ - ".env_example"
119
+ - ".gitignore"
120
+ - Gemfile
121
+ - Gemfile.lock
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - bin/k2e
126
+ - k2e.gemspec
127
+ - lib/k2e.rb
128
+ - lib/k2e/date_supporter.rb
129
+ - lib/k2e/evernote.rb
130
+ - lib/k2e/kindle.rb
131
+ - lib/k2e/version.rb
132
+ homepage: https://github.com/ttuan/k2e
133
+ licenses:
134
+ - MIT
135
+ metadata: {}
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: 2.0.0
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubyforge_project:
152
+ rubygems_version: 2.7.6
153
+ signing_key:
154
+ specification_version: 4
155
+ summary: A tool to synchronize Kindle highlights to Evernote.
156
+ test_files: []