69 lines
2.3 KiB
Python
69 lines
2.3 KiB
Python
# main.py
|
|
import os
|
|
from pathlib import Path
|
|
from utils.email_parser import procesar_eml
|
|
from utils.markdown_handler import cargar_cronologia_existente
|
|
from config.config import Config
|
|
import hashlib
|
|
|
|
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()}")
|
|
|
|
# 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 = cargar_cronologia_existente(config.get_cronologia_file())
|
|
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
|
|
for msg in nuevos_mensajes:
|
|
if msg.hash not in mensajes_hash:
|
|
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 todos los mensajes por fecha
|
|
mensajes.sort(key=lambda x: x.fecha)
|
|
|
|
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:
|
|
for msg in mensajes:
|
|
f.write(msg.to_markdown())
|
|
|
|
if __name__ == '__main__':
|
|
main() |