se mueve consulta_rapida.py a tools/ y se corrige ruta de ids_prueba.json
El script apuntaba a test/ids_prueba.json, carpeta que ya no existe tras el renombre a tools/. Ahora resuelve rutas desde su ubicación y agrega la raíz del proyecto al sys.path. Se quita de py-modules y se actualiza inventario. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6664264b44
commit
80493eaa94
3 files changed
+14
-8
No files matched your search
@@ -0,0 +1,122 @@
|
||||
"""
|
||||
Script para procesar correos específicos por ID desde tools/ids_prueba.json.
|
||||
No modifica el código existente del pipeline.
|
||||
|
||||
Uso (desde cualquier directorio):
|
||||
python tools/consulta_rapida.py --set <nombre_del_set>
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
import json
|
||||
import argparse
|
||||
import requests
|
||||
from datetime import datetime, timedelta
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Raíz del proyecto en sys.path para importar los módulos del pipeline
|
||||
RAIZ = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
|
||||
sys.path.insert(0, RAIZ)
|
||||
|
||||
import database_utilities
|
||||
import logger_manager
|
||||
from pipeline import _procesar_un_correo, step_authenticate
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# OBTENER CORREO POR ID
|
||||
# ============================================================================
|
||||
def get_email_by_id(user_email: str, token: str, message_id: str) -> dict | None:
|
||||
"""Obtiene un correo desde Microsoft Graph por su ID de mensaje."""
|
||||
url = f"https://graph.microsoft.com/v1.0/users/{user_email}/messages/{message_id}"
|
||||
headers = {"Authorization": f"Bearer {token}"}
|
||||
resp = requests.get(url, headers=headers, verify=False)
|
||||
if resp.status_code != 200:
|
||||
print(f" ❌ Error al obtener mensaje {message_id}: HTTP {resp.status_code}")
|
||||
return None
|
||||
msg = resp.json()
|
||||
utc_str = msg.get("receivedDateTime", "")
|
||||
dt_utc = datetime.fromisoformat(utc_str.replace("Z", "+00:00"))
|
||||
dt_local = dt_utc - timedelta(hours=5)
|
||||
from_obj = msg.get("from", {})
|
||||
sender = from_obj.get("emailAddress", {}).get("address", "Desconocido")
|
||||
return {
|
||||
"id": msg.get("id"),
|
||||
"fecha": dt_local.strftime("%Y-%m-%d %H:%M:%S"),
|
||||
"sender": sender,
|
||||
"subject": msg.get("subject", ""),
|
||||
"has_attach": msg.get("hasAttachments"),
|
||||
}
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# PIPELINE PARA IDS
|
||||
# ============================================================================
|
||||
def run_pipeline_for_ids(ids: list[str]) -> None:
|
||||
# Ruta absoluta: no depende del directorio de trabajo desde el que se lance el proceso
|
||||
load_dotenv(dotenv_path=os.path.join(RAIZ, ".env"))
|
||||
email_addr = os.getenv("EMAIL", "")
|
||||
download_folder = os.getenv("DOWNLOAD_FOLDER", "adjuntos")
|
||||
output_folder = os.getenv("OUTPUT_FOLDER", "resultado")
|
||||
|
||||
if not email_addr:
|
||||
print("❌ EMAIL no configurado en .env")
|
||||
return
|
||||
|
||||
database_utilities.init_db()
|
||||
token = step_authenticate()
|
||||
if not token:
|
||||
return
|
||||
|
||||
for msg_id in ids:
|
||||
email = get_email_by_id(email_addr, token, msg_id.strip())
|
||||
if not email:
|
||||
continue
|
||||
_procesar_un_correo(email, token, email_addr, download_folder, output_folder)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# MAIN
|
||||
# ============================================================================
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Procesar correos específicos por set de IDs desde tools/ids_prueba.json"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--set", "-s", required=True,
|
||||
help="Nombre del set en tools/ids_prueba.json"
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
json_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "ids_prueba.json")
|
||||
if not os.path.exists(json_path):
|
||||
print(f"❌ No se encuentra {json_path}")
|
||||
sys.exit(1)
|
||||
|
||||
with open(json_path) as f:
|
||||
sets = json.load(f)
|
||||
|
||||
if args.set not in sets:
|
||||
print(f"❌ Set '{args.set}' no encontrado. Disponibles: {', '.join(sets.keys())}")
|
||||
sys.exit(1)
|
||||
|
||||
ids = sets[args.set]["ids"]
|
||||
descripcion = sets[args.set].get("descripcion", "")
|
||||
print(f"\n📋 Set: {args.set} — {descripcion}")
|
||||
print(f" IDs a procesar: {len(ids)}")
|
||||
|
||||
logger_manager.setup_logging()
|
||||
|
||||
inicio = datetime.now()
|
||||
print("\n" + "=" * 60)
|
||||
print(f"🚀 INICIO DE EJECUCIÓN: {inicio.strftime('%Y-%m-%d %H:%M:%S')}")
|
||||
print(f"📄 Log guardado en: {logger_manager.LOG_FILE}")
|
||||
print("=" * 60)
|
||||
|
||||
run_pipeline_for_ids(ids)
|
||||
|
||||
fin = datetime.now()
|
||||
duracion = fin - inicio
|
||||
print("\n" + "=" * 60)
|
||||
print(f"🏁 FIN DE EJECUCIÓN: {fin.strftime('%Y-%m-%d %H:%M:%S')}")
|
||||
print(f"⏱️ Duración total: {str(duracion).split('.')[0]}")
|
||||
print("=" * 60)
|
||||
Reference in new issue
Block a user