如何把連接 Kobo 閱讀器與 Notion 筆記軟體?

2024/04/12閱讀時間約 10 分鐘

我時常用 Kobo 閱讀器來看電子書,在閱讀的過程中,我會順手把看了有感覺的句子標註起來,另外我也會用 Notion 來記錄我的閱讀清單,我想利用程式自動把 kobo 閱讀器裡的標註段落(Highlights)匯入到 Notion 裡,該如何做呢?


步驟 1: 建立 Notion integration

要與 Notion 互動,第一步是要建立 Integration. 打開 https://www.notion.so/my-integrations 並且點選 「New integrations」,例如我建立了「kobo-export」,

raw-image

建立好 integration,進去該頁面,可以得到 Integration Secret (晚點程式需要)

raw-image



步驟 2: 將閱讀清單頁面連上integration

在 Notion 頁面右上角有「…」,選擇 Connect → export-kobo,將頁面連結上 integration,程式才有權限讀寫該頁面。

raw-image



步驟 3: 取得閱讀清單頁面的database id

連上 web 版 Notion,選取讀清單頁面,我們可以從 url 取得database id

例如閱讀清單頁面 的URL 是

https://www.notion.so/xxxx/2a6a8e2667894f4e9ea4ea5191e98f26

database id 就是 2a6a8e2667894f4e9ea4ea5191e98f26


步驟 4: 取得 Kobo sqlite database

將 Kobo 閱讀器連接上電腦,在隱藏資料夾「./kobo 」下有 KoboReader.sqlite,裡面存有閱讀器上標註的 Highlights,將該檔案與 export 程式放在同一目錄下。


步驟5: 建立 export 程式

export.py 裡面,我們先要準備 .env file,裡面有 NOTION_KEY(步驟1取得) 和 NOTION_DB_ID (步驟3取得)

NOTION_KEY=secret_xxxxxxxxxxxxxxxxxxx
NOTION_DB_ID=2a6a8e2667894f4e9ea4ea5191e98f26

利用 NOTION_KEY,我們可以 create notion client

from notion_client import Client

self.client_secret = os.getenv('NOTION_KEY')
self.client = Client(auth=self.client_secret)

假設我們打算要匯入《明日,明日,又明日》這本書的 highlights,

我們要先在閱讀清單頁面找到該書的頁面(如下)

raw-image

我們利用 notion client 來 query database

def query_parent_page(self, title): 
try:
my_page = self.client.databases.query(
**{ "database_id": self.reading_db_id,
"filter": {
"property": "Title",
"rich_text": {
"contains": title,
},
},
}
)
if my_page:
return my_page["results"][0]['id']
else:
return None
except APIResponseError as error:
print(error)

找到該書的頁面之後,我們要在這個頁面下面建立一個「Kobo Highlights」的頁面,

def create_highlight_page(self, target_page_id):
highlight_page = self.client.pages.create(
**{ "parent": {
"type": "page_id",
"page_id": target_page_id
},
"properties": {
"title": [
{
"text": {
"content": "Kobo Highlights"
}
}
]
}
}
)
return highlight_page['id']

結果就像這樣

raw-image

建立好頁面,我們就可以從 KoboReader.sqlite 讀取 Highlights,並寫到新建立的 「Kobo Highlights」頁面

def export_highlight_to_notion(self, title): 
books_in_file = pd.read_sql("select c.ContentId AS 'Content ID',
c.Title AS 'Book Title' from content As c。
where c.Title like '%"+title+"%'", self.conn)
if len(books_in_file):
print(f"step3: find book in kobo db, title = {title}, books number = {len(books_in_file)}")
for i in range(0,len(books_in_file)):
bookmark_df = pd.read_sql("SELECT VolumeID AS 'Volume ID',
Text AS 'Highlight', Annotation, DateCreated AS 'Created On',
Type " + "FROM Bookmark Where VolumeID = '" + books_in_file['Content ID'][i] + "'" " ORDER BY 4 ASC", self.conn)
print(f"step4: write highlight into notion, highlight number = {len(bookmark_df)}")
for j in range(0, len(bookmark_df)):
if bookmark_df['Highlight'][j] != None: bookmark_df['Highlight'][j] = bookmark_df['Highlight'][j].strip()
for x in range(0, len(bookmark_df)):
if bookmark_df['Type'][x] == 'highlight':
self.write_text(target_page_id, bookmark_df['Highlight'][x], 'paragraph')
else:
if bookmark_df['Annotation'][x] != None:
self.write_text(target_page_id, bookmark_df['Annotation'][x], 'quote')
if bookmark_df['Highlight'][x] != None:
self.write_text(target_page_id, bookmark_df['Highlight'][x], 'paragraph')

全部的程式碼我放在github:


13會員
14內容數
留言0
查看全部
發表第一個留言支持創作者!