Hey there, fellow developer! Ready to supercharge your Python projects with Evernote's powerful API? You're in the right place. We'll be using the evernote
package to tap into Evernote's vast capabilities, allowing you to create, retrieve, update, and delete notes programmatically. Let's dive in!
Before we get our hands dirty, make sure you've got:
First things first, let's get that evernote
package installed:
pip install evernote
Easy peasy, right?
Now, let's get you authenticated:
from evernote.api.client import EvernoteClient client = EvernoteClient( consumer_key='your_consumer_key', consumer_secret='your_consumer_secret', sandbox=True # Set to False for production ) request_token = client.get_request_token('your_callback_url') authorize_url = client.get_authorize_url(request_token)
Visit the authorize_url
to get your OAuth verifier, then:
access_token = client.get_access_token( request_token['oauth_token'], request_token['oauth_token_secret'], oauth_verifier ) client = EvernoteClient(token=access_token)
Boom! You're in.
Let's jot down some thoughts:
note_store = client.get_note_store() note = Types.Note() note.title = "My First Evernote API Note" note.content = '<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"><en-note>Hello, Evernote API!</en-note>' note_store.createNote(note)
Fetch that masterpiece:
note = note_store.getNote(note.guid, True, True, False, False)
Had a stroke of genius? Update that note:
note.content = '<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"><en-note>Hello again, Evernote API!</en-note>' note_store.updateNote(note)
Changed your mind? No problem:
note_store.deleteNote(note.guid)
Create a new notebook:
notebook = Types.Notebook() notebook.name = "My API Notebook" notebook = note_store.createNotebook(notebook)
Tag it up:
tag = Types.Tag() tag.name = "api-created" tag = note_store.createTag(tag)
Find what you need:
filter = NoteStore.NoteFilter() filter.words = "Evernote API" result_spec = NoteStore.NotesMetadataResultSpec() result_spec.includeTitle = True results = note_store.findNotesMetadata(filter, 0, 10, result_spec)
Attach files like a pro:
with open('image.png', 'rb') as f: data = f.read() md5 = hashlib.md5() md5.update(data) hash = md5.digest() data = Types.Data() data.size = len(data) data.bodyHash = hash data.body = data resource = Types.Resource() resource.mime = 'image/png' resource.data = data note.resources = [resource] note.content += '<en-media type="image/png" hash="{}"/>'.format(hash)
Always wrap your API calls in try-except blocks:
try: note_store.createNote(note) except EDAMUserException as e: if e.errorCode == EDAMErrorCode.RATE_LIMIT_REACHED: print("Slow down, speedster! You've hit the rate limit.") # Handle other exceptions...
Remember to respect rate limits and keep your API key secret!
Let's put it all together in a simple CLI:
import click from evernote.api.client import EvernoteClient @click.command() @click.option('--title', prompt='Note title') @click.option('--content', prompt='Note content') def create_note(title, content): client = EvernoteClient(token='your_access_token') note_store = client.get_note_store() note = Types.Note() note.title = title note.content = f'<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"><en-note>{content}</en-note>' created_note = note_store.createNote(note) click.echo(f"Note created with GUID: {created_note.guid}") if __name__ == '__main__': create_note()
And there you have it! You're now equipped to integrate Evernote's API into your Python projects. Remember, this is just scratching the surface - there's so much more you can do. Check out the official documentation for more advanced features.
Running into issues? Here are some common pitfalls:
Now go forth and build something awesome with Evernote and Python! Happy coding!