Class: Evernote::Jeeves::JeevesRunner

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

Instance Method Summary collapse

Instance Method Details

#runObject

Raises:

  • (RuntimeError)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/evernote/jeeves.rb', line 59

def run
  options = JeevesOptionsParser.parse(ARGV)

  # get the authToken from config
  config = YAML.load_file(File.join(ENV['HOME'], '/.jeeves/config.yml'))
  authToken = config["config"]["authToken"]

  # Since this app only accesses your own Evernote account, we can use a developer token
  # that allows you to access your own Evernote account and skip OAuth authentication.
  # To get a developer token, visit https://sandbox.evernote.com/api/DeveloperToken.action

  if authToken == "your developer token"
  puts "Please fill in your developer token"
  puts "To get a developer token, visit https://sandbox.evernote.com/api/DeveloperToken.action"
  exit(1)
  end

  # Initial development can be performed on Evernote's sandbox server. It requires a separate
  # account and authToken.  To switch to using the sandbox server, change "www.evernote.com" to
  # "sandbox.evernote.com" and replace your developer token above with a sandbox token.
  #evernoteHost = "sandbox.evernote.com"
  evernoteHost = "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)

  # Verify your Evernote gem is up to date
  versionOK = userStore.checkVersion("Evernote EDAM (Ruby)",
                                     Evernote::EDAM::UserStore::EDAM_VERSION_MAJOR,
                                     Evernote::EDAM::UserStore::EDAM_VERSION_MINOR)
  raise RuntimeError, "API version out of date" unless versionOK

  # 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.
  begin
    noteStoreUrl = userStore.getNoteStoreUrl(authToken)
  rescue Evernote::EDAM::Error::EDAMUserException => e
    pp e
    #puts e.getErrorCode()
  end
  
  noteStoreTransport = Thrift::HTTPClientTransport.new(noteStoreUrl)
  noteStoreProtocol = Thrift::BinaryProtocol.new(noteStoreTransport)
  noteStore = Evernote::EDAM::NoteStore::NoteStore::Client.new(noteStoreProtocol)

  # search notes
  noteFilter = Evernote::EDAM::NoteStore::NoteFilter.new
  # 2 == sort results by updated
  noteFilter.order = 2
  # newest first
  noteFilter.ascending = FALSE
  noteFilter.words = "updated:day-#{options.days} #{options.search}"

  # We want our search results to return title, notebook GUID, and updated date
  spec = Evernote::EDAM::NoteStore::NotesMetadataResultSpec.new
  spec.includeTitle = true
  spec.includeNotebookGuid = true
  spec.includeUpdated = true

  # we limit to 100 results to avoid craziness
  noteList = noteStore.(authToken,noteFilter,0,100, spec)

  displayedNotes = Array.new
  searchPattern = Regexp.new(options.search, options.ignorecase)

  noteList.notes.each do |note|
    # retrieve the note - just the contents, don't need other resources
    doc = noteStore.getNote(authToken, note.guid, true, false, false, false).content
    noteMatches = false
    matchingLines = ""
    # look for the search string in this note's content, line-by-line
    doc.lines.each do |line|
      if line =~ searchPattern
        noteMatches = true
        # indentation hack
        matchingLines << "  #{Sanitize.clean(line).strip}\n"
      end
    end
    if noteMatches
      # display note metadata along with matching lines
      displayedNotes << "#{note.title} (#{Time.at(note.updated/1000).strftime("%m/%d/%y")}, " +
        "#{noteStore.getNotebook(authToken, note.notebookGuid).name})\n#{matchingLines}\n"
    end
  end

  # time to display the results
  if options.verbose
    puts "There are #{displayedNotes.count} matching notes.\n\n"
  end

  displayedNotes.each do |text|
    puts text
  end
end