Evernote API & Python 3.x, #1

I'm using Evernote as my note taking app. Some of my notes are published on some Ghost sites. Because Python is my language of choice I had to code an interface from Evernote to Python and from Python to Ghost.

It has cost me quite some time to code those 2 interfaces and I'm glad to share some of my code with the Python community. In my first couple of posts I show some code for the Evernote-Python interface. Later I will show some code for the Python-Ghost interface.

Let's start:

  1. Install the Evernote SDK for Python 3
  2. Get an API key from Evernote
  3. Test the use of the Evernote API
  4. Have access to the API documentation. This is your most important source of information.

If everything worked so far, let's start to code. My code is very explicit. You should easily understand it. Create a file evernoteStore.py and add the code sections below (You will find the complete code for this tutorial at the end of Part 3. If you hover over a code sample you can copy the code by clicking on the tooltip in the upper right corner of the code block).

  1. The imports
from evernote.edam.type.ttypes import NoteSortOrder
from evernote.edam.notestore.ttypes import NoteFilter, NotesMetadataResultSpec
from evernote.api.client import EvernoteClient

2. Create the class EvernoteStore

class EvernoteStore():
    
    def __init__(self, sandbox=False):
        if sandbox == True: authToken='S=s1:.........b9' # for testing
        else sandbox == False: authToken='S=s2:U=1.........e2' # for production
        self.authToken = authToken
        self.sandbox = sandbox
        self.china = False	# always set to False
        self.noteBookName = None
        self.noteBookGuid = None
        self.client = EvernoteClient(token=self.authToken, sandbox=self.sandbox, china=self.china)
        self.userStore = self.client.get_user_store()
        # UserStore is an object used to retrieve information about the current user. To create an instance of UserStore, call the get_user_store method from EvernoteClient
        self.user = self.userStore.getUser().username    # https://dev.evernote.com/doc/reference/UserStore.html#Fn_UserStore_getUser
        self.noteStore = self.client.get_note_store()
        # NoteStore is used to create, update and delete notes, notebooks and other Evernote data found in a user’s account. Just like with UserStore, creating an instance of NoteStore is as easy as calling EvernoteClient.get_note_store
        self.noteBookList = None
        self.setNoteBooksList()
        self.noteBookTags = None
        self.timeZone = 'Europe/Zurich'
        self.logDirectory = None

 a) self.sandbox: First you do some tests on the Evernote development server. Everything is explained here. In this case sandbox is set to True and you set authToken to the developer token for the development server. When you feel comfortable working with the API you submit a key activation request to Evernote. Now you have an API key to work with your notebooks on the production server. In this case sandbox is set to False and you set authToken to the developer token for the production server.

 b) self.client: Then you create the client object by calling the EvernoteClient with authToken

 c)  self.userStore: This client object can now create the user object and has access to the user information with functions like getPublicUserInfo. All available functions in the module UserStore are listed here.

 d) self.noteStore: The client can also create the most important object, the noteStore object and has access to functions like createNote and deleteNote. All available functions in the module NoteStore are listed here.

 e) self.setNoteBooksList(): This is a method of our class EvernoteStore(). It calls the function listNotebooks (see below) of the module NoteStore. This function is documented here. As you can see from the documentation it returns a list of type Notebook. Click on <Types.Notebook> and you will find a detailed description of this data structure. For every notebook there is a name and the corresponding guid (the unique identifier of a notebook) together with some other data. To retrieve data for a specific notebook we need it's guid.

    def setNoteBooksList(self):
        notebooks = self.noteStore.listNotebooks()
        self.noteBookList = {'name': [],'guid':[]}
        for n in notebooks:
            self.noteBookList['name'].append(n.name)
            self.noteBookList['guid'].append(n.guid)

        return None
        
   
    def getNoteBooksList(self):
        return self.noteBookList

Now we can already use our class. At the bottom of evernoteStore.py insert the following code:

if __name__ == '__main__':
	store = EvernoteStore()
    notebooks = store.getNoteBooksList()

Proceed to Part 2