Skip to content

Instantly share code, notes, and snippets.

@lookingcloudy
Last active August 29, 2015 14:08
Show Gist options
  • Save lookingcloudy/81b15a464f428fc7d708 to your computer and use it in GitHub Desktop.
Save lookingcloudy/81b15a464f428fc7d708 to your computer and use it in GitHub Desktop.
Evernote - normalize dates in titles using python
######
## normalize_dates.py
## this will search for any 1* or 2* in the title, then will process each note returned using pythons regular expressions
## if a date is detected in any of these formats: YYMMDD, YYYYMMDD, YY-MM-DD, YY_MM_DD, YYYY_MM_DD, YYYY-MM-DD
## it is rewritten to a normalized format
##
## I tend to prefix note titles with a date format, but am not very consistent. This will normalize all those into the
## same format. It will not fix m/d/y date formats, since I am not interested in those, but feel free to add it!
##
## Developer documentation: https://dev.evernote.com/doc/
## API documentation: https://dev.evernote.com/doc/reference/
## 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 *
import re
config = {
"developerToken": "get this here: https://www.evernote.com/api/DeveloperToken.action",
"sandbox": False
}
rematch = [
{
"Description": "YYYYMMDD - with any separator",
"find": r'((?:19|20)\d\d)[- /._]?(0[1-9]|1[012])[- /._]?(0[1-9]|[12][0-9]|3[01])',
"replace": r'\1_\2_\3'
},
{
"description": "YYYYMM with any separator",
"find": r'((?:19|20)\d\d)[- /._]?(0[1-9]|1[012])([- ])',
"replace": r'\1_\2\3'
}
]
#connect and load a list of tags and notebooks
def connect():
global client, userStore, noteStore, tags, notebooks
client = EvernoteClient(token=config['developerToken'], sandbox=config["sandbox"])
userStore = client.get_user_store()
noteStore = client.get_note_store()
tags = noteStore.listTags()
notebooks = noteStore.listNotebooks()
#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):
for i, note in enumerate(notes):
oldtitle = note.title
for j, item in enumerate(rematch):
note.title = re.sub(item["find"], item["replace"], note.title)
if note.title != oldtitle:
print oldtitle + ' ->> ' + note.title
noteStore.updateNote(note)
def autoFile():
spec = NotesMetadataResultSpec()
spec.includeTitle = True
spec.includeCreated = True
spec.includeTagGuids = True
#find all notes where the title has a string starting with 1 or 2
filter = NoteFilter()
filter.words = "any: intitle:1* intitle:2*"
pageSize = 200
startIndex = 0
moreNotes = True
while moreNotes:
results = noteStore.findNotesMetadata(filter, startIndex, pageSize, spec)
print results.startIndex, results.totalNotes
if results.notes:
startIndex += len(results.notes)
moreNotes = (results.totalNotes > startIndex)
else:
moreNotes = False
fileNotes(results.notes)
def main():
connect()
autoFile()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment