Skip to content

Instantly share code, notes, and snippets.

@ramandv
Last active December 20, 2015 14:19
Show Gist options
  • Save ramandv/6146111 to your computer and use it in GitHub Desktop.
Save ramandv/6146111 to your computer and use it in GitHub Desktop.
from evernote.api.client import NoteStore
from evernote.api.client import EvernoteClient
#Get your dev token from the following URL
#https://www.evernote.com/api/DeveloperToken.action
dev_token = "<your api token>"
client = EvernoteClient(token=dev_token, sandbox=False)
noteStore = client.get_note_store()
filter = NoteStore.NoteFilter()
#Find the guid of your catch notebook, by selecting the catch notebook and see the URL
#https://www.evernote.com/Home.action#b=<guid>
filter.notebookGuid = "<catch_notebook_guid>"
spec = NoteStore.NotesMetadataResultSpec()
spec.includeTitle = True
spec.includeTagGuids = True
index = 0
while True:
noteList = noteStore.findNotesMetadata( dev_token, filter, index, 50, spec)
for note in noteList.notes:
index = index + 1
if (note.tagGuids != None and len(note.tagGuids) > 0):
print "already tags present, so skipping...."
print "--------------[" + str(index) + "]"
continue
note = noteStore.getNote(dev_token, note.guid, True, True, True, True)
content = note.content
print 'Title:', note.title,
print 'Content:', content
print "--------------[" + str(index) + "]"
titlecontent = note.title + " " + content
#Remove HTML tags
titlecontent = re.sub('<[^<]+?>', ' ', titlecontent)
#get tags
tags = [word for word in titlecontent.split() if word.startswith('#') and len(word) > 1]
#remove unwanted characters
tags = [word.strip(" #;.,").lower() for word in tags ]
tags = [word.split()[0] for word in tags if len(word.split()) > 0 ]
tags = [word.strip(" #;.,") for word in tags if len(word.strip(" #;.,")) > 0 ]
#remove duplicate tags
tags_set = set(tags)
tags = list(tags_set)
#Add tags only if the actual note does not contain tags
if len(tags) > 0 and (note.tagNames == None or len(note.tagNames) == 0):
print "[" + str(index) + "]###########" + str(tags)
note.tagNames = tags
noteStore.updateNote(dev_token, note)
if noteList.totalNotes <= index:
break;
print "FETCHING NEXT PAGE======================="
@atrick
Copy link

atrick commented Aug 11, 2013

Worked for me!
Just needed to add import re.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment