Skip to content

Instantly share code, notes, and snippets.

@wararyo
Last active February 4, 2017 12:50
Show Gist options
  • Save wararyo/97519dd949364ff97851 to your computer and use it in GitHub Desktop.
Save wararyo/97519dd949364ff97851 to your computer and use it in GitHub Desktop.
Evernote for Python
from evernote.api.client import EvernoteClient
import evernote.edam.type.ttypes as Types
from evernote.edam.error import ttypes as Errors #この行が正しく機能してるかは怪しい
import os
import hashlib
import mimetypes
import binascii
dev_token = "your developer token"
client = EvernoteClient(token=dev_token, sandbox=True)
userStore = client.get_user_store()
user = userStore.getUser()
print user.username
#公式ガイドより 35行目のみ改変
def makeNote(authToken, noteStore, noteTitle, noteBody, resources=[], parentNotebook=None):
"""
Create a Note instance with title and body
Send Note object to user's account
"""
ourNote = Types.Note()
ourNote.title = noteTitle
## Build body of note
nBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
nBody += "<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">"
nBody += "<en-note>%s" % noteBody
if resources:
### Add Resource objects to note body
nBody += "<br />" * 2
ourNote.resources = resources
for resource in resources:
#hexhash = binascii.hexlify(resource.data.bodyHash)
hexhash = resource.data.bodyHash
nBody += "<en-media type=\"%s\" hash=\"%s\" /><br />" % \
(resource.mime, hexhash)
nBody += "</en-note>"
ourNote.content = nBody
print nBody
## parentNotebook is optional; if omitted, default notebook is used
if parentNotebook and hasattr(parentNotebook, 'guid'):
ourNote.notebookGuid = parentNotebook.guid
## Attempt to create note in Evernote account
try:
print('Sending...')
note = noteStore.createNote(authToken, ourNote)
print('Sent.')
except Errors.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
print "EDAMUserException:", edue
return None
except Errors.EDAMNotFoundException, ednfe:
## Parent Notebook GUID doesn't correspond to an actual notebook
print "EDAMNotFoundException: Invalid parent notebook GUID"
return None
## Return created note object
return note
def getResource(filepath):
filename = os.path.basename(filepath)
data = Types.Data()
data.body = open(filepath, 'r').read()
data.size = len(data.body)
data.bodyHash = hashlib.md5(data.body).hexdigest()
resource = Types.Resource()
resource.mime = mimetypes.guess_type(filename)[0]
resource.data = data
attr = Types.ResourceAttributes()
attr.fileName = filename
resource.attributes = attr
print('attachment: ' + filename)
return resource
def sendNote(title,filepaths=[]):
resources = []
for filepath in filepaths:
resources.append(getResource(filepath))
makeNote(dev_token,client.get_note_store(),title,"",resources)
import myevernote as Evernote
import glob
Evernote.sendNote('hoge', glob.glob('~/*.jpg'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment