Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -331,6 +331,31 @@ async def get_page_title(page_id: str) -> str:
|
|
| 331 |
|
| 332 |
return "(No Title)"
|
| 333 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 334 |
|
| 335 |
async def apply_template_to_page(page_id: str):
|
| 336 |
"""Append التمبلت دي لجسم الصفحة (الـ body) باستخدام blocks API."""
|
|
@@ -367,7 +392,7 @@ async def webhook(request: Request):
|
|
| 367 |
print("\n===== NEW WEBHOOK EVENT =====")
|
| 368 |
print("Raw body:", body)
|
| 369 |
|
| 370 |
-
|
| 371 |
if event_type == "page.created":
|
| 372 |
title = await get_page_title(page_id)
|
| 373 |
|
|
@@ -376,11 +401,16 @@ async def webhook(request: Request):
|
|
| 376 |
print(f"Page ID : {page_id}")
|
| 377 |
print(f"Timestamp : {timestamp}")
|
| 378 |
|
| 379 |
-
#
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
|
| 385 |
elif event_type == "page.deleted":
|
| 386 |
print(f"Page deleted. ID: {page_id}, Timestamp: {timestamp}")
|
|
|
|
| 331 |
|
| 332 |
return "(No Title)"
|
| 333 |
|
| 334 |
+
async def is_block_archived(page_id: str) -> bool:
|
| 335 |
+
"""يرجع True لو البلوك/الصفحة archived."""
|
| 336 |
+
async with httpx.AsyncClient() as client:
|
| 337 |
+
res = await client.get(
|
| 338 |
+
f"https://api.notion.com/v1/blocks/{page_id}",
|
| 339 |
+
headers={
|
| 340 |
+
"Authorization": f"Bearer {NOTION_SECRET}",
|
| 341 |
+
"Notion-Version": NOTION_VERSION,
|
| 342 |
+
},
|
| 343 |
+
)
|
| 344 |
+
|
| 345 |
+
if res.status_code != 200:
|
| 346 |
+
print(f"[ARCHIVE CHECK] Failed to fetch block {page_id}, status: {res.status_code}")
|
| 347 |
+
try:
|
| 348 |
+
print("[ARCHIVE CHECK] Response:", res.json())
|
| 349 |
+
except Exception:
|
| 350 |
+
print("[ARCHIVE CHECK] Raw response:", res.text)
|
| 351 |
+
# لو مش عارفين حالته، هنعتبره مش archived عشان ما نكسرش الفلو
|
| 352 |
+
return False
|
| 353 |
+
|
| 354 |
+
data = res.json()
|
| 355 |
+
archived = data.get("archived", False)
|
| 356 |
+
print(f"[ARCHIVE CHECK] Block {page_id} archived = {archived}")
|
| 357 |
+
return archived
|
| 358 |
+
|
| 359 |
|
| 360 |
async def apply_template_to_page(page_id: str):
|
| 361 |
"""Append التمبلت دي لجسم الصفحة (الـ body) باستخدام blocks API."""
|
|
|
|
| 392 |
print("\n===== NEW WEBHOOK EVENT =====")
|
| 393 |
print("Raw body:", body)
|
| 394 |
|
| 395 |
+
# نتعامل مع إنشاء صفحة جديدة فقط
|
| 396 |
if event_type == "page.created":
|
| 397 |
title = await get_page_title(page_id)
|
| 398 |
|
|
|
|
| 401 |
print(f"Page ID : {page_id}")
|
| 402 |
print(f"Timestamp : {timestamp}")
|
| 403 |
|
| 404 |
+
# تشيك الأول: هل الصفحة دي Archived (زي لما تدوس Escape على placeholder)؟
|
| 405 |
+
archived = await is_block_archived(page_id)
|
| 406 |
+
if archived:
|
| 407 |
+
print(f"[TEMPLATE] Page {page_id} is archived right after creation. Skipping template apply.")
|
| 408 |
+
else:
|
| 409 |
+
# نطبّق التمبلت على الـ Page دي
|
| 410 |
+
try:
|
| 411 |
+
await apply_template_to_page(page_id)
|
| 412 |
+
except Exception as e:
|
| 413 |
+
print("[ERROR] While applying template:", e)
|
| 414 |
|
| 415 |
elif event_type == "page.deleted":
|
| 416 |
print(f"Page deleted. ID: {page_id}, Timestamp: {timestamp}")
|