From 0b1efa3044468577a1352695cf4f65b41d439fb6 Mon Sep 17 00:00:00 2001 From: bilt Date: Wed, 16 Jul 2025 16:25:01 +0800 Subject: [PATCH] notion-share_url --- notion.py | 105 +++++++++++++++++++++++++++++++++++++++++++++++ py_share.py | 8 ++-- requirements.txt | 1 + 3 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 notion.py diff --git a/notion.py b/notion.py new file mode 100644 index 000000000..f1f740d4a --- /dev/null +++ b/notion.py @@ -0,0 +1,105 @@ +import os +from notion_client import Client +from datetime import datetime + +# 设置你的 Notion API Token 和 数据库 ID +NOTION_TOKEN = "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" +NOTION_DATABASE_ID = "230de91f217680388daed107820a5dc8" + +if not NOTION_TOKEN or not NOTION_DATABASE_ID: + print("请设置 NOTION_TOKEN 和 NOTION_DATABASE_ID 环境变量。") + exit() + +# 初始化 Notion 客户端 +notion = Client(auth=NOTION_TOKEN) + +def get_all_records_from_database(): + """ + 从 Notion 数据库获取所有记录的 ID。 + """ + records_id = [] + has_more = True + next_cursor = None + + print(f"正在从数据库 '{NOTION_DATABASE_ID}' 获取记录...") + while has_more: + try: + # 查询数据库 + # 注意: Notion API 对查询结果有分页限制 (通常每页 100 条) + response = notion.databases.query( + database_id=NOTION_DATABASE_ID, + start_cursor=next_cursor + ) + + for page in response["results"]: + records_id.append(page["id"]) + + has_more = response["has_more"] + next_cursor = response["next_cursor"] + + except Exception as e: + print(f"获取记录失败: {e}") + break + + print(f"找到 {len(records_id)} 条记录。") + return records_id + +def archive_record(page_id): + """ + 归档(删除)指定的 Notion 页面(记录)。 + """ + try: + notion.pages.update( + page_id=page_id, + archived=True + ) + print(f"成功归档页面: {page_id}") + except Exception as e: + print(f"归档页面 {page_id} 失败: {e}") + +def delete_all_records(): + """ + 删除数据库中所有现有记录,然后插入新的记录。 + + Args: + new_record_data (list of dict): 包含要插入的新记录数据的列表, + 每个字典应包含 'name', 'tags', 'status' 等键。 + """ + print("\n--- 步骤 1: 删除(归档)所有现有记录 ---") + record_ids_to_delete = get_all_records_from_database() + + if not record_ids_to_delete: + print("数据库中没有需要删除的记录。") + else: + for page_id in record_ids_to_delete: + archive_record(page_id) + print("所有现有记录已归档。") + +def add_record_to_notion_database(url): + """ + 向 Notion 数据库插入一条新记录。 + + """ + current_datetime = datetime.now() + formatted_datetime = current_datetime.strftime("%Y-%m-%d %H:%M:%S") + + try: + new_page = notion.pages.create( + parent={"database_id": NOTION_DATABASE_ID}, + properties={ + "更新时间": { + "title": [ + { + "text": {"content": formatted_datetime} + } + ] + }, + "地址": { + "url": url # <<--- 传递一个简单的 URL 字符串 + } + # 根据你的数据库结构添加更多属性 + }, + ) + print(f"成功插入记录: {url}") + except Exception as e: + print(f"插入记录失败: {e}") diff --git a/py_share.py b/py_share.py index d1f8d8160..2bd141fdf 100644 --- a/py_share.py +++ b/py_share.py @@ -1,8 +1,6 @@ import threading -import time -import subprocess -import os +import notion from pyngrok import ngrok, conf # Replace 'YOUR_NGROK_AUTH_TOKEN' with your actual token @@ -32,6 +30,10 @@ def zrok_thread(port): proxy = ProxyShare.create(root=zrok_env, target=f"http://127.0.0.1:{port}") # print(f"Public URL: {proxy.endpoints}") + for share_url in proxy.endpoints: + print(share_url) + notion.add_record_to_notion_database(share_url) + proxy.run() diff --git a/requirements.txt b/requirements.txt index 96597dba6..e4c9bee32 100644 --- a/requirements.txt +++ b/requirements.txt @@ -21,6 +21,7 @@ tqdm psutil alembic SQLAlchemy +notion-client #non essential dependencies: kornia>=0.7.1