evernote_uploader 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 232f5e640e344667e8935ec083f1d3a26cb15184
4
+ data.tar.gz: 536c03c7a5942bce879935574bd58f6d9904c033
5
+ SHA512:
6
+ metadata.gz: 71b264a665fb308603e8a21ec7419c1976497d20b6eeee053b95b78b271b5966d8b99b605b2fbdef28e664370acff60700a659f5f2d4b13e4cab86b761486ed5
7
+ data.tar.gz: 0fcd29b3a0847bcf8f4afbe96ffac9b7bb319ba5d9583795d87357cbb18dacf9ee8b8655c04e5e21379eb6ea2f66c2ac3c15089bb71aaa9267a55598c189bd1e
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /rdoc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in evernote_uploader.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,52 @@
1
+ Copyright (c) 2014 Hiroyuki Tanaka
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+
25
+ "test/enlogo.png"
26
+ /*
27
+ * Copyright (c) 2007-2012 by Evernote Corporation, All rights reserved.
28
+ *
29
+ * Use of the source code and binary libraries included in this package
30
+ * is permitted under the following terms:
31
+ *
32
+ * Redistribution and use in source and binary forms, with or without
33
+ * modification, are permitted provided that the following conditions
34
+ * are met:
35
+ *
36
+ * 1. Redistributions of source code must retain the above copyright
37
+ * notice, this list of conditions and the following disclaimer.
38
+ * 2. Redistributions in binary form must reproduce the above copyright
39
+ * notice, this list of conditions and the following disclaimer in the
40
+ * documentation and/or other materials provided with the distribution.
41
+ *
42
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
43
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
44
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
45
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
46
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
48
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
49
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
50
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
51
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52
+ */
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # EvernoteUploader
2
+
3
+ Upload files to Evernote
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'evernote_uploader'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install evernote_uploader
20
+
21
+ ## Usage
22
+ Get a developer token from https://www.evernote.com/api/DeveloperToken.action,
23
+ and store the key in `~/.evernote-token`.
24
+
25
+ ### As command line tool
26
+
27
+ ```
28
+ evernote_uploader [options] <files>
29
+ options
30
+ -n, --notebook NOTEBOOK Name of notebook (default: web clip)
31
+ -t, --title TITLE Title of the note
32
+ --token TOKEN Token for evernote or filename
33
+ --tags TAGS Tags
34
+ ```
35
+
36
+ If multiple files are given, the all files are attached to a same note.
37
+
38
+ ## Contributing
39
+ 1. Fork it ( https://github.com/[my-github-username]/evernote_uploader/fork )
40
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
41
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
42
+ 4. Push to the branch (`git push origin my-new-feature`)
43
+ 5. Create a new Pull Request
44
+
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+ require 'rdoc/task'
4
+
5
+ Rake::TestTask.new(:test) do |t|
6
+ t.libs << "test"
7
+ end
8
+
9
+ task :default => :test
10
+
11
+ Rake::RDocTask.new do |rdoc|
12
+ rdoc.rdoc_dir = 'rdoc'
13
+ rdoc.main = 'README.md'
14
+ rdoc.rdoc_files.include(%w[README.md lib])
15
+ end
16
+
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+
5
+ root_dir = "#{__dir__}/../"
6
+ require "#{root_dir}/lib/evernote_uploader"
7
+
8
+ options = {notebook: "web clip", token: "~/.evernote-token"}
9
+ help_message = ""
10
+ OptionParser.new {|opts|
11
+ opts.banner = "Usage: #{File.basename(__FILE__)} [options] filename(s)..."
12
+
13
+ opts.on("-n", "--notebook NOTEBOOK",
14
+ String, "Name of notebook (default: #{options[:notebook]})") {|nb|
15
+ options[:notebook] = nb
16
+ }
17
+
18
+ opts.on("-t", "--title TITLE", String, "Title of the note") {|title|
19
+ options[:title] = title
20
+ }
21
+
22
+ opts.on("--token TOKEN", String, "Token for evernote or filename") {|token|
23
+ options[:token] = token
24
+ }
25
+
26
+ opts.on("--tags TAGS", Array, "Tags") {|tags|
27
+ options[:tags] = tags
28
+ }
29
+
30
+ opts.on_tail("-v", "--version") {
31
+ puts "#{File.basename($0)}: #{EvernoteUploader::VERSION}"
32
+ exit
33
+ }
34
+
35
+ help_message = opts.to_s
36
+ }.parse!
37
+
38
+ if ARGV.size < 1
39
+ puts help_message
40
+ exit
41
+ end
42
+
43
+ ev = EvernoteUploader.new(ARGV, options)
44
+ ev.upload
45
+
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'evernote_uploader/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "evernote_uploader"
8
+ spec.version = EvernoteUploader::VERSION
9
+ spec.authors = ["Hiroyuki Tanaka"]
10
+ spec.email = ["hryktnk@gmail.com"]
11
+ spec.summary = "Upload files to Evernote"
12
+ #spec.description = %q{TODO: Write a longer description. Optional.}
13
+ spec.homepage = "https://github.com/tanahiro/evernote_uploader.git"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "evernote-thrift", "~> 1.25"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "minitest"
26
+ end
@@ -0,0 +1,93 @@
1
+
2
+ require "digest/md5"
3
+ require "evernote-thrift"
4
+
5
+ root_dir = "#{__dir__}/../"
6
+ require "#{root_dir}/lib/evernote_uploader/version"
7
+ require "#{root_dir}/lib/evernote_uploader/helper"
8
+
9
+ class EvernoteUploader
10
+ include EvernoteUploaderHelper
11
+
12
+ EvernoteHost = "www.evernote.com"
13
+ EvernoteUrl = "https://#{EvernoteHost}/edam/user/"
14
+
15
+ class TokenError < StandardError; end # :nodoc:
16
+
17
+ def initialize filenames, options
18
+ @filenames = filenames
19
+ @options = options
20
+
21
+ check_evernote_token
22
+
23
+ evernote_api
24
+
25
+ get_notebooks
26
+ check_notebook
27
+
28
+ @note = create_note(@options, @filenames, @notebook)
29
+ end
30
+
31
+ def upload
32
+ puts "uploading #{@filenames.join(", ")}"
33
+ puts "Notebook: #{@notebook.name}"
34
+ puts "Title: #{@note.title}"
35
+ puts "tags #{@options[:tags]}"
36
+ puts "..."
37
+
38
+ @note_store.createNote(@token, @note)
39
+
40
+ puts "done!"
41
+ end
42
+
43
+ def check_evernote_token
44
+ if @options[:token]
45
+ if File.exist?(File.expand_path(@options[:token]))
46
+ filename = File.expand_path(@options[:token])
47
+ @token = File.open(filename) {|f| f.gets }.chomp
48
+ else
49
+ @token = @options[:token]
50
+ end
51
+ end
52
+
53
+ raise TokenError, "Token is not specified properly" unless @token
54
+ end
55
+
56
+ def evernote_api
57
+ user_store_transport = Thrift::HTTPClientTransport.new(EvernoteUrl)
58
+ user_store_protocol = Thrift::BinaryProtocol.new(user_store_transport)
59
+ @user_store =
60
+ Evernote::EDAM::UserStore::UserStore::Client.new(user_store_protocol)
61
+
62
+ version_ok =
63
+ @user_store.checkVersion("Evernote EDAMTest (Ruby)",
64
+ Evernote::EDAM::UserStore::EDAM_VERSION_MAJOR,
65
+ Evernote::EDAM::UserStore::EDAM_VERSION_MINOR)
66
+ unless version_ok
67
+ puts "Evernote API version not up-to-date"
68
+ exit
69
+ end
70
+ end
71
+
72
+ def get_notebooks
73
+ note_store_url = @user_store.getNoteStoreUrl(@token)
74
+ note_store_transport = Thrift::HTTPClientTransport.new(note_store_url)
75
+ note_store_protocol = Thrift::BinaryProtocol.new(note_store_transport)
76
+ @note_store =
77
+ Evernote::EDAM::NoteStore::NoteStore::Client.new(note_store_protocol)
78
+ end
79
+
80
+ def check_notebook
81
+ notebooks = @note_store.listNotebooks(@token)
82
+ note_names = notebooks.map {|nb| nb.name }
83
+
84
+ unless note_names.include?(@options[:notebook])
85
+ puts "Note book \"#{@options[:notebook]}\" doesn't exist -- exit"
86
+ exit
87
+ else
88
+ index = note_names.index(@options[:notebook])
89
+ @notebook = notebooks[index]
90
+ end
91
+ end
92
+ end
93
+
@@ -0,0 +1,70 @@
1
+
2
+ module EvernoteUploaderHelper
3
+ class FileTypeError < StandardError; end
4
+
5
+ ##
6
+ # http://www.iana.org/assignments/media-types/media-types.xhtml
7
+ def detect_mime_type filename
8
+ case File.extname(filename.downcase)
9
+ when ".pdf"
10
+ return "application/pdf"
11
+ when ".epub"
12
+ return "application/epub+zip"
13
+ when ".mobi"
14
+ return "application/x-mobipocket-ebook"
15
+ when ".jpg"
16
+ return "image/jpeg"
17
+ when ".png"
18
+ return "image/png"
19
+ when ".zip"
20
+ return "application/zip"
21
+ else
22
+ raise FileTypeError, "Unknown file type"
23
+ end
24
+ end
25
+
26
+ def create_note options, filenames, notebook = nil
27
+ note = Evernote::EDAM::Type::Note.new
28
+ note.title = options[:title] || File.basename(filenames.first, ".*")
29
+
30
+ note.tagNames = options[:tags]
31
+ if notebook
32
+ note.notebookGuid = notebook.guid
33
+ end
34
+
35
+ note.resources = []
36
+ note.content = <<EOF
37
+ <?xml version="1.0" encoding="UTF-8"?>
38
+ <!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
39
+ <en-note>
40
+ EOF
41
+
42
+ filenames.each {|filename|
43
+ file_data = File.open(filename, "rb") {|f| f.read}
44
+ hash_func = Digest::MD5.new
45
+
46
+ data = Evernote::EDAM::Type::Data.new
47
+ data.size = file_data.size
48
+ data.bodyHash = hash_func.digest(file_data)
49
+ data.body = file_data
50
+
51
+ resource = Evernote::EDAM::Type::Resource.new
52
+ resource.mime = detect_mime_type(filename)
53
+ resource.data = data
54
+ resource.attributes = Evernote::EDAM::Type::ResourceAttributes.new
55
+ resource.attributes.fileName = filename
56
+
57
+ hash_hex = hash_func.hexdigest(file_data)
58
+
59
+ note.resources << resource
60
+
61
+ note.content += " <en-media type=\"#{resource.mime}\" "
62
+ note.content += "hash = \"#{hash_hex}\"/>\n"
63
+ }
64
+
65
+ note.content += "</en-note>\n"
66
+
67
+ return note
68
+ end
69
+ end
70
+
@@ -0,0 +1,3 @@
1
+ class EvernoteUploader
2
+ VERSION = "0.0.3"
3
+ end
data/test/enlogo.png ADDED
Binary file
@@ -0,0 +1,5 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'evernote_uploader'
3
+
4
+ require 'minitest/autorun'
5
+
@@ -0,0 +1,24 @@
1
+ root_dir = "#{__dir__}/../"
2
+ require "#{root_dir}/test/minitest_helper"
3
+
4
+ class EvernoteUploader
5
+ def initialize
6
+ @options = {}
7
+ end
8
+ end
9
+
10
+ class TestEvernoteUploader < MiniTest::Unit::TestCase
11
+
12
+ def test_that_it_has_a_version_number
13
+ refute_nil ::EvernoteUploader::VERSION
14
+ end
15
+
16
+ def test_check_evernote_token
17
+ e = EvernoteUploader.new
18
+
19
+ assert_raises(EvernoteUploader::TokenError) {
20
+ e.instance_eval{ check_evernote_token }
21
+ }
22
+ end
23
+ end
24
+
@@ -0,0 +1,36 @@
1
+ ROOT_DIR = "#{__dir__}/../"
2
+ require "#{ROOT_DIR}/test/minitest_helper"
3
+
4
+ require "#{ROOT_DIR}/lib/evernote_uploader/helper"
5
+
6
+ class TestEvernoteUploader < MiniTest::Unit::TestCase
7
+ include EvernoteUploaderHelper
8
+
9
+ def test_detect_mime_type
10
+ assert_equal("application/pdf", detect_mime_type("foo.pdf"))
11
+ assert_equal("application/pdf", detect_mime_type("foo.PDF"))
12
+ assert_equal("application/epub+zip", detect_mime_type("foo.epub"))
13
+ assert_equal("application/x-mobipocket-ebook", detect_mime_type("foo.mobi"))
14
+ assert_equal("image/jpeg", detect_mime_type("foo.jpg"))
15
+ assert_equal("image/png", detect_mime_type("foo.png"))
16
+ assert_equal("application/zip", detect_mime_type("foo.zip"))
17
+
18
+ assert_raises(FileTypeError) { detect_mime_type("foo.bar") }
19
+ end
20
+
21
+ def test_create_note
22
+ expected_content = <<EOF
23
+ <?xml version="1.0" encoding="UTF-8"?>
24
+ <!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
25
+ <en-note>
26
+ <en-media type="image/png" hash = "a54fe8bcd146e20a8a5742834558543c"/>
27
+ </en-note>
28
+ EOF
29
+ note = create_note({}, ["#{ROOT_DIR}/test/enlogo.png"])
30
+
31
+ assert_equal(expected_content, note.content)
32
+ assert_equal("enlogo", note.title)
33
+
34
+ end
35
+ end
36
+
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: evernote_uploader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Hiroyuki Tanaka
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: evernote-thrift
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.25'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.25'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - hryktnk@gmail.com
72
+ executables:
73
+ - evernote_uploader
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/evernote_uploader
84
+ - evernote_uploader.gemspec
85
+ - lib/evernote_uploader.rb
86
+ - lib/evernote_uploader/helper.rb
87
+ - lib/evernote_uploader/version.rb
88
+ - test/enlogo.png
89
+ - test/minitest_helper.rb
90
+ - test/test_evernote_uploader.rb
91
+ - test/test_evernote_uploader_helper.rb
92
+ homepage: https://github.com/tanahiro/evernote_uploader.git
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.4.8
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Upload files to Evernote
116
+ test_files:
117
+ - test/enlogo.png
118
+ - test/minitest_helper.rb
119
+ - test/test_evernote_uploader.rb
120
+ - test/test_evernote_uploader_helper.rb