翻看印象筆記妥箕,發(fā)現(xiàn)已經(jīng)堅持記了 1000+ 的日記鸿摇,格式都是流水賬格式吨凑。
最近覺得『晨間日記』和『格志日記』的模板方法蠻好的,可以比較容易的引導(dǎo)自己思考和回顧一天户辱,
但是直接使用格志日記 APP 又覺得喪失了自由度,不如在印象筆記里面寫起來隨性好控制糙臼。
印象筆記本身有一些模板庐镐,但是使用起來并不方便,
每天寫日記還得復(fù)制一遍模板到對應(yīng)的筆記本变逃。于是想每天自動根據(jù)指定的模板生成日記必逆,
并將標(biāo)題置為當(dāng)天的日期。
看了下 IFTTT,發(fā)現(xiàn)里面的印象筆記模塊只能做創(chuàng)建名眉、
更新等簡單操作粟矿,無法滿足上述需求。
于是研究了下印象筆記的 API损拢,可以用 Python SDK 比較方便的寫腳本解決這個問題(需要有基本的python知識)陌粹。
擴(kuò)展開來,也可以參考這個做一些類似的對于印象筆記的自動化操作福压。
使用流程
創(chuàng)建筆記模板
在印象筆記里新建一條筆記掏秩,作為你自己的日記模板。具體內(nèi)容可以參考晨間日記和格志日記的方法(文末有參考鏈接)荆姆。
記住你創(chuàng)建的筆記標(biāo)題蒙幻,一會兒腳本里要用到。
獲取 developer token
如果你還沒有獲取過 developer token胆筒,訪問 https://app.yinxiang.com/api/DeveloperToken.action邮破。
生成并記錄你的 Token。
安裝 evernote sdk仆救,測試腳本
安裝 evernote sdk
pip install evernote
將下列代碼復(fù)制到 python 文件中抒和,修改對應(yīng)的配置,并在本地運行測試派桩。
代碼也可參見:https://github.com/huwenchao/mywiki/blob/master/codes/create_evernote_dairy_from_template.py
# -*- coding: utf-8 -*-
"""
搜索并復(fù)制指定的印象筆記模板构诚,到指定的文件夾。
"""
import logging
import datetime
import evernote.edam.userstore.constants as UserStoreConstants
import evernote.edam.type.ttypes as Types
from evernote.api.client import EvernoteClient
from evernote.edam.notestore import NoteStore
logging.basicConfig(level=logging.INFO)
"""
配置區(qū)域铆惑,請自行修改
- auth_token: 訪問 https://app.yinxiang.com/api/DeveloperToken.action 生成
- diary_template_name:日記模板名稱范嘱,請保證有且僅有一個標(biāo)題為這個的筆記
- diary_notebook_name:復(fù)制生成的筆記要放入哪個筆記本,填寫筆記本名稱
"""
auth_token = "your token here"
diary_template_name = '日記模板'
diary_notebook_name = 'diary'
# 日記標(biāo)題员魏。個人習(xí)慣用形如 『20170325(周六)』這樣的標(biāo)題丑蛤,可以根據(jù)自己的需求修改。
weekday_chinese_map = {
0: '周一',
1: '周二',
2: '周三',
3: '周四',
4: '周五',
5: '周六',
6: '周日',
}
now = datetime.datetime.now()
diary_title = '%s(%s)' % (now.strftime('%Y%m%d'),
weekday_chinese_map[now.weekday()])
logging.info('diary_title: %s', diary_title)
client = EvernoteClient(token=auth_token, service_host='app.yinxiang.com')
user_store = client.get_user_store()
note_store = client.get_note_store()
# 定位日記所在筆記本 guid
notebooks = note_store.listNotebooks()
logging.debug('Found %s notebooks', len(notebooks))
for notebook in notebooks:
logging.debug('guid: [%s], notebook [%s]', notebook.guid, notebook.name)
if notebook.name == diary_notebook_name:
logging.info('found diary notebook! guid: [%s], notebook [%s]',
notebook.guid, notebook.name)
diary_notebook_guid = notebook.guid
break
else:
logging.critical('diary [%s] not found', diary_notebook_name)
exit(1)
# 定位日記模板 guid
noteFilter = NoteStore.NoteFilter(words=diary_template_name)
spec = NoteStore.NotesMetadataResultSpec()
nmdList = note_store.findNotesMetadata(noteFilter, 0, 250, spec)
logging.debug('nmdList: %s', nmdList)
for n in nmdList.notes:
note = note_store.getNote(n.guid, True, True, False, False)
logging.debug('guid: [%s], title: [%s]', note.guid, note.title)
if note.title == diary_template_name:
logging.info('found diary template note! guid: [%s], title: [%s]',
note.guid, note.title)
diary_template_guid = note.guid
break
else:
logging.critical('diary_template [%s] not found', diary_template_name)
exit(1)
# 復(fù)制模板撕阎,生成筆記受裹,修改標(biāo)題
res_note = note_store.copyNote(diary_template_guid, diary_notebook_guid)
res_note.title = diary_title
res_note = note_store.updateNote(res_note)
logging.info('create diary for %s done!', now)
部署到服務(wù)器定時執(zhí)行
本地測試腳本沒問題的話,可以部署到服務(wù)器上虏束,使用 crontab 每天定時執(zhí)行棉饶。
參考配置(每天 00:01 執(zhí)行,創(chuàng)建日記):
1 0 * * * bash -c 'cd ~/scripts && python create_dairy_from_template.py >> ~/scripts/create_dairy.log 2>&1'