116 lines
4.0 KiB
Python
116 lines
4.0 KiB
Python
import os
|
|
import requests
|
|
import json
|
|
from dotenv import load_dotenv
|
|
|
|
load_dotenv(dotenv_path="/home/node/clawd/.env")
|
|
|
|
NOTION_TOKEN = os.getenv("NOTION_API_KEY")
|
|
HEADERS = {
|
|
"Authorization": f"Bearer {NOTION_TOKEN}",
|
|
"Content-Type": "application/json",
|
|
"Notion-Version": "2022-06-28"
|
|
}
|
|
|
|
def find_db_id(query_name):
|
|
url = "https://api.notion.com/v1/search"
|
|
payload = {"query": query_name, "filter": {"value": "database", "property": "object"}}
|
|
resp = requests.post(url, headers=HEADERS, json=payload)
|
|
if resp.status_code == 200:
|
|
results = resp.json().get("results", [])
|
|
if results:
|
|
return results[0]['id']
|
|
return None
|
|
|
|
# Cache for product names to avoid API spam
|
|
product_cache = {}
|
|
|
|
def resolve_product_name(relation_ids):
|
|
if not relation_ids:
|
|
return "None"
|
|
|
|
names = []
|
|
for rel in relation_ids:
|
|
page_id = rel['id']
|
|
if page_id in product_cache:
|
|
names.append(product_cache[page_id])
|
|
continue
|
|
|
|
url = f"https://api.notion.com/v1/pages/{page_id}"
|
|
resp = requests.get(url, headers=HEADERS)
|
|
if resp.status_code == 200:
|
|
props = resp.json().get("properties", {})
|
|
# Assume Product DB has a Title field called "Name" or "Product Name"
|
|
# We iterate to find the title
|
|
title = "Unknown"
|
|
for key, val in props.items():
|
|
if val['id'] == 'title': # The title property always has id 'title'
|
|
if val['title']:
|
|
title = val['title'][0]['plain_text']
|
|
break
|
|
product_cache[page_id] = title
|
|
names.append(title)
|
|
else:
|
|
names.append("Error fetching Product")
|
|
|
|
return ", ".join(names)
|
|
|
|
def audit_industries():
|
|
db_id = find_db_id("Industries")
|
|
if not db_id:
|
|
print("❌ Industries DB not found.")
|
|
return
|
|
|
|
print(f"--- Auditing Industries DB ({db_id}) ---")
|
|
|
|
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: {resp.text}")
|
|
return
|
|
|
|
pages = resp.json().get("results", [])
|
|
|
|
# We want to see: Vertical Name | Status | Primary Product (Resolved) | Notes Snippet
|
|
print(f"{'Vertical':<35} | {'Status':<15} | {'Primary Product':<30} | {'Notes (Snippet)'}")
|
|
print("-" * 120)
|
|
|
|
for page in pages:
|
|
props = page['properties']
|
|
|
|
# Name
|
|
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"]: # Fallback
|
|
name = props["Name"]["title"][0]["plain_text"]
|
|
|
|
# Filter for the ones we touched or are interested in
|
|
# (Optional: remove filter to see all)
|
|
|
|
# Status
|
|
status = ""
|
|
if "Freigabe" in props:
|
|
if props["Freigabe"]["type"] == "status" and props["Freigabe"]["status"]:
|
|
status = props["Freigabe"]["status"]["name"]
|
|
elif props["Freigabe"]["type"] == "select" and props["Freigabe"]["select"]:
|
|
status = props["Freigabe"]["select"]["name"]
|
|
|
|
# Primary Product (Relation)
|
|
product_name = "None"
|
|
if "Primary Product Category" in props and props["Primary Product Category"]["relation"]:
|
|
product_name = resolve_product_name(props["Primary Product Category"]["relation"])
|
|
|
|
# Notes
|
|
notes = ""
|
|
if "Notes" in props and props["Notes"]["rich_text"]:
|
|
full_note = props["Notes"]["rich_text"][0]["plain_text"]
|
|
notes = (full_note[:40] + '...') if len(full_note) > 40 else full_note
|
|
|
|
if name != "N/A":
|
|
print(f"{name:<35} | {status:<15} | {product_name:<30} | {notes}")
|
|
|
|
if __name__ == "__main__":
|
|
audit_industries()
|