Class: Evernote_utils

Inherits:
Object
  • Object
show all
Defined in:
lib/evernote-utils.rb

Constant Summary collapse

OAUTH_CONSUMER_KEY =

Client credentials for enwrite

"zzamboni-2648"
OAUTH_CONSUMER_SECRET =
"05f988c37b5e8c68"
SANDBOX =

Connect to Sandbox server?

false
ENVVAR =

Environment variable in which to look for the token

'ENWRITE_AUTH_TOKEN'
@@authToken =
nil
@@userStore =
nil
@@noteStore =
nil
@@notebooks =
nil
@@tags =
nil
@@forceAuth =
false

Class Method Summary collapse

Class Method Details

.authTokenObject



87
88
89
90
91
92
# File 'lib/evernote-utils.rb', line 87

def self.authToken
  if @@authToken == nil || @@forceAuth
    @@authToken = self.getToken
  end
  return @@authToken
end

.checkVersionObject



110
111
112
113
114
115
116
117
118
119
# File 'lib/evernote-utils.rb', line 110

def self.checkVersion
  versionOK = self.userStore.checkVersion("enwrite",
		            Evernote::EDAM::UserStore::EDAM_VERSION_MAJOR,
		            Evernote::EDAM::UserStore::EDAM_VERSION_MINOR)
  verbose "Is my Evernote API version up to date?  #{versionOK}"
  unless versionOK
    error "Please update the Evernote Ruby libraries - they are not up to date."
    exit(1)
  end
end

.getTokenObject



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/evernote-utils.rb', line 75

def self.getToken
  if @@forceAuth
    return self.interactiveGetToken
  elsif not ENV[ENVVAR].nil?
    return ENV[ENVVAR]
  elsif not config(:evernote_auth_token).nil?
    return config(:evernote_auth_token)
  else
    return self.interactiveGetToken
  end
end

.getWholeNote(metadata) ⇒ Object



183
184
185
186
187
188
189
190
191
192
# File 'lib/evernote-utils.rb', line 183

def self.getWholeNote()
  note = self.noteStore.getNote(self.authToken, .guid, true, true, false, false)
  note.tagNames = []
  if .tagGuids != nil
    tags = Evernote_utils.tags
    note.tagNames = .tagGuids.map { |guid| tags[guid].name }
  end
  verbose "Tags: #{note.tagNames}"
  return note
end

.init(force = false, token = nil) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/evernote-utils.rb', line 168

def self.init(force=false, token=nil)
  @@forceAuth = force
  if not token.nil?
    @@forceAuth = false
    @@authToken = token
  end
  self.authToken
  self.userStore
  self.checkVersion
  self.noteStore
  
  self.notebooks
  self.tags
end

.interactiveGetTokenObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/evernote-utils.rb', line 32

