try to adapt onet.pl emails
This commit is contained in:
@@ -5,9 +5,11 @@ from concurrent.futures import ThreadPoolExecutor
|
||||
from email.header import decode_header
|
||||
from email.message import Message
|
||||
|
||||
from imapclient import IMAPClient
|
||||
|
||||
from src.db.mirgration.migration_tools import migre_accepted_appointment
|
||||
from src.db.mongo_manager import MONGO_STORE_MANAGER
|
||||
from src.mail.mail_constants import DOMAIN_HOTMAIL, create_imap
|
||||
from src.mail.mail_constants import DOMAIN_HOTMAIL, create_imap, show_folders
|
||||
from src.notification.AcceptedResultPojo import get_accepted_result_from
|
||||
from src.notification.mailer import Mailer
|
||||
from src.pojo.ResultEnum import ResultEnum
|
||||
@@ -25,31 +27,66 @@ class MailConfirmationReader():
|
||||
self.login = login
|
||||
self.password = password
|
||||
|
||||
@staticmethod
|
||||
def show_folders(imap):
|
||||
for i in imap.list()[1]:
|
||||
l = i.decode().split(' "/" ')
|
||||
print(l[0] + " = " + l[1])
|
||||
|
||||
def read_emails(self, mails_messages: list) -> list:
|
||||
# create an IMAP4 class with SSL
|
||||
imap = create_imap(self.login)
|
||||
isImapClient = isinstance(imap, IMAPClient)
|
||||
# authenticate
|
||||
type, dat = imap.login(self.login, str(self.password))
|
||||
print("type is {} for {}".format(type, self.login))
|
||||
if isImapClient:
|
||||
# authenticate
|
||||
dat = imap.login(self.login, str(self.password))
|
||||
print("type is {} for {}".format(dat, self.login))
|
||||
else:
|
||||
responseType, dat = imap.login(self.login, str(self.password))
|
||||
print("type is {} for {}".format(responseType, self.login))
|
||||
mail_list = []
|
||||
print("read mails from {}".format(self.login))
|
||||
# self.show_folders(imap)
|
||||
mail_list.extend(self._get_messages_from_folder(imap, CONFIRMATION_SUBJECT_FR))
|
||||
mail_list.extend(self._get_messages_from_folder(imap, CONFIRMATION_SUBJECT_EN))
|
||||
# mail_list.extend(self._get_messages_from_folder(imap, subject=CONFIRMATION_SUBJECT_EN, folder="Junk"))
|
||||
# mail_list.extend(self._get_messages_from_folder(imap, subject=CONFIRMATION_SUBJECT_EN, folder="Bulk"))
|
||||
# close the connection and logout
|
||||
imap.close()
|
||||
if isImapClient:
|
||||
mail_list.extend(self._get_messages_from_folder_for_imapclient(imap, CONFIRMATION_SUBJECT_FR))
|
||||
mail_list.extend(self._get_messages_from_folder_for_imapclient(imap, CONFIRMATION_SUBJECT_EN))
|
||||
else:
|
||||
folderList = show_folders(imap)
|
||||
for folder in folderList:
|
||||
mail_list.extend(self._get_messages_from_folder(imap, CONFIRMATION_SUBJECT_FR, folder=folder))
|
||||
mail_list.extend(self._get_messages_from_folder(imap, CONFIRMATION_SUBJECT_EN, folder=folder))
|
||||
if isImapClient:
|
||||
imap.close()
|
||||
imap.logout()
|
||||
mails_messages.extend(mail_list)
|
||||
return mail_list
|
||||
|
||||
def _get_messages_from_folder_for_imapclient(self, imap, subject, folder="INBOX") -> list:
|
||||
mail_messages = []
|
||||
search_terms = 'SINCE "{}"'.format(
|
||||
datetime.datetime.today().strftime(
|
||||
date_format))
|
||||
print("search terms is " + search_terms)
|
||||
imap.select_folder(folder)
|
||||
messages = imap.search(['SINCE', datetime.datetime.today()])
|
||||
print("%d messages from our best friend" % len(messages))
|
||||
for uid, message_data in imap.fetch(messages, 'RFC822').items():
|
||||
try:
|
||||
email_message = email.message_from_bytes(message_data[b'RFC822'])
|
||||
from_address = email_message.get('FROM')
|
||||
subject = email_message.get('subject')
|
||||
# print("{}, {},{}".format(from_address, subject, email_message))
|
||||
body = ""
|
||||
if "no-reply@hermes.com" in from_address:
|
||||
for part in email_message.walk():
|
||||
print(part.get_content_type())
|
||||
if part.get_content_type() == "text/html":
|
||||
body = body + part.get_payload()
|
||||
elif part.get_content_type() == "text/plain":
|
||||
body = body + part.get_payload()
|
||||
if CONFIRMATION_SUBJECT_FR in subject or CONFIRMATION_SUBJECT_EN in subject:
|
||||
mail = MailPojo(subject=subject, body=body, from_address=from_address)
|
||||
mail.isImapClient = True
|
||||
print("body is {}".format(body))
|
||||
mail_messages.append(mail)
|
||||
except Exception as error:
|
||||
print(error)
|
||||
print("error trying to read email_Message for {}".format(self.login))
|
||||
return mail_messages
|
||||
|
||||
def _get_messages_from_folder(self, imap, subject, folder="INBOX") -> list:
|
||||
imap.select(folder)
|
||||
mail_messages = []
|
||||
@@ -109,7 +146,7 @@ def accept_appointment_found(accepted_result_list: list):
|
||||
mailer = Mailer()
|
||||
print(accepted_result_list)
|
||||
for reserve in accepted_result_list:
|
||||
mailer.send_email(get_accepted_result_from(reserve, MONGO_STORE_MANAGER), to_all=True)
|
||||
mailer.send_email(get_accepted_result_from(reserve, MONGO_STORE_MANAGER), to_all=False)
|
||||
MONGO_STORE_MANAGER.update_reserve_result(reserve.id, ResultEnum.ACCEPTED, reserve.message)
|
||||
|
||||
if len(accepted_result_list) > 0:
|
||||
|
||||
@@ -29,6 +29,14 @@ SEREVER_GMAIL = "imap.gmail.com"
|
||||
SERVER_IMAGE_ONET = "imap.poczta.onet.pl"
|
||||
|
||||
|
||||
def show_folders(imap) -> list:
|
||||
folders = []
|
||||
for i in imap.list()[1]:
|
||||
l = i.decode().split(' "/" ')
|
||||
folders.append(l[1])
|
||||
return folders
|
||||
|
||||
|
||||
def create_imap(login: str):
|
||||
# create an IMAP4 class with SSL
|
||||
if DOMAIN_163 in login:
|
||||
|
||||
@@ -25,8 +25,10 @@ class MailPojo:
|
||||
body: str
|
||||
subject: str
|
||||
mail_address: str = ""
|
||||
isImapClient = False
|
||||
|
||||
def __init__(self, from_address, body, subject):
|
||||
self.body = body
|
||||
self.subject = subject
|
||||
self.from_address = from_address
|
||||
self.isImapClient = False
|
||||
|
||||
Reference in New Issue
Block a user