From 6bf4c4204e3602ab8f89bc41da73400414fba59f Mon Sep 17 00:00:00 2001 From: PAN Lei Date: Thu, 11 May 2023 01:00:53 +0200 Subject: [PATCH] can read sina mails --- src/mail/mail_constants.py | 5 +++-- src/mail/mail_reader.py | 41 ++++++++++++++++++++------------------ 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/mail/mail_constants.py b/src/mail/mail_constants.py index d8c1cf7..ea2a65d 100644 --- a/src/mail/mail_constants.py +++ b/src/mail/mail_constants.py @@ -42,13 +42,14 @@ def show_folders(imap) -> list: def create_imap(login: str): # create an IMAP4 class with SSL if DOMAIN_163 in login: - imap = imaplib.IMAP4_SSL(IMAP_SERVER_163) + imap = IMAPClient(IMAP_SERVER_163, use_uid=True) elif DOMAIN_YAHOO in login: imap = imaplib.IMAP4_SSL(YAHOO_IMAP_SERVER) elif DOMAIN_GMX in login: imap = imaplib.IMAP4_SSL(SERVER_GMX) elif DOMAIN_SINA in login: - imap = imaplib.IMAP4_SSL(IMAP_SERVER_SINA) + # imap = imaplib.IMAP4_SSL(IMAP_SERVER_SINA) + imap = IMAPClient(IMAP_SERVER_SINA, use_uid=True) elif DOMAIN_HOTMAIL in login: imap = imaplib.IMAP4_SSL(HOTMAIL_IMAP_SERVER) elif DOMAIN_RAMBLER_RU in login: diff --git a/src/mail/mail_reader.py b/src/mail/mail_reader.py index c7d3086..138d681 100644 --- a/src/mail/mail_reader.py +++ b/src/mail/mail_reader.py @@ -25,6 +25,7 @@ EMAIL_ADDRESS_REGEX = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b' date_format = "%d-%b-%Y" # DD-Mon-YYYY e.g., 3-Mar-2014 REDIRECTION_MAILS = "appointment2022@aol.com, chenpeijun@aol.com,hongjiang176@aol.com,ciyuexie@aol.com,rutger.62@aol.com,ciccidaniel@aol.com,armasgoodman@aol.com,wknd.gemerine@aol.com,rafmail1981@aol.com,tonovichivanenaki@aol.com,hetland.ari@aol.com,mateusiversen@aol.com,lacerdaraffaello@aol.com,anasida76@aol.com,liamolinari@aol.com,sen70zib@aol.com,mezeiderrick@aol.com,stanisl49avchic@aol.com,damcvrobaneuron@aol.com,suyzanna_fleona@aol.com,dxealing.dissa@aol.com,hogg.karen@aol.com,obocharovamarina@aol.com,buchholzjohann@aol.com,orn.cecchini@aol.com,percivaltorgersen@aol.com,candalgudrun@aol.com,filimonis.76@aol.com,bengann_100@aol.com,axelhanne@aol.com,tiffanylarochelle@aol.com,nicoleta.r@aol.com,eichenbaum.1963@aol.com,kotensasharev@aol.com,samognat32@aol.com,edem_headshot@aol.com,kozmakuzmich1960@aol.com,damonsvensson@aol.com,anders.riva@aol.com,caiminwei123@gmail.com,yulingguo086@gmail.com,yingxiaolu086@gmail.com,lijiazhen0035@gmail.com,fangp370@gmail.com,huangyayu10086@gmail.com,fuziyuan110@gmail.com,xinyingdu886@gmail.com,yasiaforever.1971@aol.com,lukaszfidalgo@aol.com,zaichi29@aol.com,prostotakitak.1974@aol.com,mo90nroe@aol.com,blonde.87@aol.com,dimidrol.1969@aol.com" + def check_email_address(email): # pass the regular expression # and the string into the fullmatch() method @@ -58,10 +59,18 @@ class MailReader(): @staticmethod def show_folders(imap) -> list: folders = [] - for i in imap.list()[1]: - l = i.decode().split(' "/" ') - folders.append(l[1]) - return folders + isImapClient = isinstance(imap, IMAPClient) + if not isImapClient: + for i in imap.list()[1]: + l = i.decode().split(' "/" ') + folders.append(l[1]) + return folders + else: + list = imap.list_folders() + for i in list: + name = i[-1] + folders.append(name) + return folders def read_emails(self, mails_messages: list) -> list: imap = create_imap(self.login) @@ -86,7 +95,10 @@ class MailReader(): mail_list.extend(self._get_messages_from_folder(imap, subject=VALIDATION_URL_SUBJECT_EN, folder=folder)) else: - mail_list.extend(self._get_messages_from_folder_for_imapclient(imap)) + folder_list = self.show_folders(imap) + for folder in folder_list: + print("folder is " + folder) + mail_list.extend(self._get_messages_from_folder_for_imapclient(imap, folder=folder)) if DOMAIN_HOTMAIL in self.login: mail_list.extend( self._get_messages_from_folder_for_imapclient(imap, folder="Junk")) @@ -166,7 +178,7 @@ class MailReader(): subject = email_message.get('subject') # print("{}, {},{}".format(from_address, subject, email_message)) body = "" - if "no-reply@hermes.com" in from_address: + if "no-reply@hermes.com" in from_address or "appointment2022@aol.com": for part in email_message.walk(): print(part.get_content_type()) if part.get_content_type() == "text/html": @@ -219,7 +231,9 @@ def need_to_check_email(mail: str, successful_items) -> bool: if mail == "lakrishstarko@aol.com": print("found") if len(filtered_items) > 0: - validated_items = list(filter(lambda filtered_item: filtered_item.url_validated is not None and filtered_item.url_validated is True, filtered_items)) + validated_items = list(filter( + lambda filtered_item: filtered_item.url_validated is not None and filtered_item.url_validated is True, + filtered_items)) if len(validated_items) > 0: return False else: @@ -227,24 +241,13 @@ def need_to_check_email(mail: str, successful_items) -> bool: else: return False - # for item in filtered_items: - # if mail in item.email: - # print("url_validated for {} is {}".format(mail, item.url_validated)) - # if item.url_validated is not None: - # return not item.url_validated - # else: - # # if url-validated is none, need to check email - # return True - # # if the email has not been booked, we needn't read mails. - # # return True - # return False - def read_mails(): # check time before start checking emails if is_time_between(time(7, 30), time(19, 30)): # get email address mail_list = MONGO_STORE_MANAGER.get_destination_emails() + # mail_address1 = MailAddress(mail="vql8p2@sina.com", password="cf510636770153a8") # mail_address1 = MailAddress(mail="appointment2022@aol.com", password="gyilpmvyyvlcaviq") # mail_address1 = MailAddress(mail="chenpeijun@aol.com", password="ytifuwguknzifqyb") # # mail_address3 = MailAddress(mail="ciyuexie@aol.com", password="czezlmmyypokdfce")