Funcionando mejor
This commit is contained in:
parent
9be8f227cb
commit
b9c3024e04
|
@ -1,5 +1,5 @@
|
||||||
{
|
{
|
||||||
"input_dir": "D:\\Proyectos\\Scripts\\EmailCrono",
|
"input_dir": "C:\\Trabajo\\VM\\40 - 93040 - HENKEL - NEXT2 Problem\\Reporte\\Emails",
|
||||||
"output_dir": "C:\\Users\\migue\\OneDrive\\Miguel\\Obsidean\\Trabajo\\VM\\04-InLavoro\\HENKEL\\93040 - HENKEL - BowlingGreen\\Description\\HENKEL - ALPLA - AUTEFA - Batch Data",
|
"output_dir": "C:\\Users\\migue\\OneDrive\\Miguel\\Obsidean\\Trabajo\\VM\\04-InLavoro\\HENKEL\\93040 - HENKEL - BowlingGreen\\Description\\HENKEL - ALPLA - AUTEFA - Batch Data",
|
||||||
"cronologia_file": "cronologia.md",
|
"cronologia_file": "cronologia.md",
|
||||||
"attachments_dir": "adjuntos"
|
"attachments_dir": "adjuntos"
|
||||||
|
|
21
main.py
21
main.py
|
@ -4,6 +4,7 @@ from pathlib import Path
|
||||||
from utils.email_parser import procesar_eml
|
from utils.email_parser import procesar_eml
|
||||||
from utils.markdown_handler import cargar_cronologia_existente
|
from utils.markdown_handler import cargar_cronologia_existente
|
||||||
from config.config import Config
|
from config.config import Config
|
||||||
|
import hashlib
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
config = Config()
|
config = Config()
|
||||||
|
@ -31,19 +32,35 @@ def main():
|
||||||
mensajes = []
|
mensajes = []
|
||||||
print(f"Loaded {len(mensajes)} existing messages")
|
print(f"Loaded {len(mensajes)} existing messages")
|
||||||
mensajes_hash = {msg.hash for msg in mensajes}
|
mensajes_hash = {msg.hash for msg in mensajes}
|
||||||
|
|
||||||
|
total_procesados = 0
|
||||||
|
total_nuevos = 0
|
||||||
|
mensajes_duplicados = 0
|
||||||
|
|
||||||
for archivo in eml_files:
|
for archivo in eml_files:
|
||||||
print(f"Processing {archivo}")
|
print(f"\nProcessing {archivo}")
|
||||||
nuevos_mensajes = procesar_eml(archivo, config.get_attachments_dir())
|
nuevos_mensajes = procesar_eml(archivo, config.get_attachments_dir())
|
||||||
|
total_procesados += len(nuevos_mensajes)
|
||||||
|
|
||||||
|
# Verificar duplicados
|
||||||
for msg in nuevos_mensajes:
|
for msg in nuevos_mensajes:
|
||||||
if msg.hash not in mensajes_hash:
|
if msg.hash not in mensajes_hash:
|
||||||
mensajes.append(msg)
|
mensajes.append(msg)
|
||||||
mensajes_hash.add(msg.hash)
|
mensajes_hash.add(msg.hash)
|
||||||
|
total_nuevos += 1
|
||||||
|
else:
|
||||||
|
mensajes_duplicados += 1
|
||||||
|
|
||||||
|
print(f"\nEstadísticas de procesamiento:")
|
||||||
|
print(f"- Total mensajes encontrados: {total_procesados}")
|
||||||
|
print(f"- Mensajes únicos añadidos: {total_nuevos}")
|
||||||
|
print(f"- Mensajes duplicados ignorados: {mensajes_duplicados}")
|
||||||
|
|
||||||
|
# Ordenar todos los mensajes por fecha
|
||||||
mensajes.sort(key=lambda x: x.fecha)
|
mensajes.sort(key=lambda x: x.fecha)
|
||||||
|
|
||||||
output_file = config.get_cronologia_file()
|
output_file = config.get_cronologia_file()
|
||||||
print(f"Writing to {output_file}")
|
print(f"\nWriting {len(mensajes)} messages to {output_file}")
|
||||||
with open(output_file, 'w', encoding='utf-8') as f:
|
with open(output_file, 'w', encoding='utf-8') as f:
|
||||||
for msg in mensajes:
|
for msg in mensajes:
|
||||||
f.write(msg.to_markdown())
|
f.write(msg.to_markdown())
|
||||||
|
|
Binary file not shown.
|
@ -25,17 +25,26 @@ class MensajeEmail:
|
||||||
# Skip metadata lines
|
# Skip metadata lines
|
||||||
if line.strip().startswith(('Da: ', 'Inviato: ', 'A: ', 'From: ', 'Sent: ', 'To: ')) or line.strip().startswith('Oggetto: '):
|
if line.strip().startswith(('Da: ', 'Inviato: ', 'A: ', 'From: ', 'Sent: ', 'To: ')) or line.strip().startswith('Oggetto: '):
|
||||||
continue
|
continue
|
||||||
cleaned_lines.append(line)
|
# Limpiar espacios múltiples dentro de cada línea, pero mantener la línea completa
|
||||||
|
cleaned_line = re.sub(r' +', ' ', line)
|
||||||
|
cleaned_lines.append(cleaned_line)
|
||||||
|
|
||||||
# Unir las líneas
|
# Unir las líneas preservando los saltos de línea
|
||||||
text = '\n'.join(cleaned_lines)
|
text = '\n'.join(cleaned_lines)
|
||||||
|
|
||||||
# Primero limpiamos la combinación específica de CRLF+NBSP+CRLF
|
# Limpiar la combinación específica de CRLF+NBSP+CRLF
|
||||||
text = re.sub(r'\r?\n\xa0\r?\n', '\n', text)
|
text = re.sub(r'\r?\n\xa0\r?\n', '\n', text)
|
||||||
|
|
||||||
|
# Reemplazar CRLF por LF
|
||||||
|
text = text.replace('\r\n', '\n')
|
||||||
|
|
||||||
|
# Reemplazar CR por LF
|
||||||
|
text = text.replace('\r', '\n')
|
||||||
|
|
||||||
# Reemplazar 3 o más saltos de línea por dos
|
# Reemplazar 3 o más saltos de línea por dos
|
||||||
text = re.sub(r'\n{3,}', '\n\n', text)
|
text = re.sub(r'\n{3,}', '\n\n', text)
|
||||||
|
|
||||||
|
# Eliminar espacios al inicio y final del texto completo
|
||||||
return text.strip()
|
return text.strip()
|
||||||
|
|
||||||
def to_markdown(self):
|
def to_markdown(self):
|
||||||
|
@ -80,5 +89,27 @@ class MensajeEmail:
|
||||||
return fecha
|
return fecha
|
||||||
|
|
||||||
def _generar_hash(self):
|
def _generar_hash(self):
|
||||||
texto = f"{self.remitente}{self.fecha.isoformat()}{self.contenido}"
|
"""
|
||||||
return hashlib.md5(texto.encode()).hexdigest()
|
Genera un hash único para el mensaje basado en una combinación de campos
|
||||||
|
que identifican únicamente el mensaje
|
||||||
|
"""
|
||||||
|
# Limpiar y normalizar el contenido para el hash
|
||||||
|
# Para el hash, sí normalizamos completamente los espacios
|
||||||
|
contenido_hash = re.sub(r'\s+', ' ', self.contenido).strip()
|
||||||
|
|
||||||
|
# Normalizar el subject
|
||||||
|
subject_normalizado = re.sub(r'\s+', ' ', self.subject if self.subject else '').strip()
|
||||||
|
|
||||||
|
# Crear una cadena con los elementos clave del mensaje
|
||||||
|
elementos_hash = [
|
||||||
|
self.remitente.strip(),
|
||||||
|
self.fecha.strftime('%Y%m%d%H%M'), # Solo hasta minutos para permitir pequeñas variaciones
|
||||||
|
subject_normalizado,
|
||||||
|
contenido_hash[:500] # Usar solo los primeros 500 caracteres del contenido normalizado
|
||||||
|
]
|
||||||
|
|
||||||
|
# Unir todos los elementos con un separador único
|
||||||
|
texto_hash = '|'.join(elementos_hash)
|
||||||
|
|
||||||
|
# Generar el hash
|
||||||
|
return hashlib.md5(texto_hash.encode()).hexdigest()
|
Binary file not shown.
|
@ -9,126 +9,270 @@ from bs4 import BeautifulSoup
|
||||||
from email.utils import parsedate_to_datetime
|
from email.utils import parsedate_to_datetime
|
||||||
from models.mensaje_email import MensajeEmail
|
from models.mensaje_email import MensajeEmail
|
||||||
from utils.attachment_handler import guardar_adjunto
|
from utils.attachment_handler import guardar_adjunto
|
||||||
|
import tempfile
|
||||||
|
import os
|
||||||
|
|
||||||
|
def _get_payload_safely(parte):
|
||||||
|
"""
|
||||||
|
Obtiene el payload de una parte del email de forma segura
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
if parte.is_multipart():
|
||||||
|
return None
|
||||||
|
payload = parte.get_payload(decode=True)
|
||||||
|
if payload is None:
|
||||||
|
return None
|
||||||
|
charset = parte.get_content_charset() or 'utf-8'
|
||||||
|
return payload.decode(charset, errors='ignore')
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error getting payload: {str(e)}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
def _extract_subject_from_text(text):
|
||||||
|
"""
|
||||||
|
Extrae el asunto de un texto dados diferentes formatos de cabecera
|
||||||
|
"""
|
||||||
|
subject_headers = {
|
||||||
|
'Oggetto: ': 9, # Italian
|
||||||
|
'Subject: ': 9, # English
|
||||||
|
'Asunto: ': 8, # Spanish
|
||||||
|
'Sujet: ': 7, # French
|
||||||
|
'Betreff: ': 9 # German
|
||||||
|
}
|
||||||
|
|
||||||
|
for line in text.split('\n'):
|
||||||
|
line = line.strip()
|
||||||
|
for header, offset in subject_headers.items():
|
||||||
|
if line.startswith(header):
|
||||||
|
return line[offset:].strip()
|
||||||
|
return None
|
||||||
|
|
||||||
|
def _should_skip_line(line):
|
||||||
|
"""
|
||||||
|
Determina si una línea debe ser omitida por ser una cabecera de email
|
||||||
|
"""
|
||||||
|
headers_to_skip = [
|
||||||
|
'Da: ', 'Inviato: ', 'A: ', # Italian
|
||||||
|
'From: ', 'Sent: ', 'To: ', # English
|
||||||
|
'De: ', 'Enviado: ', 'Para: ', # Spanish
|
||||||
|
'Von: ', 'Gesendet: ', 'An: ', # German
|
||||||
|
'De : ', 'Envoyé : ', 'À : ' # French
|
||||||
|
]
|
||||||
|
return any(line.strip().startswith(header) for header in headers_to_skip)
|
||||||
|
|
||||||
def _html_a_markdown(html):
|
def _html_a_markdown(html):
|
||||||
# Primero limpiamos los caracteres especiales en el HTML
|
"""
|
||||||
html = html.replace('\xa0', ' ') # NBSP a espacio normal
|
Convierte contenido HTML a texto markdown, extrayendo el asunto si está presente
|
||||||
html = html.replace('\r\n', '\n') # CRLF a LF
|
"""
|
||||||
html = html.replace('\r', '\n') # CR a LF
|
if html is None:
|
||||||
|
return (None, "")
|
||||||
soup = BeautifulSoup(html, 'html.parser')
|
|
||||||
|
|
||||||
# Convert tables, keeping all newlines
|
|
||||||
for table in soup.find_all('table'):
|
|
||||||
rows = table.find_all('tr')
|
|
||||||
|
|
||||||
if rows:
|
try:
|
||||||
markdown_table = []
|
# Limpieza básica
|
||||||
# Get maximum width for each column
|
html = html.replace('\xa0', ' ') # NBSP a espacio normal
|
||||||
max_widths = []
|
html = html.replace('\r\n', '\n') # CRLF a LF
|
||||||
for row in rows:
|
html = html.replace('\r', '\n') # CR a LF
|
||||||
cells = row.find_all(['th', 'td'])
|
|
||||||
while len(max_widths) < len(cells):
|
|
||||||
max_widths.append(0)
|
|
||||||
for i, cell in enumerate(cells):
|
|
||||||
max_widths[i] = max(max_widths[i], len(cell.get_text().strip()))
|
|
||||||
|
|
||||||
# Build table rows
|
|
||||||
header_row = rows[0].find_all(['th', 'td'])
|
|
||||||
header = '| ' + ' | '.join(cell.get_text().strip().ljust(max_widths[i])
|
|
||||||
for i, cell in enumerate(header_row)) + ' |'
|
|
||||||
separator = '|' + '|'.join('-' * (width + 2) for width in max_widths) + '|'
|
|
||||||
|
|
||||||
markdown_table.append(header)
|
|
||||||
markdown_table.append(separator)
|
|
||||||
|
|
||||||
for row in rows[1:]:
|
|
||||||
cells = row.find_all(['td', 'th'])
|
|
||||||
row_text = '| ' + ' | '.join(cell.get_text().strip().ljust(max_widths[i])
|
|
||||||
for i, cell in enumerate(cells)) + ' |'
|
|
||||||
markdown_table.append(row_text)
|
|
||||||
|
|
||||||
# Join with newlines and replace
|
|
||||||
new_text = '\n' + '\n'.join(markdown_table)
|
|
||||||
table.replace_with(soup.new_string(new_text))
|
|
||||||
|
|
||||||
# Handle basic HTML elements
|
|
||||||
for br in soup.find_all('br'):
|
|
||||||
br.replace_with('\n')
|
|
||||||
|
|
||||||
# Get text content
|
|
||||||
text = soup.get_text()
|
|
||||||
|
|
||||||
# Only extract subject and remove basic email headers
|
|
||||||
lines = text.split('\n')
|
|
||||||
cleaned_lines = []
|
|
||||||
subject = None
|
|
||||||
|
|
||||||
for line in lines:
|
|
||||||
# Extract subject if present
|
|
||||||
if line.startswith('Oggetto: '):
|
|
||||||
subject = line[9:].strip()
|
|
||||||
continue
|
|
||||||
|
|
||||||
# Skip only the most basic email headers
|
soup = BeautifulSoup(html, 'html.parser')
|
||||||
if line.startswith(('Da: ', 'Inviato: ', 'A: ', 'From: ', 'Sent: ', 'To: ')):
|
|
||||||
continue
|
# Procesar tablas
|
||||||
|
for table in soup.find_all('table'):
|
||||||
|
try:
|
||||||
|
rows = table.find_all('tr')
|
||||||
|
if not rows:
|
||||||
|
continue
|
||||||
|
|
||||||
|
markdown_table = []
|
||||||
|
max_widths = []
|
||||||
|
|
||||||
|
# Calcular anchos máximos
|
||||||
|
for row in rows:
|
||||||
|
cells = row.find_all(['th', 'td'])
|
||||||
|
while len(max_widths) < len(cells):
|
||||||
|
max_widths.append(0)
|
||||||
|
for i, cell in enumerate(cells):
|
||||||
|
cell_text = cell.get_text().strip()
|
||||||
|
max_widths[i] = max(max_widths[i], len(cell_text))
|
||||||
|
|
||||||
|
# Construir tabla markdown
|
||||||
|
if max_widths: # Solo si tenemos celdas válidas
|
||||||
|
header_row = rows[0].find_all(['th', 'td'])
|
||||||
|
header = '| ' + ' | '.join(cell.get_text().strip().ljust(max_widths[i])
|
||||||
|
for i, cell in enumerate(header_row)) + ' |'
|
||||||
|
separator = '|' + '|'.join('-' * (width + 2) for width in max_widths) + '|'
|
||||||
|
|
||||||
|
markdown_table.append(header)
|
||||||
|
markdown_table.append(separator)
|
||||||
|
|
||||||
|
for row in rows[1:]:
|
||||||
|
cells = row.find_all(['td', 'th'])
|
||||||
|
row_text = '| ' + ' | '.join(cell.get_text().strip().ljust(max_widths[i])
|
||||||
|
for i, cell in enumerate(cells)) + ' |'
|
||||||
|
markdown_table.append(row_text)
|
||||||
|
|
||||||
|
table.replace_with(soup.new_string('\n' + '\n'.join(markdown_table)))
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error procesando tabla: {str(e)}")
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Procesar saltos de línea
|
||||||
|
for br in soup.find_all('br'):
|
||||||
|
br.replace_with('\n')
|
||||||
|
|
||||||
|
# Obtener texto limpio
|
||||||
|
text = soup.get_text()
|
||||||
|
|
||||||
|
# Procesar líneas
|
||||||
|
cleaned_lines = []
|
||||||
|
subject = None
|
||||||
|
|
||||||
|
for line in text.split('\n'):
|
||||||
|
if not subject:
|
||||||
|
subject = _extract_subject_from_text(line)
|
||||||
|
|
||||||
# Keep the line as is, with all its spacing
|
if not _should_skip_line(line):
|
||||||
cleaned_lines.append(line)
|
cleaned_lines.append(line)
|
||||||
|
|
||||||
# Join lines preserving all newlines
|
final_text = '\n'.join(cleaned_lines).strip()
|
||||||
text = '\n'.join(cleaned_lines)
|
return (subject, final_text)
|
||||||
|
|
||||||
return subject, text
|
except Exception as e:
|
||||||
|
print(f"Error en html_a_markdown: {str(e)}")
|
||||||
|
return (None, html if html else "")
|
||||||
|
|
||||||
|
def _procesar_email_adjunto(parte, dir_adjuntos):
|
||||||
|
"""
|
||||||
|
Procesa un email que viene como adjunto dentro de otro email.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
mensajes = []
|
||||||
|
if parte.is_multipart():
|
||||||
|
# Si es multipart, procesar cada subparte
|
||||||
|
for subparte in parte.walk():
|
||||||
|
if subparte.get_content_type() == "message/rfc822":
|
||||||
|
# Si es un mensaje RFC822, obtener el payload como lista
|
||||||
|
payload = subparte.get_payload()
|
||||||
|
if isinstance(payload, list):
|
||||||
|
for msg in payload:
|
||||||
|
mensajes.extend(procesar_eml_interno(msg, dir_adjuntos))
|
||||||
|
elif isinstance(payload, email.message.Message):
|
||||||
|
mensajes.extend(procesar_eml_interno(payload, dir_adjuntos))
|
||||||
|
else:
|
||||||
|
# Si no es multipart, intentar procesar como mensaje único
|
||||||
|
payload = parte.get_payload()
|
||||||
|
if isinstance(payload, list):
|
||||||
|
for msg in payload:
|
||||||
|
mensajes.extend(procesar_eml_interno(msg, dir_adjuntos))
|
||||||
|
elif isinstance(payload, email.message.Message):
|
||||||
|
mensajes.extend(procesar_eml_interno(payload, dir_adjuntos))
|
||||||
|
|
||||||
|
return mensajes
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error procesando email adjunto: {str(e)}")
|
||||||
|
return []
|
||||||
|
|
||||||
def procesar_eml(ruta_archivo, dir_adjuntos):
|
def procesar_eml(ruta_archivo, dir_adjuntos):
|
||||||
with open(ruta_archivo, 'rb') as eml:
|
"""
|
||||||
mensaje = BytesParser(policy=policy.default).parse(eml)
|
Punto de entrada principal para procesar archivos .eml
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
with open(ruta_archivo, 'rb') as eml:
|
||||||
|
mensaje = BytesParser(policy=policy.default).parse(eml)
|
||||||
|
return procesar_eml_interno(mensaje, dir_adjuntos)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error al abrir el archivo {ruta_archivo}: {str(e)}")
|
||||||
|
return []
|
||||||
|
|
||||||
remitente = mensaje.get('from', '')
|
def procesar_eml_interno(mensaje, dir_adjuntos):
|
||||||
fecha_str = mensaje.get('date', '')
|
"""
|
||||||
fecha = _parsear_fecha(fecha_str)
|
Procesa un mensaje de email, ya sea desde archivo o adjunto
|
||||||
|
"""
|
||||||
|
mensajes = []
|
||||||
|
|
||||||
contenido = ""
|
try:
|
||||||
subject = None
|
remitente = mensaje.get('from', '')
|
||||||
adjuntos = []
|
fecha_str = mensaje.get('date', '')
|
||||||
tiene_html = False
|
fecha = _parsear_fecha(fecha_str)
|
||||||
|
|
||||||
# Primera pasada: verificar si hay contenido HTML
|
# Get subject from email headers first
|
||||||
if mensaje.is_multipart():
|
subject = mensaje.get('subject', '')
|
||||||
for parte in mensaje.walk():
|
if subject:
|
||||||
if parte.get_content_type() == "text/html":
|
# Try to decode if it's encoded
|
||||||
tiene_html = True
|
subject = str(email.header.make_header(email.header.decode_header(subject)))
|
||||||
break
|
|
||||||
else:
|
contenido = ""
|
||||||
tiene_html = mensaje.get_content_type() == "text/html"
|
adjuntos = []
|
||||||
|
tiene_html = False
|
||||||
# Segunda pasada: procesar el contenido
|
|
||||||
if mensaje.is_multipart():
|
# First pass: check for HTML content
|
||||||
for parte in mensaje.walk():
|
if mensaje.is_multipart():
|
||||||
if parte.get_content_type() == "text/html":
|
for parte in mensaje.walk():
|
||||||
html_content = parte.get_payload(decode=True).decode(parte.get_content_charset() or 'utf-8', errors='ignore')
|
if parte.get_content_type() == "text/html":
|
||||||
part_subject, text = _html_a_markdown(html_content)
|
tiene_html = True
|
||||||
if part_subject and not subject:
|
break
|
||||||
subject = part_subject
|
|
||||||
contenido = text # Reemplazar en lugar de concatenar
|
|
||||||
elif parte.get_content_type() == "text/plain" and not tiene_html:
|
|
||||||
# Solo usar texto plano si no hay HTML
|
|
||||||
text = parte.get_payload(decode=True).decode(parte.get_content_charset() or 'utf-8', errors='ignore')
|
|
||||||
contenido = text
|
|
||||||
elif parte.get_content_disposition() == 'attachment':
|
|
||||||
ruta_adjunto = guardar_adjunto(parte, dir_adjuntos)
|
|
||||||
if ruta_adjunto:
|
|
||||||
adjuntos.append(Path(ruta_adjunto).name)
|
|
||||||
else:
|
|
||||||
if mensaje.get_content_type() == "text/html":
|
|
||||||
html_content = mensaje.get_payload(decode=True).decode(mensaje.get_content_charset() or 'utf-8', errors='ignore')
|
|
||||||
subject, contenido = _html_a_markdown(html_content)
|
|
||||||
else:
|
else:
|
||||||
contenido = mensaje.get_payload(decode=True).decode(mensaje.get_content_charset() or 'utf-8', errors='ignore')
|
tiene_html = mensaje.get_content_type() == "text/html"
|
||||||
|
|
||||||
|
# Second pass: process content and attachments
|
||||||
|
if mensaje.is_multipart():
|
||||||
|
for parte in mensaje.walk():
|
||||||
|
content_type = parte.get_content_type()
|
||||||
|
|
||||||
|
try:
|
||||||
|
if content_type == "text/html":
|
||||||
|
html_content = _get_payload_safely(parte)
|
||||||
|
if html_content:
|
||||||
|
part_subject, text = _html_a_markdown(html_content)
|
||||||
|
if not subject and part_subject:
|
||||||
|
subject = part_subject
|
||||||
|
if text:
|
||||||
|
contenido = text
|
||||||
|
elif content_type == "text/plain" and not tiene_html:
|
||||||
|
text = _get_payload_safely(parte)
|
||||||
|
if text:
|
||||||
|
contenido = text
|
||||||
|
elif content_type == "message/rfc822":
|
||||||
|
# Procesar email adjunto
|
||||||
|
mensajes_adjuntos = _procesar_email_adjunto(parte, dir_adjuntos)
|
||||||
|
mensajes.extend(mensajes_adjuntos)
|
||||||
|
elif parte.get_content_disposition() == 'attachment':
|
||||||
|
nombre = parte.get_filename()
|
||||||
|
if nombre and nombre.lower().endswith('.eml'):
|
||||||
|
# Si es un archivo .eml adjunto
|
||||||
|
mensajes_adjuntos = _procesar_email_adjunto(parte, dir_adjuntos)
|
||||||
|
mensajes.extend(mensajes_adjuntos)
|
||||||
|
else:
|
||||||
|
# Otros tipos de adjuntos
|
||||||
|
ruta_adjunto = guardar_adjunto(parte, dir_adjuntos)
|
||||||
|
if ruta_adjunto:
|
||||||
|
adjuntos.append(Path(ruta_adjunto).name)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error procesando parte del mensaje: {str(e)}")
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
if mensaje.get_content_type() == "text/html":
|
||||||
|
html_content = _get_payload_safely(mensaje)
|
||||||
|
if html_content:
|
||||||
|
part_subject, contenido = _html_a_markdown(html_content)
|
||||||
|
if not subject and part_subject:
|
||||||
|
subject = part_subject
|
||||||
|
else:
|
||||||
|
contenido = _get_payload_safely(mensaje) or ""
|
||||||
|
|
||||||
|
# Solo agregar el mensaje si tiene contenido útil
|
||||||
|
if contenido or subject or adjuntos:
|
||||||
|
mensajes.append(MensajeEmail(
|
||||||
|
remitente=remitente,
|
||||||
|
fecha=fecha,
|
||||||
|
contenido=contenido,
|
||||||
|
subject=subject,
|
||||||
|
adjuntos=adjuntos
|
||||||
|
))
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error procesando mensaje: {str(e)}")
|
||||||
|
|
||||||
return [MensajeEmail(remitente=remitente, fecha=fecha, contenido=contenido, subject=subject, adjuntos=adjuntos)]
|
return mensajes
|
||||||
|
|
||||||
def _parsear_fecha(fecha_str):
|
def _parsear_fecha(fecha_str):
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in New Issue