serverV2.pyโข8.91 kB
"""
Complete Notion Server V2 - Direct API Implementation
Clean implementation using only Notion API - no MCP client complexity
"""
import os
import asyncio
import json
from typing import Any, Dict, List, Optional, Union
from datetime import datetime, timedelta, timezone
from dotenv import load_dotenv
from notion_client import Client
from notion_client.errors import APIResponseError
from .notion_utils import NotionUtils
from .core_operations import CoreOperations
from .analytics_operations import AnalyticsOperations
from .bulk_operations import BulkOperations
from .update_operations import UpdateOperations
# Load environment variables first
load_dotenv()
class ComprehensiveNotionServer:
"""
Complete Notion Server with full API coverage
- Direct Notion API calls only
- Comprehensive workflows and analytics
- Bulk operations and content management
- Production-ready error handling
"""
def __init__(self, notion_token: str):
self.notion_token = notion_token
self.notion = Client(auth=notion_token)
# Initialize operation classes
self.core_ops = CoreOperations(self.notion)
self.analytics_ops = AnalyticsOperations(self.notion)
self.bulk_ops = BulkOperations(self.notion)
self.update_ops = UpdateOperations(self.notion)
async def run_enhanced_conversation(self):
"""Run interactive conversation with comprehensive capabilities"""
print("=== ๐ Complete Notion Server V2 ===")
print("๐ง Direct Notion API Integration + Custom Workflows")
print("๐ Type 'help' for all available capabilities")
print("๐ช Type 'exit' to quit")
print("-" * 60)
while True:
try:
user_input = input("\n๐ค User: ").strip()
except KeyboardInterrupt:
print("\n\n๐ Goodbye!")
break
if user_input.lower() in ['exit', 'quit', 'bye']:
print("\n๐ Goodbye!")
break
if user_input.lower() == 'help':
self.show_comprehensive_help()
continue
if not user_input:
continue
# Route to appropriate handler
await self.route_user_request(user_input)
async def route_user_request(self, user_input: str):
"""Route user request to appropriate handler"""
# READ/GET OPERATIONS
if any(keyword in user_input.lower() for keyword in ['read', 'get', 'show', 'view']):
if 'page' in user_input.lower():
page_identifier = NotionUtils.extract_page_identifier(user_input)
if page_identifier:
await self.core_ops.read_page_content(page_identifier)
else:
await self.core_ops.read_page_interactive()
elif 'database' in user_input.lower():
database_id = input("Enter database ID: ").strip()
if database_id:
await self.core_ops.read_database_content(database_id)
else:
print("What would you like to read?")
print("โข read page [name/id] - Read page content")
print("โข read database [id] - Read database content")
# SEARCH OPERATIONS
elif 'search' in user_input.lower():
search_term = user_input.lower().replace('search', '').strip()
if not search_term:
search_term = input("Enter search term: ").strip()
await self.core_ops.search_content(search_term)
# CREATE OPERATIONS
elif 'create' in user_input.lower():
if 'page' in user_input.lower():
await self.core_ops.create_page_interactive()
elif 'database' in user_input.lower():
await self.core_ops.create_database_interactive()
else:
print("What would you like to create?")
print("โข create page - Create a new page")
print("โข create database - Create a new database")
# UPDATE OPERATIONS
elif 'update' in user_input.lower():
await self.update_ops.update_content_interactive()
# LIST OPERATIONS
elif 'list' in user_input.lower():
await self.core_ops.list_content_interactive()
# ANALYTICS WORKFLOWS
elif any(keyword in user_input.lower() for keyword in ['analyze', 'analytics', 'metrics', 'stats']):
await self.analytics_ops.handle_analytics_requests(user_input)
# BULK OPERATIONS
elif any(keyword in user_input.lower() for keyword in ['bulk', 'multiple', 'batch']):
await self.bulk_ops.handle_bulk_operations(user_input)
else:
print("๐ก I can help you with:")
print("โข Search: 'search [term]'")
print("โข Read: 'read page [name/id]'")
print("โข Create: 'create page'")
print("โข Update: 'update content'")
print("โข List: 'list pages'")
print("โข Analytics: 'analyze workspace'")
print("โข Bulk operations: 'bulk pages'")
def show_comprehensive_help(self):
"""Show comprehensive help information"""
print("\n" + "="*60)
print("๐ง COMPLETE NOTION SERVER V2 CAPABILITIES")
print("="*60)
print("\n๐ CORE OPERATIONS:")
print(" โข search [term] - Search pages and databases")
print(" โข read page [name/id] - Read page content")
print(" โข create page - Create new pages")
print(" โข update content - Add content to existing pages")
print(" โข list pages - List all pages")
print(" โข list databases - List all databases")
print("\n๐ ANALYTICS & METRICS:")
print(" โข analyze workspace - Complete workspace analytics")
print(" โข analyze content - Content structure analysis")
print(" โข analyze activity - Recent activity patterns")
print(" โข analyze database - Database structure analysis")
print("\n๐ BULK OPERATIONS:")
print(" โข bulk pages - Bulk page operations")
print(" โข bulk database - Bulk database operations")
print("\n๐ก EXAMPLES:")
print(" โข 'search jaat'")
print(" โข 'read page jaat'")
print(" โข 'create page'")
print(" โข 'update content'")
print(" โข 'analyze workspace'")
print(" โข 'bulk pages'")
print(" โข 'list pages'")
print("\n๐ CONNECTION:")
print(" โข Direct Notion API: โ
Always available")
print(" โข Clean implementation: โ
No MCP complexity")
print("\n" + "="*60)
# Main execution
async def main():
"""Main entry point for the comprehensive Notion server"""
try:
# Get token
notion_token = os.getenv("NOTION_API_KEY") or os.getenv("NOTION_TOKEN")
print(f"๐ Debug: Token found: {'โ
Yes' if notion_token else 'โ No'}")
if not notion_token:
raise ValueError("NOTION_API_KEY or NOTION_TOKEN environment variable required")
# Test Notion API connection first
print("๐ Testing Notion API connection...")
try:
test_client = Client(auth=notion_token)
user_info = test_client.users.me()
print(f"โ
Notion API connection successful! User: {user_info.get('name', 'N/A')}")
except Exception as api_error:
print(f"โ Notion API connection failed: {api_error}")
print("Please check your NOTION_TOKEN is valid")
return 1
# Create server
print("๐ Creating Notion server...")
server = ComprehensiveNotionServer(notion_token)
# Run interactive conversation
print("โถ๏ธ Starting interactive conversation...")
await server.run_enhanced_conversation()
except KeyboardInterrupt:
print("\n\n๐ Goodbye! (Interrupted)")
return 0
except Exception as e:
print(f"โ Error: {str(e)}")
print(f"๐ Error type: {type(e).__name__}")
import traceback
print(f"๐ Full traceback:")
traceback.print_exc()
return 1
return 0
if __name__ == "__main__":
try:
exit_code = asyncio.run(main())
exit(exit_code)
except KeyboardInterrupt:
print("\n\n๐ Goodbye!")
exit(0)
except Exception as e:
print(f"โ Fatal error: {str(e)}")
import traceback
traceback.print_exc()
exit(1)