翻看印象筆記,發(fā)現(xiàn)已經(jīng)堅(jiān)持記了 1000+ 的日記,格式都是流水賬格式。
最近覺得『晨間日記』和『格志日記』的模板方法蠻好的,可以比較容易的引導(dǎo)自己思考和回顧一天,
但是直接使用格志日記 APP 又覺得喪失了自由度,不如在印象筆記里面寫起來隨性好控制。
印象筆記本身有一些模板,但是使用起來并不方便,
每天寫日記還得復(fù)制一遍模板到對(duì)應(yīng)的筆記本。于是想每天自動(dòng)根據(jù)指定的模板生成日記,
并將標(biāo)題置為當(dāng)天的日期。
看了下 IFTTT,發(fā)現(xiàn)里面的印象筆記模塊只能做創(chuàng)建、
更新等簡(jiǎn)單操作,無法滿足上述需求。
于是研究了下印象筆記的 API,可以用 Python SDK 比較方便的寫腳本解決這個(gè)問題(需要有基本的python知識(shí))。
擴(kuò)展開來,也可以參考這個(gè)做一些類似的對(duì)于印象筆記的自動(dòng)化操作。
使用流程
創(chuàng)建筆記模板
在印象筆記里新建一條筆記,作為你自己的日記模板。具體內(nèi)容可以參考晨間日記和格志日記的方法(文末有參考鏈接)。
記住你創(chuàng)建的筆記標(biāo)題,一會(huì)兒腳本里要用到。
獲取 developer token
如果你還沒有獲取過 developer token,訪問 https://app.yinxiang.com/api/DeveloperToken.action。
生成并記錄你的 Token。
安裝 evernote sdk,測(cè)試腳本
安裝 evernote sdk
pip install evernote
將下列代碼復(fù)制到 python 文件中,修改對(duì)應(yīng)的配置,并在本地運(yùn)行測(cè)試。
代碼也可參見: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ū)域,請(qǐng)自行修改
- auth_token: 訪問 https://app.yinxiang.com/api/DeveloperToken.action 生成
- diary_template_name:日記模板名稱,請(qǐng)保證有且僅有一個(gè)標(biāo)題為這個(gè)的筆記
- diary_notebook_name:復(fù)制生成的筆記要放入哪個(gè)筆記本,填寫筆記本名稱
"""
auth_token = "your token here"
diary_template_name = '日記模板'
diary_notebook_name = 'diary'
# 日記標(biāo)題。個(gè)人習(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ù)器定時(shí)執(zhí)行
本地測(cè)試腳本沒問題的話,可以部署到服務(wù)器上,使用 crontab 每天定時(shí)執(zhí)行。
參考配置(每天 00:01 執(zhí)行,創(chuàng)建日記):
1 0 * * * bash -c 'cd ~/scripts && python create_dairy_from_template.py >> ~/scripts/create_dairy.log 2>&1'