Mejoras a logger y limpieza de email

This commit is contained in:
2020-11-13 00:04:52 -03:00
parent 3eae0194f7
commit 1b57d705bb
6 changed files with 84 additions and 132 deletions

View File

@ -1,3 +1 @@
from .email_interpreter import EmailInterpreter
from .revisor_worker import RevisorEmailWorker
from .workers import Obtenedor, Validador, Confirmador
from .workers import Obtenedor, Validador, Confirmador

View File

@ -1,10 +0,0 @@
from threading import Thread
class EmailInterpreter(Thread):
def __init__(self, configs, params):
super().__init__()
self.logging = params['logging']
def run(self):
self.logging.log('Starting', type(self))

View File

@ -1,90 +0,0 @@
from threading import Thread
import re
import time
import email.utils
from bs4 import BeautifulSoup
from entry.email.inbox import connect, check_inbox
from src.communication.message import Message
class RevisorEmailWorker(Thread):
def __init__(self, configs, params):
super().__init__()
self._url = configs.get('email.server')
self._port = configs.get('email.port')
self._username = configs.get('email.username')
self._password = configs.get('email.password')
self._ssl = configs.get('email.ssl')
self.queue = params['queues']['emails']
self.questions = params['queues']['questions']
self.event = params['events']['stop']
self._wait = configs.get('supervisor.wait')
self._bosses = params['bosses']
self._logger = params['queues']['log']
self.logging = params['logging']
self.revisados = []
def revisado(self, uid):
if not self.check_revisado(uid):
self.revisados.append(uid)
def check_revisado(self, uid):
if uid in self.revisados:
return True
return False
def run(self):
self.logging.log('Starting', type(self))
self._logger.put({'action': 'Inicio jornada trabajador Revisor Email'})
while not self.event.is_set():
self.logging.log('Looping status {0}'.format(not self.event.is_set()), type(self))
self.logging.log('Connecting to Email Server', type(self))
imap = connect(imap_url=self._url, port=self._port, username=self._username, password=self._password,
ssl=self._ssl)
self.logging.log('Getting emails', type(self))
emails = check_inbox(imap)
if emails is not None:
c = 0
p = 0
for em in emails:
if self.check_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']))
if not self._bosses.is_boss(sender):
self.logging.log('Sender {0} is not a boss'.format(sender), type(self))
self.revisado(em.uid)
self.questions.put(msg)
p += 1
continue
self.queue.put(msg)
self.revisado(em.uid)
c += 1
self.logging.log('{0} emails checked'.format(c), type(self))
if c > 0:
self._logger.put({'action': 'Revise {0} nuevos correos'.format(c)})
self.logging.log('{0} emails pending'.format(p), type(self))
if p > 0:
self._logger.put({'action': 'Tengo dudas en {0} correos'.format(p)})
imap.close()
time.sleep(self._wait)
self.logging.log('Exiting', type(self))
return
def build_message(self, email_part):
output = []
if email_part.is_multipart():
for part in email_part.get_payload():
output.append(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

View File

@ -55,7 +55,7 @@ class Obtenedor(Worker):
def run(self) -> None:
self.logger.log('Starting', type(self))
self.diary.put({'action': 'Inicio de turno de Obtenedor'})
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:
@ -75,15 +75,15 @@ class Obtenedor(Worker):
self.add_revisado(em.uid)
e += 1
self.logger.log('{0} new emails found'.format(e), type(self))
self.diary.put({'action': 'Obtenidos{0} correos nuevos'.format(e)})
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))
self.diary.put({'action': 'Terminando el turno de Obtenedor'})
class Validador(Worker):
def __init__(self, configs, params):
super(Validador, self).__init__(configs, params)
super(Validador, self).__init__(configs=configs, params=params)
self.emails = params['queues']['emails']
self.validos = params['queues']['valid']
@ -94,7 +94,7 @@ class Validador(Worker):
def run(self):
self.logger.log('Starting', type(self))
self.diary.put({'action': 'Inicio de turno de Validador'})
self.diary.put({'start_turn': 'Validador'})
while not self.stop.is_set():
try:
em = self.emails.get(timeout=self.frec)
@ -106,7 +106,7 @@ class Validador(Worker):
continue
self.validos.put(em)
self.logger.log('Exiting', type(self))
self.diary.put({'action': 'Terminando el turno de Validador'})
self.diary.put({'end_turn': 'Validador'})
class Confirmador(Worker):
@ -115,15 +115,37 @@ class Confirmador(Worker):
self.invalidos = params['queues']['invalid']
self.frec = configs.get('supervisor.wait')
self.max = configs.get('email.max')
self.mensajes = []
def crear_mega_mensaje(self):
pass
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:
pass
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