mutt/consolidate_to_mega_pdf.py

86 lines
2.9 KiB
Python
Executable file
Raw 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
"""
Consolidates all individual email PDFs into one giant PDF for NotebookLM.
Sorted by date (prefix in filename).
"""
import os
import sys
import fitz # PyMuPDF
from pathlib import Path
import logging
import requests
from datetime import datetime
# === CONFIG ===
BASE_DIR = Path(__file__).parent
SOURCE_DIR = BASE_DIR / "exported_emails" / "all"
OUTPUT_FILE = BASE_DIR / "ALL_EMAILS_CONSOLIDATED.pdf"
BOT_TOKEN = "8725618164:AAH1tGalq-pw1l0t4P5c0sdLkCVJdh7IE7M"
CHAT_ID = "197957361"
PROXY = "socks5h://127.0.0.1:10808"
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
log = logging.getLogger("Consolidator")
def send_tg_alert(count, size_mb):
url = f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage"
text = f"""
🔥 <b>ВАЖНО: СВОДНЫЙ ФАЙЛ ПОЧТЫ ОБНОВЛЕН!</b> 🔥
📑 <b>Файл:</b> <code>ALL_EMAILS_CONSOLIDATED.pdf</code>
📧 <b>Всего писем в архиве:</b> {count}
🐘 <b>Размер файла:</b> {size_mb:.2f} МБ
📅 <b>Обновлено:</b> {datetime.now().strftime('%Y-%m-%d %H:%M')}
🚀 <b>СРОЧНО:</b> Загрузи этот файл в Google Drive и обнови источники в <b>NotebookLM</b>!
<i>Старые данные AI должен "забыть", новые — осознать.</i>
"""
try:
requests.post(url, json={"chat_id": CHAT_ID, "text": text, "parse_mode": "HTML"},
proxies={"https": PROXY}, timeout=20)
log.info("✅ Telegram alert sent.")
except Exception as e:
log.error(f"❌ TG Alert failed: {e}")
def consolidate():
log.info("Starting consolidation...")
if not SOURCE_DIR.exists():
log.error(f"Source directory {SOURCE_DIR} not found!")
return
pdf_files = list(SOURCE_DIR.glob("*.pdf"))
if not pdf_files:
log.warning("No PDF files found to consolidate.")
return
# Sort by filename (which starts with date YYYY-MM-DD or contains it)
# Most files are: domain_YYYY-MM-DD_... or YYYY-MM-DD_...
# We'll just use natural sort of filenames.
pdf_files.sort(key=lambda x: x.name)
log.info(f"Found {len(pdf_files)} PDFs. Merging...")
merger = fitz.open()
for pdf_path in pdf_files:
try:
doc = fitz.open(pdf_path)
merger.insert_pdf(doc)
doc.close()
except Exception as e:
log.warning(f"Failed to add {pdf_path.name}: {e}")
if len(merger) > 0:
merger.save(OUTPUT_FILE, garbage=3, deflate=True)
size_mb = OUTPUT_FILE.stat().st_size / (1024 * 1024)
log.info(f"✅ Consolidated PDF saved: {OUTPUT_FILE} ({size_mb:.2f} MB)")
merger.close()
# Send alert
send_tg_alert(len(pdf_files), size_mb)
else:
log.error("Merger result is empty.")
merger.close()
if __name__ == "__main__":
consolidate()