def self.interactiveGetToken
  callback_url = "http://zzamboni.org/enwrite-callback.html"
  client = EvernoteOAuth::Client.new(token: nil, consumer_key: OAUTH_CONSUMER_KEY, consumer_secret: OAUTH_CONSUMER_SECRET, sandbox: SANDBOX)
  request_token = client.request_token(:oauth_callback => callback_url)
  authorize_url = request_token.authorize_url

  puts("Welcome to enwrite's Evernote authentication.

Please open the following URL:
#{authorize_url}

Once you authenticate you will be redirected to
a page in the zzamboni.org domain that will show you an authentication verifier
token. Please enter that token now.")
  print("> ")
  $stdout.flush
  oauth_verifier = gets.chomp

  access_token = request_token.get_access_token(:oauth_verifier => oauth_verifier)

  puts("Thank you! Your access token is the following string:
#{access_token.token}

I can store the token for you in the config file (#{config_file}),
then enwrite will use it automatically in the future.
")
  print "Would you like me to do that for you now (Y/n)? "
  $stdout.flush
  yesno = gets.chomp
  if yesno =~ /^([yY].*|)$/
    setconfig(:evernote_auth_token, access_token.token)
    puts "Token stored."
  else
    puts "OK, I won't store the token, just use it for now.

You can also store it in the ENWRITE_AUTH_TOKEN environment variable."
  end
  # Cancel force mode after we've gotten the token
  @@forceAuth = false
  
  return access_token.token
end

.notebooks(force = false) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/evernote-utils.rb', line 136

def self.notebooks(force=false)
  if (@@notebooks == nil) or force
    # List all of the notebooks in the user's account
    @@notebooks = self.noteStore.listNotebooks(self.authToken)
    verbose "Found #{notebooks.size} notebooks:"
    defaultNotebook = notebooks.first
    notebooks.each do |notebook|
      verbose "  * #{notebook.name}"
    end
  end
  return @@notebooks
end

.noteStoreObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/evernote-utils.rb', line 121

def self.noteStore
  if @@noteStore == nil
    # Get the URL used to interact with the contents of the user's account
    # When your application authenticates using OAuth, the NoteStore URL will
    # be returned along with the auth token in the final OAuth request.
    # In that case, you don't need to make this call.
    noteStoreUrl = self.userStore.getNoteStoreUrl(self.authToken)

    noteStoreTransport = Thrift::HTTPClientTransport.new(noteStoreUrl)
    noteStoreProtocol = Thrift::BinaryProtocol.new(noteStoreTransport)
    @@noteStore = Evernote::EDAM::NoteStore::NoteStore::Client.new(noteStoreProtocol)
  end
  return @@noteStore
end

.tags(force = false) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/evernote-utils.rb', line 149

def self.tags(force=false)
  if (@@tags == nil) or force
    verbose "Reading all tags:"
    
    # Get list of all tags, cache it for future use
    taglist = self.noteStore.listTags(self.authToken)
    # Create a hash for easier reference
    @@tags = {}
    tagstr = ""
    for t in taglist
      @@tags[t.guid] = t
      @@tags[t.name] = t
      tagstr += "#{t.name} "
    end
    verbose tagstr
  end
  return @@tags
end

.translate_error(e) ⇒ Object

From pollen.io/2012/12/creating-a-note-in-evernote-from-ruby/ With changes to handle RATE_LIMIT_REACHED



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/evernote-utils.rb', line 196

def self.translate_error(e)
  error_name = "unknown"
  case e.errorCode
  when Evernote::EDAM::Error::EDAMErrorCode::AUTH_EXPIRED
    error_name = "AUTH_EXPIRED"
  when Evernote::EDAM::Error::EDAMErrorCode::BAD_DATA_FORMAT
    error_name = "BAD_DATA_FORMAT"
  when Evernote::EDAM::Error::EDAMErrorCode::DATA_CONFLICT
    error_name = "DATA_CONFLICT"
  when Evernote::EDAM::Error::EDAMErrorCode::DATA_REQUIRED
    error_name = "DATA_REQUIRED"
  when Evernote::EDAM::Error::EDAMErrorCode::ENML_VALIDATION
    error_name = "ENML_VALIDATION"
  when Evernote::EDAM::Error::EDAMErrorCode::INTERNAL_ERROR
    error_name = "INTERNAL_ERROR"
  when Evernote::EDAM::Error::EDAMErrorCode::INVALID_AUTH
    error_name = "INVALID_AUTH"
  when Evernote::EDAM::Error::EDAMErrorCode::LIMIT_REACHED
    error_name = "LIMIT_REACHED"
  when Evernote::EDAM::Error::EDAMErrorCode::PERMISSION_DENIED
    error_name = "PERMISSION_DENIED"
  when Evernote::EDAM::Error::EDAMErrorCode::QUOTA_REACHED
    error_name = "QUOTA_REACHED"
  when Evernote::EDAM::Error::EDAMErrorCode::SHARD_UNAVAILABLE
    error_name = "SHARD_UNAVAILABLE"
  when Evernote::EDAM::Error::EDAMErrorCode::UNKNOWN
    error_name = "UNKNOWN"
  when Evernote::EDAM::Error::EDAMErrorCode::VALID_VALUES
    error_name = "VALID_VALUES"
  when Evernote::EDAM::Error::EDAMErrorCode::VALUE_MAP
    error_name = "VALUE_MAP"
  when Evernote::EDAM::Error::EDAMErrorCode::RATE_LIMIT_REACHED
    error_name = "RATE_LIMIT_REACHED"
    e.message = "Rate limit reached. Please retry in #{e.rateLimitDuration} seconds"
  end
  rv = "Error code was: #{error_name}[#{e.errorCode}] and parameter: [#{e.message}]"
end

.userStoreObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/evernote-utils.rb', line 94

def self.userStore
  if @@userStore == nil
    # Initial development is performed on our sandbox server. To use the production
    # service, change "sandbox.evernote.com" to "www.evernote.com" and replace your
    # developer token above with a token from
    # https://www.evernote.com/api/DeveloperToken.action
    evernoteHost = SANDBOX ? "sandbox.evernote.com" : "www.evernote.com"
    userStoreUrl = "https://#{evernoteHost}/edam/user"

    userStoreTransport = Thrift::HTTPClientTransport.new(userStoreUrl)
    userStoreProtocol = Thrift::BinaryProtocol.new(userStoreTransport)
    @@userStore = Evernote::EDAM::UserStore::UserStore::Client.new(userStoreProtocol)
  end
  return @@userStore
end