import os import shutil from datetime import datetime, timedelta from pathlib import Path from .config import CLEAN_OLD_SESSIONS_DAYS, ESTADO_FILE, LOG_FILE def log_text(line): ts = datetime.now().strftime("%Y-%m-%d %H:%M:%S") with open(LOG_FILE, "a", encoding="utf-8") as file: file.write(f"{ts} | {line}\n") def asegurar_directorio(path): os.makedirs(path, exist_ok=True) def save_session_state(active): try: with open(ESTADO_FILE, "w", encoding="utf-8") as file: file.write("SESION_ACTIVA" if active else "SESION_INACTIVA") except Exception as exc: print(f"No se pudo guardar estado de sesion: {exc}") def cleanup_old_chrome_data(dirpath="./perfil_whatsapp", days=CLEAN_OLD_SESSIONS_DAYS): if days <= 0: return try: base = Path(dirpath) if not base.exists(): return cutoff = datetime.now() - timedelta(days=days) for child in base.iterdir(): try: mtime = datetime.fromtimestamp(child.stat().st_mtime) if mtime < cutoff and child.is_dir(): shutil.rmtree(child) print(f"Eliminada sesion antigua: {child}") except Exception: continue except Exception as exc: print(f"Error limpiando sesiones antiguas: {exc}")