can filter mail by subject

This commit is contained in:
2022-08-06 20:22:24 +02:00
parent d707bd56e0
commit fc331aa39b
5 changed files with 124 additions and 32 deletions
+40 -24
View File
@@ -16,6 +16,7 @@ from src.workers.link_validator import LinkValidator
AOL_IMAP_SERVER = "imap.aol.com"
VALIDATION_URL_SUBJECT = 'Validation de votre demande de rendez-vous'
VALIDATION_URL_REGEX = """https:\/\/rendezvousparis.hermes.com\/client\/register\/[A-Z0-9]+\/validate.code=[A-Z0-9]+"""
HERMES_EMAIL = "no-reply@hermes.com"
class MailReader():
@@ -23,19 +24,41 @@ class MailReader():
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, email_number=0) -> list:
# create an IMAP4 class with SSL
mail_list = []
imap = imaplib.IMAP4_SSL(AOL_IMAP_SERVER)
# authenticate
imap.login(self.login, self.password)
status, messages = imap.select("INBOX")
mail_list =[]
self.show_folders(imap)
# total number of emails
messages = int(messages[0])
for i in range(messages, 0, -1):
# get mails from inbox
# (\Archive \HasNoChildren) = "Archive"
# (\Junk \HasNoChildren) = "Bulk"
# (\Drafts \HasNoChildren) = "Draft"
# (\HasNoChildren) = "Inbox"
# (\Sent \HasNoChildren) = "Sent"
# (\Trash \HasNoChildren) = "Trash"
mail_list.extend(self._get_messages_from_folder(imap))
# mail_list.extend(self._get_messages_from_folder(imap, folder="Bulk"))
# close the connection and logout
imap.close()
imap.logout()
return mail_list
def _get_messages_from_folder(self, imap, folder="INBOX") -> list:
imap.select(folder)
mail_messages = []
typ, data = imap.search(None, '(SUBJECT "{}")'.format(VALIDATION_URL_SUBJECT))
for i in data[0].split():
# fetch the email message by ID
res, msg = imap.fetch(str(i), "(RFC822)")
# res, msg = imap.fetch(str(i))
res, msg = imap.fetch(i.decode("utf-8"), "(RFC822)")
body = ''
for response in msg:
if isinstance(response, tuple):
@@ -72,17 +95,8 @@ class MailReader():
print(body)
if VALIDATION_URL_SUBJECT in subject:
mail = MailPojo(subject=subject, body=body, from_address=from_address)
mail_list.append(mail)
# close the connection and logout
imap.close()
imap.logout()
return mail_list
hermes_email = "no-reply@hermes.com"
# account credentials
username = "appointment2022@aol.com"
password = "gyilpmvyyvlcaviq"
mail_messages.append(mail)
return mail_messages
def clean(text):
@@ -112,12 +126,15 @@ def need_to_valid_url(url: str, successful_items) -> bool:
def read_mails():
mail_address1 = MailAddress(mail="appointment2022@aol.com", password="gyilpmvyyvlcaviq")
mail_address2 = MailAddress(mail="chenpeijun@aol.com", password="ytifuwguknzifqyb")
mail_address3 = MailAddress(mail="ciyuexie@aol.com", password="czezlmmyypokdfce")
mail_address4 = MailAddress(mail="hongjiang176@aol.com", password="ftzpscgzvwneelmn")
mail_list = [mail_address3, mail_address2, mail_address1, mail_address4]
# mail_list = [mail_address3]
# get email address
mail_list = MONGO_STORE_MANAGER.get_destination_emails()
# mail_address1 = MailAddress(mail="appointment2022@aol.com", password="gyilpmvyyvlcaviq")
# mail_address2 = MailAddress(mail="chenpeijun@aol.com", password="ytifuwguknzifqyb")
# mail_address2 = MailAddress(mail="sdfgfhgf1986@aol.com", password="fjwcgvhxxlywqfwm")
# mail_address3 = MailAddress(mail="ciyuexie@aol.com", password="czezlmmyypokdfce")
# mail_address4 = MailAddress(mail="hongjiang176@aol.com", password="ftzpscgzvwneelmn")
# mail_list = [mail_address3, mail_address2, mail_address1, mail_address4]
# mail_list = [mail_address1]
for mail in mail_list:
mail_reader = MailReader(mail.mail, mail.password)
successful_items = MONGO_STORE_MANAGER.get_all_successful_items_for_day()
@@ -130,7 +147,6 @@ def read_mails():
if need_to_valid_url(url, successful_items):
url_validator = LinkValidator(url)
print("need to validate url: " + url)
# .start_page(params.get_proxy(ProxyType.BRIGHT_DATA))
executor.submit(url_validator.start_page, params.get_proxy(ProxyType.BRIGHT_DATA), True)
else:
print("do not need to click url --> {}".format(mail))