yinx 0.1.0

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
+ SHA1:
3
+ metadata.gz: f7a68a3017767f66cc536c160d4fdeddcb51c875
4
+ data.tar.gz: 7d42de0bf67a8dec6465ee4cd3590ef387950ef4
5
+ SHA512:
6
+ metadata.gz: 5ca136c524b0af951a85c355134676653f6df342d47845d2901a7a29d4b2b0bef4ff540564af6cbd50a4f0acfdca0db4e74cc03d56af14fd9724cb3f2845f94b
7
+ data.tar.gz: 7e741e652e9ac6f9dcb93b47f4cc9bbad1bb451649c6c66cb5770378f164942e746e31f3b250161fb84cd80dfc419c71d14810c6221809b7bd3406b165a51bb0
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.swp
11
+ *.gem
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.14.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in yinx.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 ken
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,39 @@
1
+ # Yinx
2
+
3
+ Fetch note metdata with evernote api
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'yinx'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install yinx
20
+
21
+ ## Usage
22
+
23
+ Put your api key in `$HOME/.yinx`, then:
24
+
25
+ ```ruby
26
+ notes = Yinx.fetch_all
27
+ ```
28
+
29
+ To filter out:
30
+
31
+ ```ruby
32
+ notes = Yinx.fetch do
33
+ word 'active record'
34
+ tag 'rails'
35
+ book 'ruby'
36
+ stack 'programming language'
37
+ end
38
+ ```
39
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "yinx"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/yinx.rb ADDED
@@ -0,0 +1,62 @@
1
+ require 'yinx/version'
2
+ require 'yinx/user_store'
3
+ require 'yinx/down_config'
4
+ require 'yinx/note_meta'
5
+ require 'yinx/note_store'
6
+
7
+ module Yinx
8
+
9
+ Ex_Result = [:includeDeleted, :includeUpdateSequenceNum,
10
+ :includeAttributes, :includeLargestResourceMime,
11
+ :includeLargestResourceSize]
12
+
13
+ Result = (NoteStore::NOTE_META_RESULT_SPECS - Ex_Result).reduce({}) do |hash, attr_present|
14
+ hash[attr_present] = true; hash
15
+ end
16
+
17
+ class << self
18
+
19
+ attr_reader :config
20
+
21
+ def fetch real = true, &block
22
+ @real = real
23
+ @config = DownConfig.new note_store
24
+ config.instance_eval &block
25
+ download
26
+ end
27
+
28
+ def fetch_all
29
+ fetch {book /./}
30
+ end
31
+
32
+ private
33
+
34
+ def download
35
+ config.note_filters.map do |filter|
36
+ note_store.findNotes(filter.merge Result)
37
+ end.flatten.map do |note|
38
+ NoteMeta.new note, note_store
39
+ end
40
+ end
41
+
42
+ def note_store
43
+ @note_store ||= UserStore.new(@real).note_store
44
+ end
45
+
46
+ def formated book, meta
47
+ address = book.stack ? "#{book.stack}/#{book.name}/#{meta.title}" : "#{book.name}/#{meta.title}"
48
+ time = Time.at(meta.updated / 1000).strftime('%F %T')
49
+ "#{meta.updated} #{time} #{address}"
50
+ end
51
+
52
+ def concat_metas real
53
+ books = note_store(real).listNotebooks &block
54
+ books.map do |book|
55
+ metas = note_store.findNotes({notebookGuid: book.guid}).notes
56
+ metas.map do |meta|
57
+ "#{formated book, meta}\n"
58
+ end.join
59
+ end.join
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,83 @@
1
+ module Yinx
2
+ class DownConfig
3
+
4
+ attr_reader :note_store
5
+
6
+ class NullNoteStore < BasicObject
7
+ def method_missing *p
8
+ :Kernel.send :raise, 'note store not set'
9
+ end
10
+ end
11
+
12
+ def initialize note_st = NullNoteStore.new
13
+ @note_store = note_st
14
+ end
15
+
16
+ %w{book stack tag word}.each do |condition|
17
+ define_method "wanted_#{condition}s" do
18
+ instance_variable_get("@wanted_#{condition}s") || []
19
+ end
20
+
21
+ define_method "want_#{condition}?" do |name|
22
+ wanted_names = self.send "wanted_#{condition}s"
23
+ wanted_names.empty? ? false : wanted_names.any? do |wanted|
24
+ wanted === name or wanted.to_s == name
25
+ end
26
+ end
27
+
28
+ define_method condition do |*conditions|
29
+ instance_variable_set "@wanted_#{condition}s", conditions
30
+ end
31
+ end
32
+
33
+ def note_filters
34
+ merged_filters_in_array = individual_filters.reduce do |merged, indv|
35
+ merged.empty? ? indv : (indv.empty? ? merged : merged.product(indv))
36
+ end
37
+ merged_filters_in_hash =
38
+ if merged_filters_in_array.fetch(0){[]}.kind_of? Array
39
+ merged_filters_in_array.map do |sub_arr|
40
+ sub_arr.flatten.reduce do |merged, indv|
41
+ merged.merge indv
42
+ end
43
+ end
44
+ else
45
+ merged_filters_in_array
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ def individual_filters
52
+ self.private_methods.select do |m|
53
+ m =~ /filter$/
54
+ end.map do |filter_convertor|
55
+ self.send filter_convertor
56
+ end
57
+ end
58
+
59
+ def book_id_filter
60
+ return [] if wanted_books.empty? and wanted_stacks.empty?
61
+ from_book = note_store.listNotebooks{ |book| want_book? book.name }
62
+ from_stack = note_store.listNotebooks{ |book| want_stack? book.stack }
63
+ (from_book.map(&:guid) | from_stack.map(&:guid)).map{|guid| {notebookGuid: guid}}
64
+ end
65
+
66
+ def tag_id_filter
67
+ return [] if wanted_tags.empty?
68
+ note_store.listTags do |tag|
69
+ want_tag? tag.name
70
+ end.map do |tag|
71
+ {tagGuids: [tag.guid]}
72
+ end
73
+ end
74
+
75
+ def words_filter
76
+ wanted_words.map do |word|
77
+ word = word.join ' ' if word.kind_of? Array
78
+ {words: word.to_s}
79
+ end
80
+ end
81
+
82
+ end
83
+ end
@@ -0,0 +1,56 @@
1
+ require 'evernote-thrift'
2
+
3
+ class NoteMeta
4
+
5
+ [:updated, :created, :title, :notebookGuid, :guid, :contentLength, :tagGuids].each do |method|
6
+ define_method method do
7
+ iv_name = "@#{method}"
8
+ value = instance_variable_get iv_name
9
+ unless value
10
+ value = instance_variable_get("@meta").send method
11
+ instance_variable_set iv_name, value
12
+ end
13
+ value
14
+ end
15
+ end
16
+
17
+ def initialize meta, note_store
18
+ @meta = meta
19
+ @store = note_store
20
+ end
21
+
22
+ def tags
23
+ @tags ||= (tagGuids ? tagGuids.map{|id| @store.tag_name id} : [])
24
+ end
25
+
26
+ def book
27
+ @book ||= @store.book_name notebookGuid
28
+ end
29
+
30
+ def stack
31
+ @stack = (instance_variable_defined? :@stack) ? @stack : @store.stack_name(notebookGuid)
32
+ end
33
+
34
+ def to_h
35
+ @h = attr_methods.each_with_object({}) do |method, hash|
36
+ hash[method] = send method
37
+ end
38
+ end
39
+
40
+ def marshal_dump
41
+ to_h
42
+ end
43
+
44
+ def marshal_load hash
45
+ hash.each do |key, value|
46
+ instance_variable_set "@#{key}", value
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ def attr_methods
53
+ self.class.instance_methods(false) - [:to_h, :marshal_dump, :marshal_load]
54
+ end
55
+
56
+ end
@@ -0,0 +1,92 @@
1
+ require 'evernote-thrift'
2
+
3
+ module Yinx
4
+ class NoteStore
5
+
6
+ attr_reader :auth_token
7
+
8
+ NOTE_FILTERS = [:order, :ascending, :words,
9
+ :notebookGuid, :tagGuids, :timeZone,
10
+ :inactive, :emphasized]#, :includeAllReadableNotebooks]
11
+
12
+ NOTE_META_RESULT_SPECS = [:includeTitle, :includeContentLength,
13
+ :includeCreated, :includeUpdated,
14
+ :includeDeleted, :includeUpdateSequenceNum,
15
+ :includeNotebookGuid, :includeTagGuids,
16
+ :includeAttributes, :includeLargestResourceMime,
17
+ :includeLargestResourceSize]
18
+
19
+ def note_store
20
+ @noteStore
21
+ end
22
+
23
+ def initialize userStore, authToken
24
+ noteStoreUrl = userStore.getNoteStoreUrl(authToken)
25
+ noteStoreTransport = Thrift::HTTPClientTransport.new(noteStoreUrl)
26
+ noteStoreProtocol = Thrift::BinaryProtocol.new(noteStoreTransport)
27
+ @auth_token = authToken
28
+ @noteStore = Evernote::EDAM::NoteStore::NoteStore::Client.new(noteStoreProtocol)
29
+ end
30
+
31
+ def listNotebooks authToken = auth_token, &blk
32
+ @notebooks ||= note_store.listNotebooks(authToken)
33
+ block_given? ? @notebooks.select(&blk) : @notebooks
34
+ end
35
+
36
+ def listTags &blk
37
+ @tags ||= note_store.listTags(auth_token)
38
+ block_given? ? @tags.select(&blk) : @tags
39
+ end
40
+
41
+ def stack_name id
42
+ @stack_hash ||= Hash[listNotebooks.map{|book| [book.guid, book.stack]}]
43
+ @stack_hash[id]
44
+ end
45
+
46
+ def book_name id
47
+ @book_hash ||= Hash[listNotebooks.map{|book| [book.guid, book.name]}]
48
+ @book_hash[id]
49
+ end
50
+
51
+ def tag_name id
52
+ @tag_hash ||= Hash[listTags.map{|tag| [tag.guid, tag.name]}]
53
+ @tag_hash[id]
54
+ end
55
+
56
+ def findNotes opt = {}
57
+ fl, start, ending, sp = filter(opt), 0, 250, spec(opt)
58
+ md_list = note_store.findNotesMetadata auth_token, fl, start, ending, sp
59
+ result = md_list.notes
60
+ while md_list.totalNotes > start + ending
61
+ start += ending
62
+ md_list = note_store.findNotesMetadata auth_token, fl, start, ending, sp
63
+ result.concat md_list.notes
64
+ end
65
+ result
66
+ end
67
+
68
+ private
69
+
70
+ def filter opt = {}
71
+ filter = Evernote::EDAM::NoteStore::NoteFilter.new
72
+ merge filter, opt, NOTE_FILTERS
73
+ filter.order ||= Evernote::EDAM::Type::NoteSortOrder::UPDATED
74
+ filter
75
+ end
76
+
77
+ def spec opt = {}
78
+ spec = Evernote::EDAM::NoteStore::NotesMetadataResultSpec.new
79
+ merge spec, opt, NOTE_META_RESULT_SPECS
80
+ spec.includeTitle ||= true
81
+ spec.includeUpdated ||= true
82
+ spec
83
+ end
84
+
85
+ def merge struct, hash, keys
86
+ keys.each do |k|
87
+ struct.send (k.to_s + '=').to_sym, hash[k]
88
+ end
89
+ struct
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,42 @@
1
+ require 'evernote-thrift'
2
+ require 'yinx/note_store'
3
+
4
+ module Yinx
5
+ class UserStore
6
+
7
+ attr_reader :userStore
8
+
9
+ SANDBOX = "sandbox.evernote.com"
10
+ #REAL = "www.evernote.com"
11
+ REAL = "app.yinxiang.com"
12
+
13
+ TOKEN = "#{ENV['HOME']}/.yinx"
14
+
15
+ def initialize real_env = true
16
+ @real = real_env
17
+ userStoreUrl = "https://#{host}/edam/user"
18
+ userStoreTransport = Thrift::HTTPClientTransport.new(userStoreUrl)
19
+ userStoreProtocol = Thrift::BinaryProtocol.new(userStoreTransport)
20
+ @userStore = Evernote::EDAM::UserStore::UserStore::Client.new(userStoreProtocol)
21
+ end
22
+
23
+ def host
24
+ @real ? REAL : SANDBOX
25
+ end
26
+
27
+ def checkVersion
28
+ userStore.checkVersion("Evernote EDAMTest (Ruby)",
29
+ Evernote::EDAM::UserStore::EDAM_VERSION_MAJOR,
30
+ Evernote::EDAM::UserStore::EDAM_VERSION_MINOR)
31
+ end
32
+
33
+ def note_store authToken = default_token
34
+ NoteStore.new userStore, authToken
35
+ end
36
+
37
+ def default_token
38
+ File.exist?(TOKEN) ? File.read(TOKEN).chomp : nil
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,3 @@
1
+ module Yinx
2
+ VERSION = "0.1.0"
3
+ end
data/yinx.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'yinx/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "yinx"
8
+ spec.version = Yinx::VERSION
9
+ spec.authors = ["ken"]
10
+ spec.email = ["block24block@gmail.com"]
11
+
12
+ spec.summary = %q{Get my evernote metadata}
13
+ spec.homepage = "https://github.com/turnon/yinx"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.14"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "minitest", "~> 5.0"
26
+
27
+ spec.add_dependency "evernote-thrift", "~> 1.25.0"
28
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yinx
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ken
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: evernote-thrift
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.25.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.25.0
69
+ description:
70
+ email:
71
+ - block24block@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - lib/yinx.rb
85
+ - lib/yinx/down_config.rb
86
+ - lib/yinx/note_meta.rb
87
+ - lib/yinx/note_store.rb
88
+ - lib/yinx/user_store.rb
89
+ - lib/yinx/version.rb
90
+ - yinx.gemspec
91
+ homepage: https://github.com/turnon/yinx
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.5.2
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Get my evernote metadata
115
+ test_files: []