optimization on mail_reader_all_contacts.py
This commit is contained in:
+4
-1
@@ -83,7 +83,8 @@ class MongoDbManager:
|
|||||||
result_list.append(ContactPojo.from_firestore_dict(document))
|
result_list.append(ContactPojo.from_firestore_dict(document))
|
||||||
return result_list
|
return result_list
|
||||||
|
|
||||||
def save_links_to_validate(self, link: str, mail_address: str, model: str, _all_contact_list: list):
|
def save_links_to_validate(self, link: str, mail_address: str, model: str,
|
||||||
|
_all_contact_list: list, _used_ip: str = ""):
|
||||||
collection_to_use = self.db[LINKS_TO_VALIDATE]
|
collection_to_use = self.db[LINKS_TO_VALIDATE]
|
||||||
updated_at = time.strftime("%H:%M:%S", time.localtime())
|
updated_at = time.strftime("%H:%M:%S", time.localtime())
|
||||||
_ip_country = "FR"
|
_ip_country = "FR"
|
||||||
@@ -100,6 +101,7 @@ class MongoDbManager:
|
|||||||
u'serial': serial,
|
u'serial': serial,
|
||||||
u'model': model,
|
u'model': model,
|
||||||
u'ip_country': _ip_country,
|
u'ip_country': _ip_country,
|
||||||
|
u'_used_ip': _used_ip,
|
||||||
"updated_at": updated_at
|
"updated_at": updated_at
|
||||||
},
|
},
|
||||||
upsert=True)
|
upsert=True)
|
||||||
@@ -109,6 +111,7 @@ class MongoDbManager:
|
|||||||
u'serial': serial,
|
u'serial': serial,
|
||||||
u'model': model,
|
u'model': model,
|
||||||
u'ip_country': _ip_country,
|
u'ip_country': _ip_country,
|
||||||
|
u'_used_ip': _used_ip,
|
||||||
"updated_at": updated_at
|
"updated_at": updated_at
|
||||||
},
|
},
|
||||||
upsert=True)
|
upsert=True)
|
||||||
|
|||||||
Executable → Regular
+278
-157
@@ -2,11 +2,10 @@ import datetime
|
|||||||
import email
|
import email
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
from builtins import list
|
|
||||||
from concurrent.futures import ThreadPoolExecutor
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
from email.header import decode_header
|
from email.header import decode_header
|
||||||
from email.message import Message
|
from email.message import Message
|
||||||
from typing import Union
|
from typing import Union, List
|
||||||
|
|
||||||
from imapclient import IMAPClient
|
from imapclient import IMAPClient
|
||||||
from db.mongo_manager import MONGO_STORE_MANAGER
|
from db.mongo_manager import MONGO_STORE_MANAGER
|
||||||
@@ -15,74 +14,87 @@ from mail.mail_constants import DOMAIN_HOTMAIL, create_imap
|
|||||||
from models.ReserveResultPojo import ReserveResultPojo
|
from models.ReserveResultPojo import ReserveResultPojo
|
||||||
from models.mail_pojo import MailPojo, MailAddress
|
from models.mail_pojo import MailPojo, MailAddress
|
||||||
|
|
||||||
VALIDATION_URL_SUBJECT_fr = 'Validation de votre demande de rendez-vous'
|
# 定义常量
|
||||||
|
VALIDATION_URL_SUBJECT_FR = 'Validation de votre demande de rendez-vous'
|
||||||
VALIDATION_URL_SUBJECT_EN = 'Please confirm your appointment request'
|
VALIDATION_URL_SUBJECT_EN = 'Please confirm your appointment request'
|
||||||
VALIDATION_URL_REGEX = """https:\/\/rendezvousparis.hermes.com\/client\/register\/[A-Z0-9]+\/validate.code=[A-Z0-9]+"""
|
VALIDATION_URL_REGEX = r"https:\/\/rendezvousparis.hermes.com\/client\/register\/[A-Z0-9]+\/validate.code=[A-Z0-9]+"
|
||||||
PART_VALIDATION_URL_REGEX = """client\/register\/[A-Z0-9]+\/validate.code=[A-Z0-9]+"""
|
PART_VALIDATION_URL_REGEX = r"client\/register\/[A-Z0-9]+\/validate.code=[A-Z0-9]+"
|
||||||
HERMES_EMAIL = "no-reply@hermes.com"
|
HERMES_EMAIL = "no-reply@hermes.com"
|
||||||
EMAIL_ADDRESS_REGEX = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b'
|
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
|
# 日期格式
|
||||||
|
DATE_FORMAT = "%d-%b-%Y"
|
||||||
|
|
||||||
|
# 邮箱列表(简化为常量)
|
||||||
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"
|
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
|
def is_valid_email(email: str) -> bool:
|
||||||
# and the string into the fullmatch() method
|
"""验证邮箱地址是否有效"""
|
||||||
if (re.fullmatch(EMAIL_ADDRESS_REGEX, email)):
|
return re.fullmatch(EMAIL_ADDRESS_REGEX, email) is not None
|
||||||
print("Valid Email")
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
print("Invalid Email:" + email)
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def find_from_mail(param):
|
def extract_email_from_from_address(content: str) -> str:
|
||||||
|
"""从邮件地址中提取邮箱"""
|
||||||
|
match = re.search(r'[\w.+-]+@[\w-]+\.[\w.-]+', content)
|
||||||
|
return match.group(0) if match else ""
|
||||||
|
|
||||||
|
|
||||||
|
def find_from_mail(param) -> str:
|
||||||
|
"""解析邮件地址"""
|
||||||
from_address, encoded_algo = param[0]
|
from_address, encoded_algo = param[0]
|
||||||
|
|
||||||
|
# 处理字节编码
|
||||||
if isinstance(from_address, bytes):
|
if isinstance(from_address, bytes):
|
||||||
from_address = from_address.decode(encoded_algo)
|
from_address = from_address.decode(encoded_algo)
|
||||||
if not check_email_address(from_address) and len(param) == 2:
|
|
||||||
|
# 如果邮箱地址无效,尝试另一种编码
|
||||||
|
if not is_valid_email(from_address) and len(param) == 2:
|
||||||
from_address, new_encode = param[1]
|
from_address, new_encode = param[1]
|
||||||
if new_encode is None:
|
if new_encode is None:
|
||||||
new_encode = encoded_algo
|
new_encode = encoded_algo
|
||||||
if isinstance(from_address, bytes):
|
if isinstance(from_address, bytes):
|
||||||
from_address = from_address.decode(new_encode)
|
from_address = from_address.decode(new_encode)
|
||||||
return from_address.strip(" ").strip(">").strip("<")
|
|
||||||
|
# 清理邮箱地址
|
||||||
return from_address.strip(" ").strip(">").strip("<")
|
return from_address.strip(" ").strip(">").strip("<")
|
||||||
|
|
||||||
|
|
||||||
def extract_email_from_from_address(content: str):
|
class MailReader:
|
||||||
_match = re.search(r'[\w.+-]+@[\w-]+\.[\w.-]+', content)
|
"""邮件读取器类"""
|
||||||
return _match.group(0)
|
|
||||||
|
|
||||||
|
def __init__(self, login: str, password: str):
|
||||||
class MailReader():
|
|
||||||
def __init__(self, login, password):
|
|
||||||
self.login = login
|
self.login = login
|
||||||
self.password = password
|
self.password = password
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def show_folders(imap) -> list:
|
def show_folders(imap) -> List[str]:
|
||||||
|
"""获取邮箱文件夹列表"""
|
||||||
folders = []
|
folders = []
|
||||||
isImapClient = isinstance(imap, IMAPClient)
|
is_imap_client = isinstance(imap, IMAPClient)
|
||||||
if not isImapClient:
|
|
||||||
|
if not is_imap_client:
|
||||||
|
# 处理非IMAPClient对象
|
||||||
for i in imap.list()[1]:
|
for i in imap.list()[1]:
|
||||||
l = i.decode().split(' "/" ')
|
l = i.decode().split(' "/" ')
|
||||||
folders.append(l[1])
|
folders.append(l[1])
|
||||||
return folders
|
|
||||||
else:
|
else:
|
||||||
list = imap.list_folders()
|
# 处理IMAPClient对象
|
||||||
for i in list:
|
folder_list = imap.list_folders()
|
||||||
|
for i in folder_list:
|
||||||
name = i[-1]
|
name = i[-1]
|
||||||
folders.append(name)
|
folders.append(name)
|
||||||
return folders
|
|
||||||
|
|
||||||
def read_emails(self, mails_messages: list) -> list:
|
return folders
|
||||||
|
|
||||||
|
def read_emails(self, mails_messages: List[MailPojo]) -> List[MailPojo]:
|
||||||
|
"""读取邮件"""
|
||||||
imap = create_imap(self.login)
|
imap = create_imap(self.login)
|
||||||
isImapClient = isinstance(imap, IMAPClient)
|
is_imap_client = isinstance(imap, IMAPClient)
|
||||||
print("isImapClient is " + str(isImapClient))
|
|
||||||
if isImapClient:
|
# 登录邮箱
|
||||||
# authenticate
|
if is_imap_client:
|
||||||
dat = imap.login(self.login, str(self.password))
|
dat = imap.login(self.login, str(self.password))
|
||||||
print("type is {} for {}".format(dat, self.login))
|
print("type is {} for {}".format(dat, self.login))
|
||||||
else:
|
else:
|
||||||
@@ -91,224 +103,333 @@ class MailReader():
|
|||||||
|
|
||||||
mail_list = []
|
mail_list = []
|
||||||
print("read mails from {}".format(self.login))
|
print("read mails from {}".format(self.login))
|
||||||
if not isImapClient:
|
|
||||||
folder_list = self.show_folders(imap)
|
# 获取文件夹列表
|
||||||
for folder in folder_list:
|
folder_list = self.show_folders(imap)
|
||||||
print("folder is {}".format(folder))
|
|
||||||
mail_list.extend(self._get_messages_from_folder(imap, subject=VALIDATION_URL_SUBJECT_fr,
|
# 处理每个文件夹
|
||||||
folder=folder))
|
for folder in folder_list:
|
||||||
mail_list.extend(self._get_messages_from_folder(imap, subject=VALIDATION_URL_SUBJECT_EN,
|
print("folder is {}".format(folder))
|
||||||
folder=folder))
|
|
||||||
else:
|
# 跳过Sent和Drafts文件夹
|
||||||
folder_list = self.show_folders(imap)
|
if folder in ["Sent", "Drafts"]:
|
||||||
for folder in folder_list:
|
continue
|
||||||
print("folder is " + folder)
|
|
||||||
if folder == "Sent" or folder == "Drafts":
|
if is_imap_client:
|
||||||
pass
|
# 使用IMAPClient处理
|
||||||
else:
|
mail_list.extend(self._get_messages_from_folder_for_imapclient(imap, folder))
|
||||||
mail_list.extend(self._get_messages_from_folder_for_imapclient(imap, folder=folder))
|
else:
|
||||||
if not isImapClient:
|
# 使用传统IMAP处理
|
||||||
|
mail_list.extend(self._get_messages_from_folder(imap, subject=VALIDATION_URL_SUBJECT_FR, folder=folder))
|
||||||
|
mail_list.extend(self._get_messages_from_folder(imap, subject=VALIDATION_URL_SUBJECT_EN, folder=folder))
|
||||||
|
|
||||||
|
# 关闭连接
|
||||||
|
if not is_imap_client:
|
||||||
imap.close()
|
imap.close()
|
||||||
imap.logout()
|
imap.logout()
|
||||||
|
|
||||||
|
# 添加邮件到结果列表
|
||||||
mails_messages.extend(mail_list)
|
mails_messages.extend(mail_list)
|
||||||
return mail_list
|
return mail_list
|
||||||
|
|
||||||
def _get_messages_from_folder(self, imap, subject, folder="INBOX") -> list:
|
def _get_messages_from_folder(self, imap, subject: str, folder: str = "INBOX") -> List[MailPojo]:
|
||||||
|
"""从指定文件夹获取邮件(传统IMAP方式)"""
|
||||||
imap.select(folder)
|
imap.select(folder)
|
||||||
mail_messages = []
|
mail_messages = []
|
||||||
typ, data = imap.search(None, '(SUBJECT "{}" SINCE "{}")'.format(subject,
|
|
||||||
datetime.datetime.today().strftime(
|
# 搜索邮件
|
||||||
date_format)))
|
search_query = '(SUBJECT "{}" SINCE "{}")'.format(subject, datetime.datetime.today().strftime(DATE_FORMAT))
|
||||||
|
typ, data = imap.search(None, search_query)
|
||||||
|
|
||||||
for i in data[0].split():
|
for i in data[0].split():
|
||||||
# fetch the email message by ID
|
try:
|
||||||
res, msg = imap.fetch(i.decode("utf-8"), "(RFC822)")
|
# 获取邮件内容
|
||||||
body = ''
|
res, msg = imap.fetch(i.decode("utf-8"), "(RFC822)")
|
||||||
for response in msg:
|
|
||||||
if isinstance(response, tuple):
|
# 解析邮件
|
||||||
# parse a bytes email into a message object
|
for response in msg:
|
||||||
msg = email.message_from_bytes(response[1])
|
if isinstance(response, tuple):
|
||||||
# decode the email subject
|
email_message = email.message_from_bytes(response[1])
|
||||||
subject, subject_encoded = decode_header(msg["Subject"])[0]
|
|
||||||
received_date = msg["Date"]
|
# 解码主题
|
||||||
if isinstance(subject, bytes):
|
subject, subject_encoded = decode_header(email_message["Subject"])[0]
|
||||||
# if it's a bytes, decode to str
|
if isinstance(subject, bytes):
|
||||||
subject = subject.decode(subject_encoded)
|
subject = subject.decode(subject_encoded)
|
||||||
# decode email sender
|
|
||||||
from_address = find_from_mail(decode_header(msg.get("From")))
|
# 解码发件人地址
|
||||||
to_email = find_from_mail(decode_header(msg.get("To")))
|
from_address = find_from_mail(decode_header(email_message.get("From")))
|
||||||
print("Email:", self.login)
|
|
||||||
print("From:", from_address)
|
# 解码收件人地址
|
||||||
print("To:", to_email)
|
to_email = find_from_mail(decode_header(email_message.get("To")))
|
||||||
print("Subject:", subject)
|
|
||||||
# if the email message is multipart
|
print("Email:", self.login)
|
||||||
if msg.is_multipart():
|
print("From:", from_address)
|
||||||
# iterate over email parts
|
print("To:", to_email)
|
||||||
for part in msg.walk():
|
print("Subject:", subject)
|
||||||
try:
|
|
||||||
# get the email body
|
# 获取邮件正文
|
||||||
payloads = part.get_payload()
|
body = self._extract_body(email_message)
|
||||||
if isinstance(payloads, list):
|
|
||||||
for payload in payloads:
|
# 检查是否是预约验证邮件
|
||||||
if isinstance(payload, Message):
|
if VALIDATION_URL_SUBJECT_FR in subject or VALIDATION_URL_SUBJECT_EN in subject:
|
||||||
body = body + payload.get_payload(decode=True).decode("iso-8859-1")
|
mail = MailPojo(
|
||||||
# print(body)
|
subject=subject,
|
||||||
except Exception as Error:
|
body=body,
|
||||||
print(Error)
|
from_address=from_address
|
||||||
else:
|
)
|
||||||
body = msg.get_payload(decode=True).decode()
|
|
||||||
print(body)
|
# 设置收件人地址
|
||||||
if VALIDATION_URL_SUBJECT_fr in subject or VALIDATION_URL_SUBJECT_EN in subject:
|
if to_email is None:
|
||||||
mail = MailPojo(subject=subject, body=body, from_address=from_address)
|
mail.to_address = self.login
|
||||||
if to_email is None:
|
else:
|
||||||
mail.to_address = self.login
|
mail.to_address = to_email
|
||||||
else:
|
|
||||||
mail.to_address = to_email
|
mail.mail_address = self.login
|
||||||
mail.mail_address = self.login
|
mail_messages.append(mail)
|
||||||
mail_messages.append(mail)
|
except Exception as error:
|
||||||
|
print("Error processing email: {}".format(error))
|
||||||
|
|
||||||
return mail_messages
|
return mail_messages
|
||||||
|
|
||||||
def _get_messages_from_folder_for_imapclient(self, imap, folder="INBOX") -> list:
|
def _extract_body(self, email_message: Message) -> str:
|
||||||
|
"""提取邮件正文"""
|
||||||
|
body = ""
|
||||||
|
|
||||||
|
# 遍历邮件部分
|
||||||
|
for part in email_message.walk():
|
||||||
|
try:
|
||||||
|
content_type = part.get_content_type()
|
||||||
|
|
||||||
|
if content_type == "text/html":
|
||||||
|
# 处理HTML内容
|
||||||
|
payload = part.get_payload(decode=True)
|
||||||
|
if payload:
|
||||||
|
body += payload.decode("utf-8", errors="ignore")
|
||||||
|
elif content_type == "text/plain":
|
||||||
|
# 处理纯文本内容
|
||||||
|
payload = part.get_payload()
|
||||||
|
if payload:
|
||||||
|
body += payload
|
||||||
|
except Exception as error:
|
||||||
|
print("Error extracting body part: {}".format(error))
|
||||||
|
|
||||||
|
return body
|
||||||
|
|
||||||
|
def _get_messages_from_folder_for_imapclient(self, imap, folder: str = "INBOX") -> List[MailPojo]:
|
||||||
|
"""从指定文件夹获取邮件(IMAPClient方式)"""
|
||||||
mail_messages = []
|
mail_messages = []
|
||||||
|
|
||||||
|
# 搜索邮件
|
||||||
search_terms = 'SINCE "{}"'.format(
|
search_terms = 'SINCE "{}"'.format(
|
||||||
datetime.datetime.today().strftime(
|
datetime.datetime.today().strftime(DATE_FORMAT))
|
||||||
date_format))
|
|
||||||
print("{}: search terms is {}".format(self.login, search_terms))
|
print("{}: search terms is {}".format(self.login, search_terms))
|
||||||
|
|
||||||
imap.select_folder(folder)
|
imap.select_folder(folder)
|
||||||
messages = imap.search(['SINCE', datetime.datetime.today()])
|
messages = imap.search(['SINCE', datetime.datetime.today()])
|
||||||
print("{}: {} messages from our best friend".format(self.login, len(messages)))
|
print("{}: {} messages from our best friend".format(self.login, len(messages)))
|
||||||
|
|
||||||
if len(messages) == 0:
|
if len(messages) == 0:
|
||||||
return mail_messages
|
return mail_messages
|
||||||
|
|
||||||
|
# 处理每封邮件
|
||||||
for uid, message_data in imap.fetch(messages, 'RFC822').items():
|
for uid, message_data in imap.fetch(messages, 'RFC822').items():
|
||||||
try:
|
try:
|
||||||
email_message = email.message_from_bytes(message_data[b'RFC822'])
|
email_message = email.message_from_bytes(message_data[b'RFC822'])
|
||||||
|
|
||||||
|
# 获取发件人和主题
|
||||||
from_address = email_message.get('FROM')
|
from_address = email_message.get('FROM')
|
||||||
subject = email_message.get('subject')
|
subject = email_message.get('subject')
|
||||||
body = ""
|
|
||||||
hermes_mail_adress = "no-reply@hermes.com"
|
# 检查是否是Hermes邮件
|
||||||
if hermes_mail_adress in from_address or "outlook.com" in from_address or "hotmail" in from_address:
|
hermes_mail_address = "no-reply@hermes.com"
|
||||||
for part in email_message.walk():
|
if (hermes_mail_address in from_address or
|
||||||
print(part.get_content_type())
|
"outlook.com" in from_address or
|
||||||
if part.get_content_type() == "text/html":
|
"hotmail" in from_address):
|
||||||
body = body + part.get_payload(decode=True).decode("utf-8")
|
|
||||||
elif part.get_content_type() == "text/plain":
|
# 提取邮件正文
|
||||||
body = body + part.get_payload()
|
body = self._extract_body_for_imapclient(email_message)
|
||||||
if VALIDATION_URL_SUBJECT_fr in subject or VALIDATION_URL_SUBJECT_EN in subject or "Votre=20demande=20de=20rendez-vous" in subject or "Votre demande de rendez-vous" in body:
|
|
||||||
mail = MailPojo(subject=subject, body=body, from_address=from_address)
|
# 检查是否是预约验证邮件
|
||||||
|
if (VALIDATION_URL_SUBJECT_FR in subject or
|
||||||
|
VALIDATION_URL_SUBJECT_EN in subject or
|
||||||
|
"Votre=20demande=20de=20rendez-vous" in subject or
|
||||||
|
"Votre demande de rendez-vous" in body):
|
||||||
|
|
||||||
|
mail = MailPojo(
|
||||||
|
subject=subject,
|
||||||
|
body=body,
|
||||||
|
from_address=from_address
|
||||||
|
)
|
||||||
mail.isImapClient = True
|
mail.isImapClient = True
|
||||||
|
|
||||||
print("email is {}".format(self.login))
|
print("email is {}".format(self.login))
|
||||||
print("body is {}".format(body))
|
print("body is {}".format(body))
|
||||||
print("subject is {}".format(subject))
|
print("subject is {}".format(subject))
|
||||||
|
|
||||||
|
# 设置收件人地址
|
||||||
if len(mail.to_address) == 0:
|
if len(mail.to_address) == 0:
|
||||||
if "outlook.com" in from_address or "hotmail.com" in from_address:
|
if "outlook.com" in from_address or "hotmail.com" in from_address:
|
||||||
# it is a transferred email
|
# 转发邮件
|
||||||
mail.to_address = extract_email_from_from_address(from_address)
|
mail.to_address = extract_email_from_from_address(from_address)
|
||||||
else:
|
else:
|
||||||
mail.to_address = self.login
|
mail.to_address = self.login
|
||||||
|
|
||||||
mail_messages.append(mail)
|
mail_messages.append(mail)
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
print(error)
|
print("Error trying to read email_Message for {}: {}".format(self.login, error))
|
||||||
print("error trying to read email_Message for {}".format(self.login))
|
|
||||||
return mail_messages
|
return mail_messages
|
||||||
|
|
||||||
|
def _extract_body_for_imapclient(self, email_message: Message) -> str:
|
||||||
|
"""提取IMAPClient邮件正文"""
|
||||||
|
body = ""
|
||||||
|
|
||||||
#
|
for part in email_message.walk():
|
||||||
# Find the ReserveResultPojo object from persisted items of DB
|
content_type = part.get_content_type()
|
||||||
#
|
|
||||||
|
if content_type == "text/html":
|
||||||
|
payload = part.get_payload(decode=True)
|
||||||
|
if payload:
|
||||||
|
body += payload.decode("utf-8", errors="ignore")
|
||||||
|
elif content_type == "text/plain":
|
||||||
|
payload = part.get_payload()
|
||||||
|
if payload:
|
||||||
|
body += payload
|
||||||
|
|
||||||
|
return body
|
||||||
|
|
||||||
|
|
||||||
|
# 邮件处理相关函数
|
||||||
def find_item_by_url(url: str, successful_items) -> Union[None, ReserveResultPojo]:
|
def find_item_by_url(url: str, successful_items) -> Union[None, ReserveResultPojo]:
|
||||||
|
"""根据URL查找预约结果对象"""
|
||||||
print("url is :" + url)
|
print("url is :" + url)
|
||||||
parts = url.split('/')
|
parts = url.split('/')
|
||||||
_id = parts[5]
|
_id = parts[5]
|
||||||
|
|
||||||
if len(_id) == 6:
|
if len(_id) == 6:
|
||||||
for item in successful_items:
|
for item in successful_items:
|
||||||
if item.id == _id:
|
if item.id == _id:
|
||||||
return item
|
return item
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def need_to_valid_url(url: str, item: Union[ReserveResultPojo, None]) -> bool:
|
def need_to_valid_url(url: str, item: Union[ReserveResultPojo, None]) -> bool:
|
||||||
|
"""判断是否需要验证URL"""
|
||||||
print("url is :" + url)
|
print("url is :" + url)
|
||||||
parts = url.split('/')
|
parts = url.split('/')
|
||||||
id = parts[5]
|
_id = parts[5]
|
||||||
if len(id) == 6:
|
|
||||||
|
if len(_id) == 6:
|
||||||
if item:
|
if item:
|
||||||
if item.url_validated is not None:
|
if item.url_validated is not None:
|
||||||
return not item.url_validated
|
return not item.url_validated
|
||||||
else:
|
else:
|
||||||
# if url_validated is None
|
# 如果url_validated为None,需要验证
|
||||||
return True
|
return True
|
||||||
return True
|
return True
|
||||||
else:
|
|
||||||
print("id not valid:{}".format(id))
|
print("id not valid:{}".format(_id))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def need_to_check_email(mail: str, successful_items) -> bool:
|
def need_to_check_email(mail: str, successful_items) -> bool:
|
||||||
|
"""判断是否需要检查邮件"""
|
||||||
print("successful_items size is " + str(len(successful_items)))
|
print("successful_items size is " + str(len(successful_items)))
|
||||||
|
|
||||||
|
# 特殊处理
|
||||||
if mail == "saigecong1990@pissmail.com":
|
if mail == "saigecong1990@pissmail.com":
|
||||||
return True
|
return True
|
||||||
filtered_items = list(filter(lambda item: item.email == mail, successful_items))
|
|
||||||
# has validated value
|
# 过滤已验证的项目
|
||||||
if len(filtered_items) > 0:
|
filtered_items = [item for item in successful_items if item.email == mail]
|
||||||
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 = [item for item in filtered_items
|
||||||
if len(validated_items) > 0:
|
if item.url_validated is not None and item.url_validated is True]
|
||||||
return False
|
|
||||||
else:
|
return len(validated_items) == 0
|
||||||
return True
|
|
||||||
else:
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def find_links_to_validate_from_mail_list(mail_list: list, logger):
|
def find_links_to_validate_from_mail_list(mail_list: List[MailAddress], logger) -> None:
|
||||||
|
"""从邮件列表中查找需要验证的链接"""
|
||||||
if not mail_list:
|
if not mail_list:
|
||||||
return
|
return
|
||||||
# check time before start checking emails
|
|
||||||
|
# 检查时间前开始检查邮件
|
||||||
contact_to_book_list = MONGO_STORE_MANAGER.get_all_contact_to_book_list()
|
contact_to_book_list = MONGO_STORE_MANAGER.get_all_contact_to_book_list()
|
||||||
successful_items = MONGO_STORE_MANAGER.get_all_successful_items_for_day()
|
successful_items = MONGO_STORE_MANAGER.get_all_successful_items_for_day()
|
||||||
|
|
||||||
mails_messages = []
|
mails_messages = []
|
||||||
with ThreadPoolExecutor(max_workers=200) as executor:
|
|
||||||
|
# 使用线程池处理邮件
|
||||||
|
with ThreadPoolExecutor(max_workers=20) as executor:
|
||||||
|
futures = []
|
||||||
|
|
||||||
for mail in mail_list:
|
for mail in mail_list:
|
||||||
# check whether we need to read mail
|
# 检查是否需要读取邮件
|
||||||
if need_to_check_email(mail.mail, successful_items):
|
if need_to_check_email(mail.mail, successful_items):
|
||||||
mail_reader = MailReader(mail.mail, mail.password)
|
mail_reader = MailReader(mail.mail, mail.password)
|
||||||
executor.submit(mail_reader.read_emails, mails_messages)
|
future = executor.submit(mail_reader.read_emails, mails_messages)
|
||||||
|
futures.append(future)
|
||||||
|
|
||||||
|
# 等待所有任务完成
|
||||||
|
for future in futures:
|
||||||
|
try:
|
||||||
|
future.result()
|
||||||
|
except Exception as e:
|
||||||
|
print("Error processing mail: {}".format(e))
|
||||||
|
|
||||||
|
# 刷新成功的项目
|
||||||
_refreshed_successful_items = MONGO_STORE_MANAGER.get_all_successful_items_for_day()
|
_refreshed_successful_items = MONGO_STORE_MANAGER.get_all_successful_items_for_day()
|
||||||
|
|
||||||
|
# 处理邮件中的链接
|
||||||
for mail in mails_messages:
|
for mail in mails_messages:
|
||||||
match = re.search(VALIDATION_URL_REGEX, mail.body)
|
match = re.search(VALIDATION_URL_REGEX, mail.body)
|
||||||
if match:
|
if match:
|
||||||
url = match.group(0)
|
url = match.group(0)
|
||||||
_item = find_item_by_url(url, _refreshed_successful_items)
|
_item = find_item_by_url(url, _refreshed_successful_items)
|
||||||
|
|
||||||
if need_to_valid_url(url, _item):
|
if need_to_valid_url(url, _item):
|
||||||
logger.info("need to validate url: " + url)
|
logger.info("need to validate url: " + url)
|
||||||
_model = ""
|
_model = ""
|
||||||
|
_used_ip = ""
|
||||||
if _item:
|
if _item:
|
||||||
_model = _item.model
|
_model = _item.model
|
||||||
MONGO_STORE_MANAGER.save_links_to_validate(url, mail.to_address, model=_model,
|
_used_ip = _item.current_ip
|
||||||
_all_contact_list=contact_to_book_list)
|
|
||||||
|
MONGO_STORE_MANAGER.save_links_to_validate(
|
||||||
|
url,
|
||||||
|
mail.to_address,
|
||||||
|
model=_model,
|
||||||
|
_all_contact_list=contact_to_book_list, _used_ip= _used_ip)
|
||||||
else:
|
else:
|
||||||
logger.info("do not need to click url --> {}".format(mail.mail_address))
|
logger.info("do not need to click url --> {}".format(mail.mail_address))
|
||||||
|
|
||||||
|
|
||||||
|
# 主函数
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# mail_address1 = MailAddress(mail="tinagonzales685585@aol.com", password="yhihvdkrbxnksema")
|
# 读取联系人列表
|
||||||
# mail_list = [mail_address1]
|
|
||||||
contact_to_book_list = read_contacts(
|
contact_to_book_list = read_contacts(
|
||||||
# file_name="/Users/rdv/Desktop/contact_list_not_used_contacts.xlsx")
|
|
||||||
# file_name="/Users/lpan/Desktop/contact_list_not_used_contacts.xlsx")
|
|
||||||
# file_name="/Users/rdv/Desktop/contact_list_2025-10-30.xlsx")
|
|
||||||
# file_name="~/Desktop/contact_list_all.xlsx")
|
|
||||||
file_name="~/Desktop/contact_list_2025-11-06.xlsx")
|
file_name="~/Desktop/contact_list_2025-11-06.xlsx")
|
||||||
# file_name="/Users/rdv/Desktop/contact_list_all_studo_gmx_us.xlsx")
|
|
||||||
# file_name="/Users/rdv/Desktop/contact_list_2025-05-24.xlsx")
|
# 获取目标邮箱列表
|
||||||
all_mail_list = MONGO_STORE_MANAGER.get_destination_emails()
|
all_mail_list = MONGO_STORE_MANAGER.get_destination_emails()
|
||||||
|
|
||||||
|
# 筛选需要检查的邮件列表
|
||||||
mail_list_to_check = []
|
mail_list_to_check = []
|
||||||
for contact in contact_to_book_list:
|
for contact in contact_to_book_list:
|
||||||
for mail in all_mail_list:
|
for mail in all_mail_list:
|
||||||
if contact.mail == mail.mail:
|
if contact.mail == mail.mail:
|
||||||
mail_list_to_check.append(mail)
|
mail_list_to_check.append(mail)
|
||||||
|
|
||||||
|
# 设置日志记录器
|
||||||
logger = logging.getLogger()
|
logger = logging.getLogger()
|
||||||
|
|
||||||
|
# 获取已验证的链接列表
|
||||||
_all_links = MONGO_STORE_MANAGER.get_links_to_validate()
|
_all_links = MONGO_STORE_MANAGER.get_links_to_validate()
|
||||||
|
|
||||||
|
# 过滤掉已处理的邮件
|
||||||
filter_mail = []
|
filter_mail = []
|
||||||
for mail_pojo in mail_list_to_check:
|
for mail_pojo in mail_list_to_check:
|
||||||
_to_add = True
|
_to_add = True
|
||||||
@@ -317,6 +438,6 @@ if __name__ == '__main__':
|
|||||||
_to_add = False
|
_to_add = False
|
||||||
if _to_add:
|
if _to_add:
|
||||||
filter_mail.append(mail_pojo)
|
filter_mail.append(mail_pojo)
|
||||||
# filter_mail.append(MailAddress("saigecong1990@pissmail.com", "cvExXKOP8oY1D@"))
|
# filter_mail = [MailAddress("utatapi@gmx.net", "RSAzHAFek8s")]
|
||||||
# filter_mail = [MailAddress("saigecong1990@pissmail.com", "cvExXKOP8oY1D@")]
|
# 处理邮件
|
||||||
find_links_to_validate_from_mail_list(filter_mail, logger)
|
find_links_to_validate_from_mail_list(filter_mail, logger)
|
||||||
|
|||||||
@@ -113,6 +113,9 @@ class ReserveResultPojo:
|
|||||||
if 'validated_at' in source:
|
if 'validated_at' in source:
|
||||||
validated_at = source['validated_at']
|
validated_at = source['validated_at']
|
||||||
result.validated_at = validated_at
|
result.validated_at = validated_at
|
||||||
|
if 'current_ip' in source:
|
||||||
|
current_ip = source['current_ip']
|
||||||
|
result.current_ip = current_ip
|
||||||
result.id = id
|
result.id = id
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,11 @@ import json
|
|||||||
import random
|
import random
|
||||||
import re
|
import re
|
||||||
from typing import Union
|
from typing import Union
|
||||||
|
|
||||||
|
import requests
|
||||||
|
|
||||||
# import requests
|
# import requests
|
||||||
from curl_cffi import requests
|
# from curl_cffi import requests
|
||||||
|
|
||||||
from captcha.jspl_encoder_wrapper import encrpte_to_jspl
|
from captcha.jspl_encoder_wrapper import encrpte_to_jspl
|
||||||
from models.jsdata_le_pojo import JsDataLeTypePojo
|
from models.jsdata_le_pojo import JsDataLeTypePojo
|
||||||
@@ -106,7 +109,7 @@ class CaptchaResultGetter:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
def get_ch_raw_data_from_js_data(self, js_data: JsDataPojo, old_valid_cookie) -> str:
|
def get_ch_raw_data_from_js_data(self, js_data: JsDataPojo, old_valid_cookie) -> str:
|
||||||
_tag_version = "5.1.8"
|
_tag_version = "5.1.9"
|
||||||
_jspl = encrpte_to_jspl(js_data.to_url_encoded_json())
|
_jspl = encrpte_to_jspl(js_data.to_url_encoded_json())
|
||||||
_raw_data = "jspl={}&eventCounters=%5B%5D&jsType=ch&cid={}&ddk=789361B674144528D0B7EE76B35826&Referer=https%253A%252F%252Frendezvousparis.hermes.com%252Fclient%252Fregister&request=%252Fclient%252Fregister&responsePage=origin&ddv={}".format(
|
_raw_data = "jspl={}&eventCounters=%5B%5D&jsType=ch&cid={}&ddk=789361B674144528D0B7EE76B35826&Referer=https%253A%252F%252Frendezvousparis.hermes.com%252Fclient%252Fregister&request=%252Fclient%252Fregister&responsePage=origin&ddv={}".format(
|
||||||
_jspl, old_valid_cookie, _tag_version)
|
_jspl, old_valid_cookie, _tag_version)
|
||||||
@@ -177,7 +180,7 @@ class CaptchaResultGetter:
|
|||||||
# old_valid_cookie=old_valid_cookie)
|
# old_valid_cookie=old_valid_cookie)
|
||||||
_cid = get_datadome_cookies(old_valid_cookie)
|
_cid = get_datadome_cookies(old_valid_cookie)
|
||||||
_jspl = encrpte_to_jspl(js_le_type_data.to_url_encoded_json())
|
_jspl = encrpte_to_jspl(js_le_type_data.to_url_encoded_json())
|
||||||
_raw_data = "jsData={}&eventCounters=%7B%22mousemove%22%3A{}%2C%22click%22%3A{}%2C%22scroll%22%3A{}%2C%22touchstart%22%3A{}%2C%22touchend%22%3A{}%2C%22touchmove%22%3A{}%2C%22keydown%22%3A{}%2C%22keyup%22%3A{}%7D&jsType=le&cid={}&ddk=789361B674144528D0B7EE76B35826&Referer=https%253A%252F%252Frendezvousparis.hermes.com%252Fclient%252Fregister&request=%252Fclient%252Fregister&responsePage=origin&ddv=5.1.8".format(
|
_raw_data = "jsData={}&eventCounters=%7B%22mousemove%22%3A{}%2C%22click%22%3A{}%2C%22scroll%22%3A{}%2C%22touchstart%22%3A{}%2C%22touchend%22%3A{}%2C%22touchmove%22%3A{}%2C%22keydown%22%3A{}%2C%22keyup%22%3A{}%7D&jsType=le&cid={}&ddk=789361B674144528D0B7EE76B35826&Referer=https%253A%252F%252Frendezvousparis.hermes.com%252Fclient%252Fregister&request=%252Fclient%252Fregister&responsePage=origin&ddv=5.1.9".format(
|
||||||
_jspl, mousemove_count, click_count, scroll_count, touch_count, touch_count,
|
_jspl, mousemove_count, click_count, scroll_count, touch_count, touch_count,
|
||||||
touch_move,
|
touch_move,
|
||||||
key_count,
|
key_count,
|
||||||
|
|||||||
Reference in New Issue
Block a user