Skip to content

Instantly share code, notes, and snippets.

@lookingcloudy
Last active February 12, 2023 02:03
Show Gist options
  • Save lookingcloudy/8d4fe1d3c221b4428549 to your computer and use it in GitHub Desktop.
Save lookingcloudy/8d4fe1d3c221b4428549 to your computer and use it in GitHub Desktop.
Evernote - auto file notes, apply tags, and move to notebook, using Python
{
"configuration": {
"consumerKey": "not needed with personal account",
"consumerSecret": "not needed with personal account",
"developerToken": "Get this here in your personal production account https://www.evernote.com/api/DeveloperToken.action"
},
"autofile": [
{
"search": "notebook:!!inbox myBalsamiq",
"notebook": "21 - Expense Reports",
"tags": ["@Work", "work-expense"],
"title": "balsamiq"
},
{
"search": "notebook:!!inbox suntrust retirement",
"notebook": "10-Personal",
"tags": ["@Home", "bl-investment", "ref-401k"],
"title": "SunTrust - IRA"
},
{
"search": "notebook:!!inbox 1745000000",
"notebook": "10-Personal",
"tags": ["@Home", "bl-utility"],
"title": "BOCC"
}
]
}
######
## autofile.py
## search evernote api for key words, then automatically apply tags, ttitle, and move to specified notebook
## This is using my personal Evernote account, therefore, no need to use oAuth. Just need a developertoken from
## my account. Be sure to set "sandbox = False" when using a production account.
##
## Developer documentation: https://dev.evernote.com/doc/
## API documentation: https://dev.evernote.com/doc/reference/
## Sandbox Developer Token: https://sandbox.evernote.com/api/DeveloperToken.action
## Production Developer Token: https://www.evernote.com/api/DeveloperToken.action
from evernote.api.client import EvernoteClient
import evernote.edam.type.ttypes as Types
import json
from pprint import pprint
from datetime import datetime
from evernote.edam.notestore.ttypes import *
#load the JSON configuration file
def getConfiguration():
global config, autofile
config = json.load(open('autofile.json'))
autofile = config['autofile']
config = config['configuration']
#connect and load a list of tags and notebooks
def connect():
global client, userStore, noteStore, tags, notebooks
client = EvernoteClient(token=config['developerToken'], sandbox=False)
userStore = client.get_user_store()
noteStore = client.get_note_store()
tags = noteStore.listTags()
notebooks = noteStore.listNotebooks()
# pprint(tags)
# pprint(notebooks)
#called once for each search mapping - converts list of tag names to list of tag guids
def tags_to_guids(taglist):
for i, name in enumerate(taglist):
# lookup by name in the list of tag objects
guid = next((item.guid for item in tags if item.name.lower() == name.lower()), None)
# if it was not found, create it
if not guid:
tag = Types.Tag()
tag.name = name
tag = noteStore.createTag(tag)
guid = tag.guid
taglist[i] = guid
return taglist
#converts notebook name to guid
def notebook_to_guid(nb):
guid = next((item.guid for item in notebooks if item.name.lower() == nb.lower()), None)
return guid
def fileNotes(notes, mapitem):
for i, item in enumerate(notes):
note = item
#build a new title
# if not note.title.strip()[:1].isdigit():
note.title = datetime.fromtimestamp(note.created / 1e3 ).strftime('%Y-%m-%d') + ' - ' + mapitem['title']
print note.title
note.tagGuids = mapitem['tags']
note.notebookGuid = mapitem['notebook']
noteStore.updateNote(note)
def autoFile():
spec = NotesMetadataResultSpec()
spec.includeTitle = True
spec.includeCreated = True
spec.includeTagGuids = True
for i, item in enumerate(autofile):
item['tags'] = tags_to_guids(item['tags'])
item['notebook'] = notebook_to_guid(item['notebook'])
filter = NoteFilter()
filter.words = item['search']
results = noteStore.findNotesMetadata(filter, 0, 9999, spec)
fileNotes(results.notes, item)
def main():
getConfiguration()
connect()
autoFile()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment