101 lines
3.4 KiB
Python
101 lines
3.4 KiB
Python
"""
|
|
Script para dessasemblar los emails y generar un archivo de texto con la cronología de los mensajes.
|
|
"""
|
|
|
|
# main.py
|
|
import os
|
|
from pathlib import Path
|
|
from utils.email_parser import procesar_eml
|
|
from utils.markdown_handler import cargar_cronologia_existente
|
|
from utils.beautify import BeautifyProcessor
|
|
from config.config import Config
|
|
import hashlib
|
|
|
|
def generar_indice(mensajes):
|
|
"""
|
|
Genera una lista de mensajes usando el formato de Obsidian
|
|
"""
|
|
indice = "# Índice de Mensajes\n\n"
|
|
|
|
for mensaje in mensajes:
|
|
indice += mensaje.get_index_entry() + "\n"
|
|
|
|
indice += "\n---\n\n"
|
|
return indice
|
|
|
|
def main():
|
|
config = Config()
|
|
|
|
# Debug prints
|
|
print(f"Input directory: {config.get_input_dir()}")
|
|
print(f"Output directory: {config.get_output_dir()}")
|
|
print(f"Cronologia file: {config.get_cronologia_file()}")
|
|
print(f"Attachments directory: {config.get_attachments_dir()}")
|
|
|
|
# Obtener el directorio donde está el script actual
|
|
script_dir = os.path.dirname(os.path.abspath(__file__))
|
|
# Construir la ruta al archivo de reglas en el subdirectorio config
|
|
beautify_rules = os.path.join(script_dir, "config", "beautify_rules.json")
|
|
beautifier = BeautifyProcessor(beautify_rules)
|
|
print(f"Beautify rules file: {beautify_rules}")
|
|
|
|
# Ensure directories exist
|
|
os.makedirs(config.get_output_dir(), exist_ok=True)
|
|
os.makedirs(config.get_attachments_dir(), exist_ok=True)
|
|
|
|
# Check if input directory exists and has files
|
|
input_path = Path(config.get_input_dir())
|
|
if not input_path.exists():
|
|
print(f"Error: Input directory {input_path} does not exist")
|
|
return
|
|
|
|
eml_files = list(input_path.glob('*.eml'))
|
|
print(f"Found {len(eml_files)} .eml files")
|
|
|
|
mensajes = []
|
|
print(f"Loaded {len(mensajes)} existing messages")
|
|
mensajes_hash = {msg.hash for msg in mensajes}
|
|
|
|
total_procesados = 0
|
|
total_nuevos = 0
|
|
mensajes_duplicados = 0
|
|
|
|
for archivo in eml_files:
|
|
print(f"\nProcessing {archivo}")
|
|
nuevos_mensajes = procesar_eml(archivo, config.get_attachments_dir())
|
|
total_procesados += len(nuevos_mensajes)
|
|
|
|
# Verificar duplicados y aplicar beautify solo a los mensajes nuevos
|
|
for msg in nuevos_mensajes:
|
|
if msg.hash not in mensajes_hash:
|
|
# Aplicar beautify solo si el mensaje es nuevo
|
|
msg.contenido = beautifier.process_text(msg.contenido)
|
|
mensajes.append(msg)
|
|
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 mensajes de más reciente a más antiguo
|
|
mensajes.sort(key=lambda x: x.fecha, reverse=True)
|
|
|
|
# Generar el índice
|
|
indice = generar_indice(mensajes)
|
|
|
|
# Escribir el archivo con el índice y los mensajes
|
|
output_file = config.get_cronologia_file()
|
|
print(f"\nWriting {len(mensajes)} messages to {output_file}")
|
|
with open(output_file, 'w', encoding='utf-8') as f:
|
|
# Primero escribir el índice
|
|
f.write(indice)
|
|
# Luego escribir todos los mensajes
|
|
for msg in mensajes:
|
|
f.write(msg.to_markdown())
|
|
|
|
if __name__ == '__main__':
|
|
main() |