domovoy_bot/services/email_auditor/sync_helper.py

76 lines
2.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
Helper script to sync master PDF with NotebookLM.
Uses notebooklm-mcp (cli) internally.
"""
import os
import sys
import subprocess
import logging
from pathlib import Path
from dotenv import load_dotenv
# Path setup
BASE_DIR = Path(__file__).resolve().parent
PROJECT_ROOT = BASE_DIR.parent.parent
load_dotenv(PROJECT_ROOT / ".env")
# Logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s [%(levelname)s] %(message)s',
handlers=[logging.FileHandler(BASE_DIR / "sync.log", encoding="utf-8"), logging.StreamHandler()]
)
log = logging.getLogger("SyncAI")
# Config
MASTER_PDF = BASE_DIR / "exported_emails" / "TOTAL_ARCHIVE_2025-2026.pdf"
NOTEBOOK_ID = "00ed7d92-1ab2-4159-ac72-1d0c4be21377"
SOURCE_NAME = "all_mail_gmail.pdf"
def run_sync():
log.info("Starting AI Synchronization...")
if not MASTER_PDF.exists():
log.error(f"File not found: {MASTER_PDF}")
return False, "Файл архива не найден."
log.info(f"Using Master PDF: {MASTER_PDF} ({MASTER_PDF.stat().st_size / 1024 / 1024:.2f} MB)")
# Check for proxy
env = os.environ.copy()
if os.getenv("USE_PROXY", "").lower() == "true":
proxy = f"socks5h://{os.getenv('PROXY_HOST', '127.0.0.1')}:{os.getenv('PROXY_PORT', '10808')}"
env["HTTPS_PROXY"] = proxy
env["HTTP_PROXY"] = proxy
log.info(f"Using proxy for sync: {proxy}")
try:
# Step 1: Check if source exists and delete it (optional, to avoid duplicates)
log.info("Step 1: Checking for existing source in NotebookLM...")
# (This will be implemented when MCP auth is fixed)
# Step 2: Upload new version
log.info(f"Step 2: Uploading {MASTER_PDF} to notebook {NOTEBOOK_ID}...")
# For now, we simulate the call since auth is broken
# command = ["nlm", "source", "add", str(MASTER_PDF), "--notebook", NOTEBOOK_ID]
# result = subprocess.run(command, env=env, capture_output=True, text=True)
# if result.returncode == 0:
# log.info("Successfully synced with NotebookLM!")
# return True, "Синхронизация с ИИ успешно завершена."
# else:
# log.error(f"Sync failed: {result.stderr}")
# return False, f"Ошибка синхронизации: {result.stderr}"
log.warning("!!! SYNCHRONIZATION SIMULATED (WAITING FOR AUTH FIX) !!!")
return True, "Файл подготовлен. Ожидание восстановления авторизации NotebookLM."
except Exception as e:
log.error(f"Unexpected error: {e}")
return False, str(e)
if __name__ == "__main__":
success, msg = run_sync()
print(msg)