81 lines
2.8 KiB
Python
81 lines
2.8 KiB
Python
# models/mensaje_email.py
|
|
import re
|
|
import hashlib
|
|
from datetime import datetime
|
|
from email.utils import parseaddr, parsedate_to_datetime
|
|
|
|
class MensajeEmail:
|
|
def __init__(self, remitente, fecha, contenido, subject=None, adjuntos=None):
|
|
self.remitente = self._estandarizar_remitente(remitente)
|
|
self.fecha = self._estandarizar_fecha(fecha)
|
|
self.subject = subject
|
|
self.contenido = self._limpiar_contenido(contenido)
|
|
self.adjuntos = adjuntos if adjuntos else []
|
|
self.hash = self._generar_hash()
|
|
|
|
def _limpiar_contenido(self, contenido):
|
|
if not contenido:
|
|
return ""
|
|
|
|
# Eliminar líneas de metadatos
|
|
lines = contenido.split('\n')
|
|
cleaned_lines = []
|
|
|
|
for line in lines:
|
|
# Skip metadata lines
|
|
if line.strip().startswith(('Da: ', 'Inviato: ', 'A: ', 'From: ', 'Sent: ', 'To: ')) or line.strip().startswith('Oggetto: '):
|
|
continue
|
|
cleaned_lines.append(line)
|
|
|
|
# Unir las líneas
|
|
text = '\n'.join(cleaned_lines)
|
|
|
|
# Reemplazar 3 o más saltos de línea por dos
|
|
text = re.sub(r'\n{3,}', '\n\n', text)
|
|
|
|
return text.strip()
|
|
|
|
def to_markdown(self):
|
|
fecha_formato = self.fecha.strftime('%Y%m%d%H%M%S')
|
|
md = f"## {fecha_formato}|{self.remitente}\n\n"
|
|
if self.subject:
|
|
md += f"**Asunto**: {self.subject}\n\n"
|
|
md += self.contenido + "\n\n"
|
|
if self.adjuntos:
|
|
md += "### Adjuntos\n"
|
|
for adj in self.adjuntos:
|
|
md += f"- [[{adj}]]\n"
|
|
md += "---\n\n"
|
|
return md
|
|
|
|
def _estandarizar_remitente(self, remitente):
|
|
if 'Da:' in remitente:
|
|
remitente = remitente.split('Da:')[1].split('Inviato:')[0]
|
|
elif 'From:' in remitente:
|
|
remitente = remitente.split('From:')[1].split('Sent:')[0]
|
|
|
|
nombre, email = parseaddr(remitente)
|
|
if not nombre and email:
|
|
nombre = email.split('@')[0]
|
|
elif not nombre and not email:
|
|
nombre_match = re.search(r'([A-Za-z\s]+)\s*<', remitente)
|
|
if nombre_match:
|
|
nombre = nombre_match.group(1)
|
|
else:
|
|
return "Remitente Desconocido"
|
|
|
|
nombre = re.sub(r'[<>:"/\\|?*]', '', nombre.strip())
|
|
nombre = nombre.encode('ascii', 'ignore').decode('ascii')
|
|
return nombre
|
|
|
|
def _estandarizar_fecha(self, fecha):
|
|
if isinstance(fecha, str):
|
|
try:
|
|
return parsedate_to_datetime(fecha)
|
|
except:
|
|
return datetime.now()
|
|
return fecha
|
|
|
|
def _generar_hash(self):
|
|
texto = f"{self.remitente}{self.fecha.isoformat()}{self.contenido}"
|
|
return hashlib.md5(texto.encode()).hexdigest() |