import queue from src.email.functions import connect, check_inbox import time from types import SimpleNamespace from src.worker import Worker from bs4 import BeautifulSoup import re import email.utils from src.communication import Message class Obtenedor(Worker): """ Trabajador que obtiene la lista de correos """ def __init__(self, configs, params): super(Obtenedor, self).__init__(configs, params) self.url = configs.get('email.imap.server') self.port = configs.get('email.imap.port') user = {'user': '', 'password': ''} self.user = SimpleNamespace(**user) self.user.name = configs.get('email.imap.user.name') self.user.password = configs.get('email.imap.user.password') self.ssl = configs.get('email.imap.ssl') self.revisados = [] self.queue = params['queues']['emails'] self.frec = configs.get('supervisor.wait') def is_revisado(self, uid): return uid in self.revisados def add_revisado(self, uid): if self.is_revisado(uid): return self.revisados.append(uid) def build_message(self, email_part): output = [] if email_part.is_multipart(): for part in email_part.get_payload(): output += self.build_message(part) else: html = email_part.get_payload(decode=True) bs = BeautifulSoup(html, 'html.parser') if bs.body: html = bs.body.get_text() else: html = bs.get_text() html = re.sub(' +', ' ', re.sub("\n+", ' ', html)).strip(' ') output.append(html) return output def run(self) -> None: self.logger.log('Starting', type(self)) self.diary.put({'start_turn': 'Obtenedor'}) while not self.stop.is_set(): e = 0 with connect(self.url, self.port, self.user.name, self.user.password, self.ssl) as imap: self.logger.log('Getting emails', type(self)) emails = check_inbox(imap) if emails is None: self.logger.log('No emails found', type(self)) continue for em in emails: if self.is_revisado(em.uid): continue sender = em.message['from'] text = ' '.join([em.message['subject'] + '.'] + self.build_message(em.message)) msg = Message('email', text=text, original=em, sender=sender, datetime=email.utils.parsedate_to_datetime(em.message['Date'])) self.queue.put(msg) self.add_revisado(em.uid) e += 1 self.logger.log('{0} new emails found'.format(e), type(self)) self.diary.put({'message': 'Obtenidos {0} correos nuevos'.format(e)}) time.sleep(self.frec) self.diary.put({'end_turn': 'Obtenedor'}) self.logger.log('Exiting', type(self)) class Validador(Worker): def __init__(self, configs, params): super(Validador, self).__init__(configs=configs, params=params) self.emails = params['queues']['emails'] self.validos = params['queues']['valid'] self.invalidos = params['queues']['invalid'] self.bosses = params['bosses'] self.frec = configs.get('supervisor.wait') def run(self): self.logger.log('Starting', type(self)) self.diary.put({'start_turn': 'Validador'}) while not self.stop.is_set(): try: em = self.emails.get(timeout=self.frec) except queue.Empty: continue if not self.bosses.is_boss(em.sender): self.invalidos.put(em) continue self.validos.put(em) self.logger.log('Exiting', type(self)) self.diary.put({'end_turn': 'Validador'}) class Confirmador(Worker): def __init__(self, configs, params): super(Confirmador, self).__init__(configs=configs, params=params) self.invalidos = params['queues']['invalid'] self.frec = configs.get('supervisor.wait') self.max = configs.get('email.max') self.mensajes = [] def mandar(self): self.logger.log('Sending {0} strange emails'.format(len(self.mensajes)), type(self)) print(self.mensajes) def crear_mega_mensaje(self, msg): if len(self.mensajes) >= self.max: self.mandar() self.mensajes = [] self.mensajes.append(msg) def run(self) -> None: self.logger.log('Starting', type(self)) self.diary.put({'start_turn': 'Confirmador'}) while not self.stop.is_set(): try: em = self.invalidos.get(self.frec) self.crear_mega_mensaje(em) except queue.Empty: continue self.logger.log('Exiting', type(self)) self.diary.put({'end_turn': 'Confirmador'}) class Procesador(Worker): def __init__(self, configs, params): super(Procesador, self).__init__(configs=configs, params=params) def run(self) -> None: self.diary.put({'start_turn': 'Procesador'}) self.diary.put({'end_turn': 'Procesador'}) pass