Created
June 19, 2022 12:34
-
-
Save yeonhoyoon/75126982e0fb431132bf512843532c27 to your computer and use it in GitHub Desktop.
Upload CSV file to Evernote
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'evernote_oauth' | |
require 'csv' | |
developer_token 'asdf' # https://www.evernote.com/api/DeveloperToken.action | |
client = EvernoteOAuth::Client.new(token: developer_token, sandbox: false) | |
note_store = client.note_store | |
notebooks = note_store.listNotebooks | |
notebooks.each do |notebook| | |
puts "Notebook: #{notebook.name}" | |
end | |
target_notebook = notebooks.last | |
csv_text = File.read('log.csv') | |
csv = CSV.parse(csv_text, headers: true, liberal_parsing: true) | |
csv.each_with_index { |row, index| | |
puts index | |
date_text = row[0] | |
note_timestamp = Date.parse(date_text).strftime("%Q").to_i + 3600 * 1000 | |
# puts Time.at(note_timestamp) | |
content = "<div>#{row[1].gsub("\b", "").gsub("&", "&").gsub("\n", "<br/>")}</div>" | |
make_note(note_store, date_text, content, note_timestamp, target_notebook) | |
}; :ok | |
def make_note(note_store, note_title, note_body, note_timestamp, parent_notebook=nil) | |
n_body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" | |
n_body += "<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">" | |
n_body += "<en-note>#{note_body}</en-note>" | |
## Create note object | |
our_note = Evernote::EDAM::Type::Note.new | |
our_note.title = note_title | |
our_note.content = n_body | |
our_note.created = note_timestamp # utc timestamp in milliseconds. Time.now.to_i * 1000 | |
our_note.updated = note_timestamp | |
## parent_notebook is optional; if omitted, default notebook is used | |
if parent_notebook && parent_notebook.guid | |
our_note.notebookGuid = parent_notebook.guid | |
end | |
## Attempt to create note in Evernote account | |
begin | |
note = note_store.createNote(our_note) | |
rescue Evernote::EDAM::Error::EDAMUserException => edue | |
## Something was wrong with the note data | |
## See EDAMErrorCode enumeration for error code explanation | |
## http://dev.evernote.com/documentation/reference/Errors.html#Enum_EDAMErrorCode | |
puts "EDAMUserException: #{edue.inspect}" | |
rescue Evernote::EDAM::Error::EDAMNotFoundException => ednfe | |
## Parent Notebook GUID doesn't correspond to an actual notebook | |
puts "EDAMNotFoundException: Invalid parent notebook GUID" | |
end | |
## Return created note object | |
note | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment