Evernote API & Python 3.x, #2
In Part 1 of the tutorial we used the method getNoteBooksList to retrieve a dict with 2 lists of all notebooks. One list with all notebook name and a corresponding list with all guid (the unique identifier). We need the guid of a notebook if we want to restrict our work (e.g. finding notes) to a specific notebook.
def setNotebook(self, noteBookName):
self.noteBookName = noteBookName
self.setNoteBookGuid()
return None
def setNoteBookGuid(self):
self.noteBookGuid = self.noteBookList['guid'][self.noteBookList['name'].index(self.noteBookName)]
return None
In the code above we set the notebook by name with setNotebook(). This method calls setNoteBookGuid() which sets the instance variable noteBookGuid.
Now we can retrieve e.g. all tags of a notebook:
def getNoteBookTags(self):
tags = self.noteStore.listTagsByNotebook(self.authToken, self.noteBookGuid)
self.noteBookTags = {'name': [],'guid':[],'parentGuid':[]}
for t in tags:
self.noteBookTags['name'].append(t.name)
self.noteBookTags['guid'].append(t.guid)
self.noteBookTags['parentGuid'].append(t.parentGuid)
return self.noteBookTags
getNoteBookTags: This is a method of our class EvernoteStore(). It calls the function listTagsByNotebook of the module NoteStore. It returns a list with this data structure. We store a subset of the tag data in the instance variable noteBookTags.
Now we want to find a note by title. In the NoteStore module there is a function getNote. This function uses the guid (the unique identifier of a note). Hence we first have to find the guid of the note (there is no function like getNoteByTitle). We code the method getNoteByTitle of our class EvernoteStore().
def getNoteByTitle(self, title):
# filter
filter = NoteFilter()
filter.timeZone = self.timeZone
filter.words = 'intitle:'+title
filter.order = NoteSortOrder.UPDATED
filter.notebookGuid = self.noteBookGuid
# resultspec
resultSpec = NotesMetadataResultSpec()
notes = self.noteStore.findNotesMetadata(self.authToken, filter, 0, 250, resultSpec)
notes = [self.getNoteByGuid(i.guid) for i in notes.notes]
return notes
filter: To find a note by title we have to set a filter. We create the data structure NoteFilter and set various fields. words is the most important one (from the documentation: "If present, a search query string that will filter the set of notes to be returned. Accepts the full search grammar documented in the Evernote API Overview"). The search grammar is documented here. In our example it's pretty simple: e.g. 'intitle:My wonderful recipe'.
resultspec: We also have to define the amount of data returned for a note. We create the data structure NotesMetadataResultSpec. Here we could define all the fields we want returned for a note. If we set no fields, like in our code above, only the guid of a note is returned. If we would like to get the title of the note too, we would define: resultSpec.includeTitle = True.
Given the 2 data structures filter and resultspec we can then call self.noteStore.findNotesMetadata(self.authToken, filter, 0, 250, resultSpec). This function returns a list of notes (notes.notes). See the documentation for further explanations. In our example this is a list of guid for all notes with the same title or the same substring in the title. We then call the method getNoteByGuid of our class EvernoteStore() on each guid in this list and we get a list of notes with all kind of data per note ([self.getNoteByGuid(i.guid) for i in notes.notes]).
def getNoteByGuid(self, guid):
noteE = self.noteStore.getNote(self.authToken, guid, True, True, True, True)
note = {}
note['Result'] = 'success:'
note['Title'] = noteE.title
note['Guid'] = noteE.guid
note['Notebook'] = self.noteBookList['name'][self.noteBookList['guid'].index(noteE.notebookGuid)]
note['Created'] = enDateTimeToPythonDateTime(noteE.created)
note['Updated'] = enDateTimeToPythonDateTime(noteE.updated)
note['Deleted'] = enDateTimeToPythonDateTime(noteE.deleted)
note['Content'] = self.noteStore.getNoteContent(self.authToken, noteE.guid)
note['TagsGuid'] = noteE.tagGuids
note['TagsName'] = self.noteStore.getNoteTagNames(self.authToken, noteE.guid)
note['Resources'] = self.getAllResourceData(noteE.resources)
return note
The method above first calls the function getNote of the module NoteStore (the documentation says that getNote is depreciated and getNoteWithResultSpec should be used instead. But this function is not available yet). We then create the dict note with data from getNote. Various date fields are translated from the Evernote date format to the Python date format (the function enDateTimeToPythonDateTime in our own dateTime module is pretty simple: fromtimestamp(enTime/1000) where fromtimestamp is a function of the Python datetime module).
To get the content of a note we have to call getNoteContent of the module NoteStore. The field tagGuids from getNote is a list of with all the guid for the tags used in the note. We then call the function getNoteTagNames of the module NoteStore to get the names for all tag guid in this list.
Finally we call the method getAllResourceData of our class EvernoteStore(). This method retrieves resources of a note (e.g. image files) and is discussed in part 3 of this tutorial.