import os import sys import logging import json from pathlib import Path from pypdf import PdfWriter, PdfReader from reportlab.pdfgen import canvas from reportlab.lib.pagesizes import A4 from reportlab.lib import colors from PIL import Image import subprocess # Setup logging logging.basicConfig( level=logging.INFO, format='%(asctime)s [%(levelname)s] %(message)s', handlers=[ logging.FileHandler("/app/logs/sudrf_compiler.log", encoding="utf-8"), logging.StreamHandler(sys.stdout) ] ) log = logging.getLogger("SUD-Compiler") # Add libs path (if mounted) sys.path.append("/app") try: from swarmlib.gdrive import GDriveManager except ImportError as e: log.error(f"Cannot import swarmlib: {e}") GDriveManager = None EXPORT_DIR = Path("/app/exports/sudrf") OUTPUT_DIR = Path("/app/exports/compiled_sudrf") OUTPUT_DIR.mkdir(parents=True, exist_ok=True) BOT_TOKEN = "8725618164:AAH1tGalq-pw1l0t4P5c0sdLkCVJdh7IE7M" CHAT_ID = "197957361" def send_tg(msg): subprocess.run(["curl", "-s", "-X", "POST", f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage", "-d", f"chat_id={CHAT_ID}&text={msg}&parse_mode=markdown"]) def tag_image_to_pdf(image_path, output_pdf_path, label): try: c = canvas.Canvas(output_pdf_path, pagesize=A4) width, height = A4 c.setFillColor(colors.black) c.rect(0, height - 50, width, 50, fill=1) c.setFillColor(colors.white) c.setFont("Helvetica-Bold", 10) c.drawString(20, height - 30, f"SUD: {label}".upper()) img = Image.open(image_path) img_width, img_height = img.size aspect = img_height / float(img_width) display_width = width - 40 display_height = display_width * aspect if display_height > (height - 70): display_height = height - 70 display_width = display_height / aspect c.drawImage(image_path, 20, height - 60 - display_height, width=display_width, height=display_height) c.showPage() c.save() return True except Exception as e: log.error(f"Failed to tag image {image_path}: {e}") return False def compile_appeals(): send_tg("🧬 **ГАС Правосудие: Запуск PDF-компилятора**\nОбрабатываю выгруженные материалы...") if not EXPORT_DIR.exists(): log.error(f"Export dir not found: {EXPORT_DIR}") send_tg("❌ **Компиляция прервана**: папка с выгрузками не найдена.") return gdrive = None if GDriveManager: try: gdrive = GDriveManager() except Exception as e: log.error(f"Failed to initialize GDriveManager: {e}") folders = [d for d in EXPORT_DIR.iterdir() if d.is_dir() and d.name.startswith("appeal_")] processed = 0 for folder in folders: appeal_id = folder.name.replace("appeal_", "") log.info(f"Compiling appeal: {appeal_id}") writer = PdfWriter() # 1. Convert detail screenshot (evidence.png) to PDF first evidence_img = folder / "evidence.png" evidence_pdf = folder / "evidence.pdf" if evidence_img.exists(): if tag_image_to_pdf(str(evidence_img), str(evidence_pdf), f"APPEAL {appeal_id}"): writer.append(str(evidence_pdf)) # 2. Append all other PDF files inside the folder files = sorted(list(folder.iterdir())) for f in files: if f.suffix.lower() == ".pdf" and f.name != "evidence.pdf": try: writer.append(str(f)) log.info(f"Appended PDF: {f.name}") except Exception as e: log.error(f"Failed to append PDF {f.name}: {e}") elif f.suffix.lower() in [".png", ".jpg", ".jpeg"] and f.name != "evidence.png": # Convert image attachments to PDF and append img_pdf = folder / f"{f.stem}_attachment.pdf" if tag_image_to_pdf(str(f), str(img_pdf), f"ATTACHMENT {f.name}"): try: writer.append(str(img_pdf)) log.info(f"Appended converted image: {f.name}") except Exception as e: log.error(f"Failed to append converted image {f.name}: {e}") output_pdf = OUTPUT_DIR / f"SUD_APPEAL_{appeal_id}.pdf" try: with open(output_pdf, "wb") as out_f: writer.write(out_f) processed += 1 log.info(f"Successfully compiled: {output_pdf.name}") # Upload to GDrive drive_status = "не настроен" if gdrive: try: drive_status = gdrive.upload_or_update(str(output_pdf), output_pdf.name) except Exception as e: drive_status = f"ошибка GDrive: {e}" send_tg(f"📄 **Обращение {appeal_id} собрано в PDF**\n☁️ GDrive: `{drive_status}`") except Exception as e: log.error(f"Failed to write compiled PDF for {appeal_id}: {e}") send_tg(f"❌ **Ошибка сборки обращения {appeal_id}**: {e}") # Compile consolidated master PDF of all appeals if processed > 0: master_writer = PdfWriter() compiled_pdfs = sorted(list(OUTPUT_DIR.glob("SUD_APPEAL_*.pdf"))) log.info(f"Consolidating {len(compiled_pdfs)} appeal PDFs into SUD_CABINET_ALL.pdf...") for pdf_file in compiled_pdfs: if pdf_file.name == "SUD_CABINET_ALL.pdf": continue try: master_writer.append(str(pdf_file)) except Exception as e: log.error(f"Failed to append to master PDF {pdf_file.name}: {e}") master_pdf_path = OUTPUT_DIR / "SUD_CABINET_ALL.pdf" try: with open(master_pdf_path, "wb") as out_f: master_writer.write(out_f) log.info("Successfully compiled consolidated master PDF: SUD_CABINET_ALL.pdf") # Upload consolidated PDF to GDrive drive_status = "не настроен" if gdrive: try: drive_status = gdrive.upload_or_update(str(master_pdf_path), "SUD_CABINET_ALL.pdf") except Exception as e: drive_status = f"ошибка GDrive: {e}" send_tg(f"☁️ **Сводный файл ГАС Правосудие (SUD_CABINET_ALL.pdf) обновлен**\nGDrive: `{drive_status}`") except Exception as e: log.error(f"Failed to create master PDF: {e}") send_tg(f"❌ **Ошибка создания сводного PDF**: {e}") send_tg(f"🏁 **Компиляция судебных дел завершена!**\nСобрано обращений: {processed}") if __name__ == "__main__": compile_appeals()