Spaces:
Running
Running
#!/usr/bin/env python3 | |
""" | |
List all Notion databases accessible to the integration. | |
This script helps you find the correct database ID for your FoodWise inventory. | |
Run with: python scripts/list_databases.py | |
""" | |
import sys | |
import os | |
from pathlib import Path | |
# Add src to path so we can import our modules | |
sys.path.insert(0, str(Path(__file__).parent.parent / "src")) | |
from dotenv import load_dotenv | |
load_dotenv() | |
from notion_client import Client | |
def list_databases(): | |
"""List all databases accessible to the Notion integration.""" | |
print("π Listing all accessible Notion databases...") | |
try: | |
# Initialize Notion client | |
notion_secret = os.getenv("NOTION_SECRET") | |
if not notion_secret: | |
print("β NOTION_SECRET not found in environment") | |
print(" Make sure your .env file contains NOTION_SECRET=your_integration_token") | |
return | |
client = Client(auth=notion_secret) | |
print("β Notion client initialized successfully") | |
# Search for databases | |
print("\nπ Searching for databases...") | |
response = client.search( | |
filter={"property": "object", "value": "database"} | |
) | |
databases = response.get("results", []) | |
if not databases: | |
print("β No databases found!") | |
print("\nπ§ Possible reasons:") | |
print(" 1. Your integration doesn't have access to any databases") | |
print(" 2. You need to share your database with the integration") | |
print(" 3. The integration token might be invalid") | |
return | |
print(f"β Found {len(databases)} database(s):") | |
print("=" * 80) | |
for i, db in enumerate(databases, 1): | |
title = "Untitled" | |
if db.get("title") and len(db["title"]) > 0: | |
title = db["title"][0]["text"]["content"] | |
print(f"\n{i}. π {title}") | |
print(f" π Database ID: {db['id']}") | |
print(f" π URL: {db['url']}") | |
print(f" π Created: {db['created_time']}") | |
print(f" π Last edited: {db['last_edited_time']}") | |
# Show some properties | |
if db.get("properties"): | |
prop_names = list(db["properties"].keys())[:5] # First 5 properties | |
print(f" π·οΈ Properties: {', '.join(prop_names)}") | |
if len(db["properties"]) > 5: | |
print(f" ... and {len(db['properties']) - 5} more") | |
print("\n" + "=" * 80) | |
print("π‘ To use a database with FoodWise:") | |
print(" 1. Copy the Database ID of your food inventory database") | |
print(" 2. Add it to your .env file as: NOTION_INVENTORY_DB_ID=your_database_id") | |
print(" 3. Re-run the manual test: python scripts/manual_test.py") | |
except Exception as e: | |
print(f"β Failed to list databases: {e}") | |
print("\nπ§ Troubleshooting:") | |
print(" 1. Check that NOTION_SECRET is correct in your .env file") | |
print(" 2. Verify your Notion integration is properly configured") | |
print(" 3. Make sure you've shared at least one database with your integration") | |
if __name__ == "__main__": | |
print("π½οΈ FoodWise Database Discovery") | |
print("=" * 40) | |
list_databases() |