用印象笔记的Python SDK接口开发个自用小工具

2017-11-13  本文已影响81人  向阳乔木

去年写过一篇文章:https://zhuanlan.zhihu.com/p/24545638?utm_source=itdadao&utm_medium=referral

其中分享了如何用印象笔记的开发者接口,用php开发一个收集金句的小工具。

今年换了Macbook,之前的环境和工具都没了,于是使用Python3从头写了一个同样的工具。

首先,因为印象笔记官方只提供Python2的SDK,所以去Github下载非官方的Python3 SDK。

下载地址:https://github.com/evernote/evernote-sdk-python3

点击绿色的Clone or Download按钮,然后选择Download ZIP。

解压后打开sample文件夹,然后再进入client文件夹,其中有个文件名叫EDAMTest.py

复制一份改名为yinxiang.py

在这个文件基础上进行修改,开发属于自己的工具,代码如下。

# 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申请地址: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"

# 关掉沙盒模式
sandbox = False
# True代表使用的国内的印象笔记,而不是Evernote国际版
china = True

# 创建一个印象笔记client对象
client = EvernoteClient(token = auth_token, sandbox = sandbox, china = china)

# user_store = client.get_user_store()
# 通过client对象获取note_store对象
note_store = client.get_note_store()

# 下面代码为了获取所有的笔记本数量、笔记本名和对应的GUID,目前是注释状态,为了找到对应的笔记本GUID
# notebooks = note_store.listNotebooks()
# print("Found", len(notebooks), " notebooks:")
# for notebook in notebooks:
#     print(notebook.name, notebook.guid)

# 生成笔记标题,格式例如:20171113阅读记录
daily_title = time.strftime('%Y%m%d',time.localtime()) + "阅读记录"

# 生成查找笔记的规则,使用笔记创建排序,指定笔记本GUID,使用标题名搜索
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)

# 如何找到对应笔记则,在这个笔记里加一条金句,否则创建一条新笔记把金句加进去
if result_list.totalNotes:
    
    # 获取找到笔记的GUID
    note_guid = result_list.notes[0].guid
    # 获取到对应笔记
    note = note_store.getNote(note_guid,1,1,1,1)
    # 使用正则匹配出笔记里的内容文本
    match_res = re.search(r'<en-note>(.*)<\/en-note>', note.content, re.M|re.I)
    # 如果能匹配到内容,则获取内容设置变量名为old_content,否则old_content为空
    if match_res:
        old_content = match_res.group(1)
    else:
        old_content = ""
    
    # 读取Mac系统tmp文件夹下的evernote.txt里的内容,使用utf-8编码
    newline = open('/tmp/evernote.txt','r', encoding='utf-8').read()


    # 构建待更新的笔记内容
    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>'
    # 更新找到的旧笔记内容,把新的金句加进去。
    res = note_store.updateNote(auth_token, note)
# 没有找到旧的笔记,则新建一个使用今天日期+阅读记录为名字的新笔记
else:
    # 创建note对象
    note = Types.Note() 
    # 设置笔记名称 
    note.title = daily_title 
    # 读取evenote.txt里的内容
    words = open('/tmp/evernote.txt','r',encoding='utf-8').read() + '<br/>'

    # 构建note的内容,把句子加到<en-note>标签中

    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"
    # 创建笔记
    created_note = note_store.createNote(note)
    # 打印创建好的笔记标题和GUID
    print(note.title + "创建成功,GUID: ", created_note.guid)

在以上内容完成后,打开Automator工具,新建一个服务,然后点击资源库->实用工具->运行 Shell脚本(双击)

15105707054299.jpg

先解释下第一行
export PYTHONPATH=/Users/joe/Documents/codelife/python/evernote_py3/lib

这句为了指定python3印象笔记的SDK环境变量,否则命令行可以运行,但在automator里不行,会提示找不到对应的evenote模块。

LANG=en_US.UTF-8 pbpaste -txt > /tmp/evernote.txt

请注意,LANG=en_US.UTF-8 很重要,否则会造成乱码。
pbpaste是Linux类系统自带的命令,可以读取剪贴板里的数据。然后使用 “> /tmp/evernote.txt” 在/tmp目录下输入生成一个叫做evernote.txt的文件。

整个脚本的意思是:

先把剪贴板里的内容存入临时文件夹下的evernote.txt文件中,然后用Python3主文件去执行yinxiang.py 这个文件写入印象笔记中。

对应的Python3主文件地址在命令行下使用 which python3查询。

整体代码如下,请对应修改自己的环境变量地址、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

然后打开“系统偏好设置”->“键盘”->“快捷键”->"服务"

15105712578089.jpg

这时你会发现有一个你刚才保存的服务会出现在这里,然后给它设置个快捷键,比如Ctrl+ALt+Command+V

至此,终于一切都搞定了,以后遇到好段落和句子,只需要选中按下Command+C,然后再按 Ctrl+ALt+Command+V 就自动贴到印象笔记了。

上一篇下一篇

猜你喜欢

热点阅读