Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 43 additions & 5 deletions scripts/weread.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,25 @@ def get_review_list(bookId):
def check(bookId):
"""检查是否已经插入过 如果已经插入了就删除"""
filter = {"property": "BookId", "rich_text": {"equals": bookId}}
response = client.databases.query(database_id=database_id, filter=filter)
try:
response = client.databases.query(database_id=database_id, filter=filter)
except AttributeError:
try:
response = client.databases.query_database(database_id=database_id, filter=filter)
except AttributeError:
url = f"https://api.notion.com/v1/databases/{database_id}/query"
auth_token = os.getenv("NOTION_TOKEN")
if not auth_token:
raise Exception("无法获取 Notion API token")
headers = {
"Authorization": f"Bearer {auth_token}",
"Notion-Version": "2022-06-28",
"Content-Type": "application/json"
}
payload = {"filter": filter}
resp = requests.post(url, json=payload, headers=headers)
resp.raise_for_status()
response = resp.json()
for result in response["results"]:
try:
client.blocks.delete(block_id=result["id"])
Expand Down Expand Up @@ -222,9 +240,29 @@ def get_sort():
"direction": "descending",
}
]
response = client.databases.query(
database_id=database_id, filter=filter, sorts=sorts, page_size=1
)
try:
response = client.databases.query(
database_id=database_id, filter=filter, sorts=sorts, page_size=1
)
except AttributeError:
try:
response = client.databases.query_database(
database_id=database_id, filter=filter, sorts=sorts, page_size=1
)
except AttributeError:
url = f"https://api.notion.com/v1/databases/{database_id}/query"
auth_token = os.getenv("NOTION_TOKEN")
if not auth_token:
raise Exception("无法获取 Notion API token")
headers = {
"Authorization": f"Bearer {auth_token}",
"Notion-Version": "2022-06-28",
"Content-Type": "application/json"
}
payload = {"filter": filter, "sorts": sorts, "page_size": 1}
resp = requests.post(url, json=payload, headers=headers)
resp.raise_for_status()
response = resp.json()
if len(response.get("results")) == 1:
return response.get("results")[0].get("properties").get("Sort").get("number")
return 0
Expand Down Expand Up @@ -438,4 +476,4 @@ def extract_page_id():
children, grandchild = get_children(chapter, summary, bookmark_list)
results = add_children(id, children)
if len(grandchild) > 0 and results != None:
add_grandchild(grandchild, results)
add_grandchild(grandchild, results)