Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
# Get information about your notes
# create table notebooks (
# title varchar(255),
# num_notes int,
# avg_time int,
# created_at timestamp default now()
# );
# gem install evernote-thrift pg
require "digest/md5"
require 'evernote-thrift'
require 'rest-client'
require 'pg'
# Get your developer token here:
# https://www.evernote.com/api/DeveloperToken.action
authToken = ENV["EVERNOTE_DEVELOPER_TOKEN"]
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)
versionOK = userStore.checkVersion("Evernote EDAMTest (Ruby)",
Evernote::EDAM::UserStore::EDAM_VERSION_MAJOR,
Evernote::EDAM::UserStore::EDAM_VERSION_MINOR)
unless versionOK
puts "Is my Evernote API version up to date? #{versionOK}"
puts
exit(1)
end
# 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 = userStore.getNoteStoreUrl(authToken)
noteStoreTransport = Thrift::HTTPClientTransport.new(noteStoreUrl)
noteStoreProtocol = Thrift::BinaryProtocol.new(noteStoreTransport)
noteStore = Evernote::EDAM::NoteStore::NoteStore::Client.new(noteStoreProtocol)
# List all of the notebooks in the user's account
notebooks = noteStore.listNotebooks(authToken)
# puts "Found #{notebooks.size} notebooks:"
defaultNotebook = notebooks.first
conn = PGconn.open(:dbname => 'evernote')
conn.prepare('statement1', 'insert into notebooks (title, num_notes, avg_time) values($1, $2, $3);')
loop do
puts "Loop 1"
note_txt = "<table>"
note_txt << "<tr><th>Notebook</th><th>Avg Note Time</th><th>Num Notes</th></tr>"
notebooks.each do |notebook|
filter = Evernote::EDAM::NoteStore::NoteFilter.new(
:notebookGuid => notebook.guid,
:order => 1, # Created
)
b = Evernote::EDAM::NoteStore::NotesMetadataResultSpec.new
b.includeCreated = true
notes = noteStore.findNotesMetadata(authToken, filter, 0, 1000, b)
times = notes.notes.map do |note|
(Time.now - Time.at(note.created / 1000)).to_i / (60 * 60 * 24)
end
avg_time = times.inject{|sum,x| sum + x } / times.size rescue 0
conn.exec_prepared('statement1', [notebook.name, times.size, avg_time.to_i])
note_txt << "<tr><td>#{notebook.name }</td><td>#{avg_time}</td><td>#{times.size}</td>\n"
puts "<tr><td>#{notebook.name }</td><td>#{avg_time}</td><td>#{times.size}</td>\n"
end
note_txt << "</table>"
RestClient.post("https://api:#{ENV['MAILGUN_API_KEY']}@api.mailgun.net/v2/abhiyerra.mailgun.org/messages",
:from => "Abhi's QS <qs@abhiyerra.mailgun.org>",
:to => "ykabhinav@gmail.com",
:subject => "Evernote Avg Note Time #{Time.now.to_s}",
:html => note_txt)
sleep 60 * 60 * 24
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment