import os import requests import json from dotenv import load_dotenv load_dotenv(dotenv_path="/home/node/clawd/.env") def find_db_by_name(query_name): token = os.getenv("NOTION_API_KEY") headers = { "Authorization": f"Bearer {token}", "Content-Type": "application/json", "Notion-Version": "2022-06-28" } url = "https://api.notion.com/v1/search" payload = { "query": query_name, "filter": {"value": "database", "property": "object"} } # print(f"Searching for '{query_name}' database...") resp = requests.post(url, headers=headers, json=payload) if resp.status_code != 200: print(f"Error searching DB: {resp.text}") return None results = resp.json().get("results", []) if not results: # print(f"No database named '{query_name}' found via search.") return None db = results[0] return db['id'] def dump_db_content(db_id, db_name="DB"): token = os.getenv("NOTION_API_KEY") headers = { "Authorization": f"Bearer {token}", "Content-Type": "application/json", "Notion-Version": "2022-06-28" } # Get all pages url = f"https://api.notion.com/v1/databases/{db_id}/query" resp = requests.post(url, headers=headers, json={}) if resp.status_code != 200: print(f"Error querying DB: {resp.text}") return pages = resp.json().get("results", []) print(f"\n--- Content of '{db_name}' ({len(pages)} rows) ---") rows = [] for page in pages: props = page['properties'] # Extract Name (Title) - Robust Logic name = "N/A" if "Vertical" in props and props["Vertical"]["title"]: name = props["Vertical"]["title"][0]["plain_text"] elif "Name" in props and props["Name"]["title"]: name = props["Name"]["title"][0]["plain_text"] elif "Role" in props and props["Role"]["title"]: name = props["Role"]["title"][0]["plain_text"] # Extract Status/Freigabe freigabe = "" if "Freigabe" in props: if props["Freigabe"]["type"] == "status": freigabe = props["Freigabe"]["status"]["name"] if props["Freigabe"]["status"] else "" elif props["Freigabe"]["type"] == "select": freigabe = props["Freigabe"]["select"]["name"] if props["Freigabe"]["select"] else "" # Extract Notes notes = "" if "Notes" in props and props["Notes"]["rich_text"]: notes = props["Notes"]["rich_text"][0]["plain_text"] # Extract KPIs kpis = "" for kpi_key in ["KPIs", "KPI", "Quantitative Value"]: if kpi_key in props and props[kpi_key]["rich_text"]: kpis = props[kpi_key]["rich_text"][0]["plain_text"] break rows.append({"name": name, "freigabe": freigabe, "notes": notes, "kpis": kpis}) # Print clean table print(f"{'Name':<40} | {'Freigabe':<15} | {'KPIs':<20} | {'Notes'}") print("-" * 120) for r in rows: # Nur Zeilen mit Inhalt anzeigen (Filter empty names) if r['name'] != "N/A": print(f"{r['name']:<40} | {r['freigabe']:<15} | {r['kpis']:<20} | {r['notes']}") if __name__ == "__main__": db_id_ind = find_db_by_name("Industries") if db_id_ind: dump_db_content(db_id_ind, "Industries") db_id_roles = find_db_by_name("Personas") if db_id_roles: dump_db_content(db_id_roles, "Personas")