去年寫(xiě)過(guò)一篇文章:https://zhuanlan.zhihu.com/p/24545638?utm_source=itdadao&utm_medium=referral
其中分享了如何用印象筆記的開(kāi)發(fā)者接口,用php開(kāi)發(fā)一個(gè)收集金句的小工具桨螺。
今年換了Macbook,之前的環(huán)境和工具都沒(méi)了,于是使用Python3從頭寫(xiě)了一個(gè)同樣的工具歌亲。
首先渣窜,因?yàn)橛∠蠊P記官方只提供Python2的SDK枣耀,所以去Github下載非官方的Python3 SDK。
下載地址:https://github.com/evernote/evernote-sdk-python3
點(diǎn)擊綠色的Clone or Download按鈕较店,然后選擇Download ZIP。
解壓后打開(kāi)sample文件夾容燕,然后再進(jìn)入client文件夾梁呈,其中有個(gè)文件名叫EDAMTest.py
復(fù)制一份改名為yinxiang.py
在這個(gè)文件基礎(chǔ)上進(jìn)行修改,開(kāi)發(fā)屬于自己的工具缰趋,代碼如下捧杉。
# coding = utf-8
import hashlib
import binascii
import evernote.edam.userstore.constants as UserStoreConstants
import evernote.edam.type.ttypes as Types
import os
import time
import re
from evernote.api.client import EvernoteClient
from evernote.edam.notestore.ttypes import NoteFilter
from evernote.edam.type.ttypes import NoteSortOrder
# auth_token申請(qǐng)地址:https://dev.yinxiang.com/doc/articles/dev_tokens.php
auth_token = "S=s3:U=1d3c1:E=16081a4df70:C=15929f3b2d0:P=1cd:A=en-devtoken:V=2:H=f540d0359f7e0d5c52235eb1c19c9885"
# 關(guān)掉沙盒模式
sandbox = False
# True代表使用的國(guó)內(nèi)的印象筆記,而不是Evernote國(guó)際版
china = True
# 創(chuàng)建一個(gè)印象筆記client對(duì)象
client = EvernoteClient(token = auth_token, sandbox = sandbox, china = china)
# user_store = client.get_user_store()
# 通過(guò)client對(duì)象獲取note_store對(duì)象
note_store = client.get_note_store()
# 下面代碼為了獲取所有的筆記本數(shù)量秘血、筆記本名和對(duì)應(yīng)的GUID味抖,目前是注釋狀態(tài),為了找到對(duì)應(yīng)的筆記本GUID
# notebooks = note_store.listNotebooks()
# print("Found", len(notebooks), " notebooks:")
# for notebook in notebooks:
# print(notebook.name, notebook.guid)
# 生成筆記標(biāo)題灰粮,格式例如:20171113閱讀記錄
daily_title = time.strftime('%Y%m%d',time.localtime()) + "閱讀記錄"
# 生成查找筆記的規(guī)則仔涩,使用筆記創(chuàng)建排序,指定筆記本GUID粘舟,使用標(biāo)題名搜索
updated_filter = NoteFilter(order=NoteSortOrder.CREATED, notebookGuid="9e965c09-5045-4909-ad78-bf95a5bbc09d", words=daily_title)
# 偏移0
offset = 0
# 只取一條筆記
max_notes = 1
# 查找出符合條件的筆記
result_list = note_store.findNotes(updated_filter, offset, max_notes)
# 如何找到對(duì)應(yīng)筆記則熔脂,在這個(gè)筆記里加一條金句佩研,否則創(chuàng)建一條新筆記把金句加進(jìn)去
if result_list.totalNotes:
# 獲取找到筆記的GUID
note_guid = result_list.notes[0].guid
# 獲取到對(duì)應(yīng)筆記
note = note_store.getNote(note_guid,1,1,1,1)
# 使用正則匹配出筆記里的內(nèi)容文本
match_res = re.search(r'<en-note>(.*)<\/en-note>', note.content, re.M|re.I)
# 如果能匹配到內(nèi)容,則獲取內(nèi)容設(shè)置變量名為old_content霞揉,否則old_content為空
if match_res:
old_content = match_res.group(1)
else:
old_content = ""
# 讀取Mac系統(tǒng)tmp文件夾下的evernote.txt里的內(nèi)容旬薯,使用utf-8編碼
newline = open('/tmp/evernote.txt','r', encoding='utf-8').read()
# 構(gòu)建待更新的筆記內(nèi)容
note.content = '<?xml version="1.0" encoding="UTF-8"?>'
note.content += '<!DOCTYPE en-note SYSTEM ' \
'"http://xml.evernote.com/pub/enml2.dtd">'
note.content += '<en-note>' + old_content +'<br/>' + newline
note.content += '</en-note>'
# 更新找到的舊筆記內(nèi)容,把新的金句加進(jìn)去适秩。
res = note_store.updateNote(auth_token, note)
# 沒(méi)有找到舊的筆記绊序,則新建一個(gè)使用今天日期+閱讀記錄為名字的新筆記
else:
# 創(chuàng)建note對(duì)象
note = Types.Note()
# 設(shè)置筆記名稱
note.title = daily_title
# 讀取evenote.txt里的內(nèi)容
words = open('/tmp/evernote.txt','r',encoding='utf-8').read() + '<br/>'
# 構(gòu)建note的內(nèi)容,把句子加到<en-note>標(biāo)簽中
note.content = '<?xml version="1.0" encoding="UTF-8"?>'
note.content += '<!DOCTYPE en-note SYSTEM ' \
'"http://xml.evernote.com/pub/enml2.dtd">'
note.content += '<en-note>' + words
note.content += '</en-note>'
# 指定筆記本GUID
note.notebookGuid = "9e965c09-5045-4909-ad78-bf95a5bbc09d"
# 創(chuàng)建筆記
created_note = note_store.createNote(note)
# 打印創(chuàng)建好的筆記標(biāo)題和GUID
print(note.title + "創(chuàng)建成功秽荞,GUID: ", created_note.guid)
在以上內(nèi)容完成后骤公,打開(kāi)Automator工具,新建一個(gè)服務(wù)扬跋,然后點(diǎn)擊資源庫(kù)->實(shí)用工具->運(yùn)行 Shell腳本(雙擊)
先解釋下第一行
export PYTHONPATH=/Users/joe/Documents/codelife/python/evernote_py3/lib
這句為了指定python3印象筆記的SDK環(huán)境變量阶捆,否則命令行可以運(yùn)行,但在automator里不行钦听,會(huì)提示找不到對(duì)應(yīng)的evenote模塊洒试。
LANG=en_US.UTF-8 pbpaste -txt > /tmp/evernote.txt
請(qǐng)注意,LANG=en_US.UTF-8 很重要彪见,否則會(huì)造成亂碼儡司。
pbpaste是Linux類系統(tǒng)自帶的命令,可以讀取剪貼板里的數(shù)據(jù)余指。然后使用 “> /tmp/evernote.txt” 在/tmp目錄下輸入生成一個(gè)叫做evernote.txt的文件捕犬。
整個(gè)腳本的意思是:
先把剪貼板里的內(nèi)容存入臨時(shí)文件夾下的evernote.txt文件中,然后用Python3主文件去執(zhí)行yinxiang.py 這個(gè)文件寫(xiě)入印象筆記中酵镜。
對(duì)應(yīng)的Python3主文件地址在命令行下使用 which python3查詢碉碉。
整體代碼如下,請(qǐng)對(duì)應(yīng)修改自己的環(huán)境變量地址淮韭、python3文件地址和代碼地址垢粮。
export PYTHONPATH=/Users/joe/Documents/codelife/python/evernote_py3/lib
LANG=en_US.UTF-8 pbpaste -txt > /tmp/evernote.txt & /Library/Frameworks/Python.framework/Versions/3.5/bin/python3 /Users/joe/Documents/codelife/python/evernote_py3/sample/client/yinxiang.py
然后保存文件名為save2evenote
然后打開(kāi)“系統(tǒng)偏好設(shè)置”->“鍵盤(pán)”->“快捷鍵”->"服務(wù)"
這時(shí)你會(huì)發(fā)現(xiàn)有一個(gè)你剛才保存的服務(wù)會(huì)出現(xiàn)在這里,然后給它設(shè)置個(gè)快捷鍵靠粪,比如Ctrl+ALt+Command+V
至此蜡吧,終于一切都搞定了,以后遇到好段落和句子占键,只需要選中按下Command+C昔善,然后再按 Ctrl+ALt+Command+V 就自動(dòng)貼到印象筆記了。