Last active
August 21, 2024 01:47
-
-
Save zengjie/efd11bcac9e3f09d5e694348b49b9a3f to your computer and use it in GitHub Desktop.
把 Flomo 同步到 Notion 的得到电子书笔记,导出到 ReadWise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
from notion_client import Client | |
from notion_client.helpers import iterate_paginated_api | |
# Flomo 在 Notion 中的同步数据库需要增加两个字段: | |
# - 得到电子书:formula 字段,公式为 `contains(prop("Tags"), "得到/电子书")` | |
# - 书名:formula 字段,公式为 `replaceAll(prop("Tags"), "得到/电子书/", "")` | |
NOTION_TOKEN = '<notion-auth-token>' | |
FLOMO_DB_ID = '<flomo-sync-database-id>' | |
notion = Client(auth=NOTION_TOKEN) | |
pages = iterate_paginated_api(notion.databases.query, **{ | |
"database_id": FLOMO_DB_ID, | |
"filter": { | |
"property": "得到电子书", | |
"formula": { | |
"checkbox": { | |
"equals": True | |
} | |
} | |
} | |
}) | |
with open('dedao.csv', 'w', encoding='utf-8') as csvfile: | |
fieldnames = ["Highlight", "Title", "Author", "URL", "Note", "Location", "Date"] | |
writer = csv.DictWriter(csvfile, fieldnames=fieldnames) | |
writer.writeheader() | |
for page in pages: | |
for result in page: | |
page_id = result["id"] | |
props = result["properties"] | |
book_title = props["书名"]["formula"]["string"] | |
created_at = props["Created At"]["date"]["start"] | |
blocks = notion.blocks.children.list(page_id).get("results") | |
block = blocks[0] | |
block_text = block["paragraph"]["rich_text"][0]["plain_text"] | |
lines = block_text.splitlines() | |
highlight_text = lines[-3] | |
note_text = lines[1] if len(lines) == 5 else "" | |
writer.writerow({ | |
"Highlight": highlight_text, | |
"Title": book_title, | |
"Author": "", | |
"URL": "", | |
"Note": note_text, | |
"Location": "", | |
"Date": created_at, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment