getDB.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Wed Jul 23 20:37:12 2025
  4. @author: USER
  5. """
  6. from notion_client import Client
  7. import csv
  8. # --- CONFIG ---
  9. NOTION_TOKEN = "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"
  10. PAGE_ID = "d95dd2dd7eb14634819dadb16b738d22"
  11. TODO_PREFIX = "To do du projet @"
  12. notion = Client(auth=NOTION_TOKEN)
  13. # --- UTILS ---
  14. def get_children(block_id):
  15. try:
  16. res = notion.blocks.children.list(block_id)
  17. return res.get("results", [])
  18. except Exception:
  19. return []
  20. def extract_text(block):
  21. content = block.get(block["type"], {})
  22. for key in ("rich_text", "title"):
  23. if isinstance(content.get(key), list):
  24. return "".join(t.get("plain_text", "") for t in content[key])
  25. return ""
  26. # --- Explorer récursivement la page pour trouver la base dans un toggle ---
  27. def find_database_in_page(page_id):
  28. for block in get_children(page_id):
  29. if block["type"] == "toggle":
  30. toggle_title = extract_text(block).strip()
  31. print(f"🧱 toggle | {toggle_title} | ID: {block['id']}")
  32. children = get_children(block["id"])
  33. for child in children:
  34. if child["type"] == "child_database":
  35. print(f"📘 Base trouvée dans toggle : ID = {child['id']}")
  36. return child["id"]
  37. print("❌ Aucune base trouvée dans un toggle.")
  38. return None