read mail after request
This commit is contained in:
@@ -7,6 +7,7 @@ from pymongo import MongoClient
|
|||||||
from models.LinkPojo import LinkPojo
|
from models.LinkPojo import LinkPojo
|
||||||
from models.ReserveResultPojo import ReserveResultPojo
|
from models.ReserveResultPojo import ReserveResultPojo
|
||||||
from models.contact_pojo import ContactPojo
|
from models.contact_pojo import ContactPojo
|
||||||
|
from models.mail_pojo import MailAddress
|
||||||
|
|
||||||
MONGO_DB_URL = "mongo.lpaconsulting.fr"
|
MONGO_DB_URL = "mongo.lpaconsulting.fr"
|
||||||
CAPTCHA_ERROR_COLLECTION_PREFIX = "CAPTCHA_ERROR_"
|
CAPTCHA_ERROR_COLLECTION_PREFIX = "CAPTCHA_ERROR_"
|
||||||
@@ -72,6 +73,43 @@ 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, _all_contact_list: list):
|
||||||
|
collection_to_use = self.db[LINKS_TO_VALIDATE]
|
||||||
|
updated_at = time.strftime("%H:%M:%S", time.localtime())
|
||||||
|
_ip_country = "FR"
|
||||||
|
# find ip_country info
|
||||||
|
for _contact in _all_contact_list:
|
||||||
|
if _contact.mail == mail_address:
|
||||||
|
_ip_country = _contact.ip_country
|
||||||
|
|
||||||
|
if len(mail_address) > 0:
|
||||||
|
collection_to_use.replace_one(filter={'_id': mail_address, }, replacement={
|
||||||
|
u'url': link,
|
||||||
|
u'email': mail_address,
|
||||||
|
u'ip_country': _ip_country,
|
||||||
|
"updated_at": updated_at
|
||||||
|
},
|
||||||
|
upsert=True)
|
||||||
|
else:
|
||||||
|
collection_to_use.replace_one(filter={'_id': link, }, replacement={
|
||||||
|
u'url': link,
|
||||||
|
u'ip_country': _ip_country,
|
||||||
|
"updated_at": updated_at
|
||||||
|
},
|
||||||
|
upsert=True)
|
||||||
|
|
||||||
|
def get_destination_emails(self) -> list:
|
||||||
|
collection_name = DESTINATION_EMAIL_LIST
|
||||||
|
email_list = []
|
||||||
|
try:
|
||||||
|
collection_to_use = self.db[collection_name]
|
||||||
|
for document in collection_to_use.find():
|
||||||
|
email_list.append(MailAddress.from_firestore_dict(document))
|
||||||
|
except Exception as error:
|
||||||
|
self.logger.info(error)
|
||||||
|
print(error)
|
||||||
|
return email_list
|
||||||
|
|
||||||
def get_links_to_validate(self) -> list:
|
def get_links_to_validate(self) -> list:
|
||||||
collection_name = LINKS_TO_VALIDATE
|
collection_name = LINKS_TO_VALIDATE
|
||||||
link_list = []
|
link_list = []
|
||||||
|
|||||||
Executable
+106
@@ -0,0 +1,106 @@
|
|||||||
|
import imaplib
|
||||||
|
|
||||||
|
from imapclient import IMAPClient
|
||||||
|
|
||||||
|
DOMAIN_YAHOO = "yahoo.com"
|
||||||
|
DOMAIN_SINA = "sina.com"
|
||||||
|
DOMAIN_HOTMAIL = "hotmail.com"
|
||||||
|
DOMAIN_TIM_IT = "tim.it"
|
||||||
|
DOMAIN_163 = "163.com"
|
||||||
|
DOMAIN_RAMBLER_RU = "rambler.ru"
|
||||||
|
DOMAIN_ALICE_IT = "alice.it"
|
||||||
|
DOMAIN_MARS_DTI_NE_JP = "mars.dti.ne.jp"
|
||||||
|
DOMAN_BTVM_NE_JP = "btvm.ne.jp"
|
||||||
|
DOMAN_AURORA_DTI_NE_JP = "aurora.dti.ne.jp"
|
||||||
|
DOMAN_GMAIL = "gmail.com"
|
||||||
|
DOMAIN_GMX = "gmx.com"
|
||||||
|
DOMAIN_GMX_FR = "gmx.fr"
|
||||||
|
DOMAIN_GMX_US = "gmx.us"
|
||||||
|
DOMAIN_GMX_CH = "gmx.ch"
|
||||||
|
DOMAIN_ONET = "onet.pl"
|
||||||
|
DOMAIN_NAVER = "naver.com"
|
||||||
|
DOMAIN_INBOX_LV = "inbox.lv"
|
||||||
|
|
||||||
|
DOMAIN_PISS_MAIL = "pissmail.com"
|
||||||
|
DOMAIN_INCEL_EMAIL = "incel.email"
|
||||||
|
DOMAIN_SHITPOSTING_EXPERT = "shitposting.expert"
|
||||||
|
DOMAIN_HATESJE_WS = "hatesje.ws"
|
||||||
|
DOMAIN_CHILD_PIZZA = "child.pizza"
|
||||||
|
DOMAIN_GENOCIDE_FUN = "genocide.fun"
|
||||||
|
DOMAIN_DMC_CHAT = "dmc.chat"
|
||||||
|
|
||||||
|
AOL_IMAP_SERVER = "imap.aol.com"
|
||||||
|
IMAP_SERVER_163 = "imap.163.com"
|
||||||
|
IMAP_SERVER_SINA = "imap.sina.com"
|
||||||
|
YAHOO_IMAP_SERVER = "imap.mail.yahoo.com"
|
||||||
|
HOTMAIL_IMAP_SERVER = "outlook.office365.com"
|
||||||
|
RAMBLER_IMAP_SERVER = "imap.rambler.ru"
|
||||||
|
ALICE_IMAP_SERVER = "in.alice.it"
|
||||||
|
TIME_IT_SERVER = "imap.tim.it"
|
||||||
|
MARS_DTI_NE_JP_SERVER = "imap.cm.dream.jp"
|
||||||
|
NAVER_SERVER = "imap.naver.com"
|
||||||
|
BTVM_NE_JP = "imap.btvm.ne.jp"
|
||||||
|
SEREVER_GMAIL = "imap.gmail.com"
|
||||||
|
SERVER_IMAGE_ONET = "imap.poczta.onet.pl"
|
||||||
|
SERVER_GMX = "imap.gmx.com"
|
||||||
|
SERVER_PISS_MAIL = "mail.pissmail.com"
|
||||||
|
INBOX_LV = "mail.inbox.lv"
|
||||||
|
|
||||||
|
|
||||||
|
def show_folders(imap) -> list:
|
||||||
|
folders = []
|
||||||
|
isImapClient = isinstance(imap, IMAPClient)
|
||||||
|
if not isImapClient:
|
||||||
|
for i in imap.list()[1]:
|
||||||
|
l = i.decode().split(' "/" ')
|
||||||
|
if len(l) > 1:
|
||||||
|
folders.append(l[1])
|
||||||
|
if len(folders) == 0:
|
||||||
|
folders.append('INBOX')
|
||||||
|
return folders
|
||||||
|
else:
|
||||||
|
list = imap.list_folders()
|
||||||
|
for i in list:
|
||||||
|
name = i[-1]
|
||||||
|
folders.append(name)
|
||||||
|
return folders
|
||||||
|
|
||||||
|
|
||||||
|
def create_imap(login: str):
|
||||||
|
# create an IMAP4 class with SSL
|
||||||
|
if DOMAIN_163 in login:
|
||||||
|
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 or DOMAIN_GMX_FR in login or DOMAIN_GMX_US in login or DOMAIN_GMX_CH in login:
|
||||||
|
imap = imaplib.IMAP4_SSL(SERVER_GMX)
|
||||||
|
elif DOMAIN_SINA in login:
|
||||||
|
# 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:
|
||||||
|
imap = imaplib.IMAP4_SSL(RAMBLER_IMAP_SERVER)
|
||||||
|
elif DOMAN_BTVM_NE_JP in login:
|
||||||
|
imap = imaplib.IMAP4_SSL(BTVM_NE_JP)
|
||||||
|
elif DOMAN_GMAIL in login:
|
||||||
|
imap = imaplib.IMAP4_SSL(SEREVER_GMAIL, port=993)
|
||||||
|
elif DOMAIN_ONET in login:
|
||||||
|
imap = IMAPClient(SERVER_IMAGE_ONET, use_uid=True)
|
||||||
|
elif DOMAIN_TIM_IT in login:
|
||||||
|
imap = imaplib.IMAP4(TIME_IT_SERVER)
|
||||||
|
elif DOMAIN_ALICE_IT in login:
|
||||||
|
imap = imaplib.IMAP4(ALICE_IMAP_SERVER, port=143)
|
||||||
|
elif DOMAIN_MARS_DTI_NE_JP in login:
|
||||||
|
imap = imaplib.IMAP4(MARS_DTI_NE_JP_SERVER, port=143)
|
||||||
|
elif DOMAN_AURORA_DTI_NE_JP in login:
|
||||||
|
imap = imaplib.IMAP4(MARS_DTI_NE_JP_SERVER, port=143)
|
||||||
|
elif DOMAIN_NAVER in login:
|
||||||
|
imap = imaplib.IMAP4_SSL(NAVER_SERVER, port=993)
|
||||||
|
elif DOMAIN_INBOX_LV in login:
|
||||||
|
imap = imaplib.IMAP4_SSL(INBOX_LV, port=993)
|
||||||
|
elif DOMAIN_PISS_MAIL in login or DOMAIN_CHILD_PIZZA in login or DOMAIN_DMC_CHAT in login or DOMAIN_GENOCIDE_FUN in login or DOMAIN_HATESJE_WS in login or DOMAIN_INCEL_EMAIL in login or DOMAIN_SHITPOSTING_EXPERT in login:
|
||||||
|
imap = imaplib.IMAP4_SSL(SERVER_PISS_MAIL, port=993)
|
||||||
|
else:
|
||||||
|
imap = imaplib.IMAP4_SSL(AOL_IMAP_SERVER)
|
||||||
|
return imap
|
||||||
Executable
+265
@@ -0,0 +1,265 @@
|
|||||||
|
import datetime
|
||||||
|
import email
|
||||||
|
import re
|
||||||
|
from builtins import list
|
||||||
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
|
from datetime import time
|
||||||
|
from email.header import decode_header
|
||||||
|
from email.message import Message
|
||||||
|
|
||||||
|
from imapclient import IMAPClient
|
||||||
|
from db.mongo_manager import MONGO_STORE_MANAGER
|
||||||
|
from mail.mail_constants import DOMAIN_HOTMAIL, create_imap
|
||||||
|
from models.mail_pojo import MailPojo
|
||||||
|
|
||||||
|
VALIDATION_URL_SUBJECT_fr = 'Validation de votre demande de rendez-vous'
|
||||||
|
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]+"""
|
||||||
|
PART_VALIDATION_URL_REGEX = """client\/register\/[A-Z0-9]+\/validate.code=[A-Z0-9]+"""
|
||||||
|
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'
|
||||||
|
|
||||||
|
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
|
||||||
|
if (re.fullmatch(EMAIL_ADDRESS_REGEX, email)):
|
||||||
|
print("Valid Email")
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
print("Invalid Email")
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def find_from_mail(param):
|
||||||
|
from_address, encoded_algo = param[0]
|
||||||
|
if isinstance(from_address, bytes):
|
||||||
|
from_address = from_address.decode(encoded_algo)
|
||||||
|
if not check_email_address(from_address) and len(param) == 2:
|
||||||
|
from_address, new_encode = param[1]
|
||||||
|
if new_encode is None:
|
||||||
|
new_encode = encoded_algo
|
||||||
|
if isinstance(from_address, bytes):
|
||||||
|
from_address = from_address.decode(new_encode)
|
||||||
|
return from_address.strip(" ").strip(">").strip("<")
|
||||||
|
return from_address.strip(" ").strip(">").strip("<")
|
||||||
|
|
||||||
|
|
||||||
|
class MailReader():
|
||||||
|
def __init__(self, login, password):
|
||||||
|
self.login = login
|
||||||
|
self.password = password
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def show_folders(imap) -> list:
|
||||||
|
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)
|
||||||
|
isImapClient = isinstance(imap, IMAPClient)
|
||||||
|
print("isImapClient is " + str(isImapClient))
|
||||||
|
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))
|
||||||
|
if not isImapClient:
|
||||||
|
folder_list = self.show_folders(imap)
|
||||||
|
for folder in folder_list:
|
||||||
|
print("folder is {}".format(folder))
|
||||||
|
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))
|
||||||
|
else:
|
||||||
|
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"))
|
||||||
|
if not isImapClient:
|
||||||
|
imap.close()
|
||||||
|
imap.logout()
|
||||||
|
mails_messages.extend(mail_list)
|
||||||
|
return mail_list
|
||||||
|
|
||||||
|
def _get_messages_from_folder(self, imap, subject, folder="INBOX") -> list:
|
||||||
|
imap.select(folder)
|
||||||
|
mail_messages = []
|
||||||
|
typ, data = imap.search(None, '(SUBJECT "{}" SINCE "{}")'.format(subject,
|
||||||
|
datetime.datetime.today().strftime(
|
||||||
|
date_format)))
|
||||||
|
for i in data[0].split():
|
||||||
|
# fetch the email message by ID
|
||||||
|
res, msg = imap.fetch(i.decode("utf-8"), "(RFC822)")
|
||||||
|
body = ''
|
||||||
|
for response in msg:
|
||||||
|
if isinstance(response, tuple):
|
||||||
|
# parse a bytes email into a message object
|
||||||
|
msg = email.message_from_bytes(response[1])
|
||||||
|
# decode the email subject
|
||||||
|
subject, subject_encoded = decode_header(msg["Subject"])[0]
|
||||||
|
received_date = msg["Date"]
|
||||||
|
if isinstance(subject, bytes):
|
||||||
|
# if it's a bytes, decode to str
|
||||||
|
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")))
|
||||||
|
print("Email:", self.login)
|
||||||
|
print("From:", from_address)
|
||||||
|
print("To:", to_email)
|
||||||
|
print("Subject:", subject)
|
||||||
|
# if the email message is multipart
|
||||||
|
if msg.is_multipart():
|
||||||
|
# iterate over email parts
|
||||||
|
for part in msg.walk():
|
||||||
|
try:
|
||||||
|
# get the email body
|
||||||
|
payloads = part.get_payload()
|
||||||
|
if isinstance(payloads, list):
|
||||||
|
for payload in payloads:
|
||||||
|
if isinstance(payload, Message):
|
||||||
|
body = body + payload.get_payload(decode=True).decode("iso-8859-1")
|
||||||
|
# print(body)
|
||||||
|
except Exception as Error:
|
||||||
|
print(Error)
|
||||||
|
else:
|
||||||
|
body = msg.get_payload(decode=True).decode()
|
||||||
|
print(body)
|
||||||
|
if VALIDATION_URL_SUBJECT_fr in subject or VALIDATION_URL_SUBJECT_EN in subject:
|
||||||
|
mail = MailPojo(subject=subject, body=body, from_address=from_address)
|
||||||
|
if to_email is None:
|
||||||
|
mail.to_address = self.login
|
||||||
|
else:
|
||||||
|
mail.to_address = to_email
|
||||||
|
mail.mail_address = self.login
|
||||||
|
mail_messages.append(mail)
|
||||||
|
return mail_messages
|
||||||
|
|
||||||
|
def _get_messages_from_folder_for_imapclient(self, imap, 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 or "appointment2022@aol.com":
|
||||||
|
for part in email_message.walk():
|
||||||
|
print(part.get_content_type())
|
||||||
|
if part.get_content_type() == "text/html":
|
||||||
|
body = body + part.get_payload(decode=True).decode("utf-8")
|
||||||
|
elif part.get_content_type() == "text/plain":
|
||||||
|
body = body + part.get_payload()
|
||||||
|
if VALIDATION_URL_SUBJECT_fr in subject or VALIDATION_URL_SUBJECT_EN in subject:
|
||||||
|
mail = MailPojo(subject=subject, body=body, from_address=from_address)
|
||||||
|
mail.isImapClient = True
|
||||||
|
print("email is {}".format(self.login))
|
||||||
|
print("body is {}".format(body))
|
||||||
|
print("subject is {}".format(subject))
|
||||||
|
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 need_to_valid_url(url: str, successful_items) -> bool:
|
||||||
|
# return True
|
||||||
|
# if len(successful_items) == 0:
|
||||||
|
# return False
|
||||||
|
print("url is :" + url)
|
||||||
|
parts = url.split('/')
|
||||||
|
id = parts[5]
|
||||||
|
if len(id) == 6:
|
||||||
|
if id == "CS93VB":
|
||||||
|
print("found")
|
||||||
|
for item in successful_items:
|
||||||
|
if item.id == id:
|
||||||
|
if item.url_validated is not None:
|
||||||
|
return not item.url_validated
|
||||||
|
else:
|
||||||
|
# if url_validated is None
|
||||||
|
if item.url_validated is not None:
|
||||||
|
return not item.url_validated
|
||||||
|
return True
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
print("id not valid:{}".format(id))
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def need_to_check_email(mail: str, successful_items) -> bool:
|
||||||
|
print("successful_items size is " + str(len(successful_items)))
|
||||||
|
filtered_items = list(filter(lambda item: item.email == mail, successful_items))
|
||||||
|
# has validated value
|
||||||
|
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))
|
||||||
|
if len(validated_items) > 0:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def find_links_to_validate_from_mail_list(mail_list: list):
|
||||||
|
# check time before start checking emails
|
||||||
|
contact_to_book_list = MONGO_STORE_MANAGER.get_all_contact_to_book_list()
|
||||||
|
successful_items = MONGO_STORE_MANAGER.get_all_successful_items_for_day()
|
||||||
|
mails_messages = []
|
||||||
|
with ThreadPoolExecutor(max_workers=len(mail_list)) as executor:
|
||||||
|
for mail in mail_list:
|
||||||
|
# check whether we need to read mail
|
||||||
|
if need_to_check_email(mail.mail, successful_items):
|
||||||
|
mail_reader = MailReader(mail.mail, mail.password)
|
||||||
|
executor.submit(mail_reader.read_emails, mails_messages)
|
||||||
|
# for mail in mail_list:
|
||||||
|
# # check whether we need to read mail
|
||||||
|
# if need_to_check_email(mail.mail, successful_items):
|
||||||
|
# mail_reader = MailReader(mail.mail, mail.password)
|
||||||
|
# mail_reader.read_emails(mails_messages)
|
||||||
|
|
||||||
|
for mail in mails_messages:
|
||||||
|
match = re.search(VALIDATION_URL_REGEX, mail.body)
|
||||||
|
if match:
|
||||||
|
url = match.group(0)
|
||||||
|
if need_to_valid_url(url, successful_items):
|
||||||
|
print("need to validate url: " + url)
|
||||||
|
MONGO_STORE_MANAGER.save_links_to_validate(url, mail.to_address,
|
||||||
|
_all_contact_list=contact_to_book_list)
|
||||||
|
else:
|
||||||
|
print("do not need to click url --> {}".format(mail.mail_address))
|
||||||
@@ -39,13 +39,15 @@ class ContactPojo:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_firestore_dict(source):
|
def from_firestore_dict(source):
|
||||||
ccid = source['ccid']
|
|
||||||
phone = source['phone']
|
phone = source['phone']
|
||||||
position = source['position']
|
|
||||||
passport = source['passport']
|
passport = source['passport']
|
||||||
email = source['mail']
|
email = source['mail']
|
||||||
last_name = source['last_name']
|
last_name = source['last_name']
|
||||||
first_name = source['first_name']
|
first_name = source['first_name']
|
||||||
result = ContactPojo(ccid=ccid, phone_number=phone, passport_number=passport, position=position, mail=email,
|
ip_country = "FR"
|
||||||
|
if source.get('ip_country'):
|
||||||
|
ip_country = source['ip_country']
|
||||||
|
result = ContactPojo(phone_number=phone, passport_number=passport, mail=email,
|
||||||
last_name=last_name, first_name=first_name)
|
last_name=last_name, first_name=first_name)
|
||||||
|
result.ip_country = ip_country
|
||||||
return result
|
return result
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
class MailAddress:
|
||||||
|
def __init__(self, mail, password):
|
||||||
|
self.mail = mail
|
||||||
|
self.password = password
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "邮箱:{}, 密码:{}".format(self.mail, self.password)
|
||||||
|
|
||||||
|
def to_firestore_dict(self):
|
||||||
|
dest = {
|
||||||
|
u'mail': self.mail,
|
||||||
|
u'password': self.password
|
||||||
|
}
|
||||||
|
return dest
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def from_firestore_dict(source):
|
||||||
|
password = source['password']
|
||||||
|
mail = source['mail']
|
||||||
|
return MailAddress(mail=mail, password=password)
|
||||||
|
|
||||||
|
|
||||||
|
class MailPojo:
|
||||||
|
from_address: str
|
||||||
|
to_address: str
|
||||||
|
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
|
||||||
|
self.to_address = ""
|
||||||
@@ -7,6 +7,7 @@ import time
|
|||||||
import pika
|
import pika
|
||||||
|
|
||||||
from db.mongo_manager import MONGO_STORE_MANAGER
|
from db.mongo_manager import MONGO_STORE_MANAGER
|
||||||
|
from mail.mail_reader_all_contacts import find_links_to_validate_from_mail_list
|
||||||
from models.ReserveResultPojo import ReserveResultPojo
|
from models.ReserveResultPojo import ReserveResultPojo
|
||||||
from models.contact_pojo import ContactPojo
|
from models.contact_pojo import ContactPojo
|
||||||
from models.jsdata_le_pojo import JsDataLeTypePojo
|
from models.jsdata_le_pojo import JsDataLeTypePojo
|
||||||
@@ -65,6 +66,7 @@ class AppointmentRequestSender(threading.Thread):
|
|||||||
self.cookiesPublisher = cookiesPublisher
|
self.cookiesPublisher = cookiesPublisher
|
||||||
self.channel = None
|
self.channel = None
|
||||||
self.valid_csrf = None
|
self.valid_csrf = None
|
||||||
|
self.list_to_retrieve_mails = sub_contact_list
|
||||||
self.contact_list = sub_contact_list
|
self.contact_list = sub_contact_list
|
||||||
self.queue_name = queue_name
|
self.queue_name = queue_name
|
||||||
self.proxy_to_use_list = proxy_to_use_list
|
self.proxy_to_use_list = proxy_to_use_list
|
||||||
@@ -175,6 +177,7 @@ class AppointmentRequestSender(threading.Thread):
|
|||||||
self.logger.info("will ack method.delivery_tag: " + str(method.delivery_tag))
|
self.logger.info("will ack method.delivery_tag: " + str(method.delivery_tag))
|
||||||
ch.basic_ack(delivery_tag=method.delivery_tag)
|
ch.basic_ack(delivery_tag=method.delivery_tag)
|
||||||
else:
|
else:
|
||||||
|
self.retreive_invalidate_urls()
|
||||||
self.logger.info("empty list")
|
self.logger.info("empty list")
|
||||||
time.sleep(120)
|
time.sleep(120)
|
||||||
self.logger.info("will basic_reject method.delivery_tag: " + str(method.delivery_tag))
|
self.logger.info("will basic_reject method.delivery_tag: " + str(method.delivery_tag))
|
||||||
@@ -188,3 +191,14 @@ class AppointmentRequestSender(threading.Thread):
|
|||||||
self.set_up_connection()
|
self.set_up_connection()
|
||||||
self.listen_to_queue(self.on_message)
|
self.listen_to_queue(self.on_message)
|
||||||
self.channel.start_consuming()
|
self.channel.start_consuming()
|
||||||
|
|
||||||
|
def retreive_invalidate_urls(self):
|
||||||
|
self.logger.info("will retreive_invalidate_urls")
|
||||||
|
_mail_list = MONGO_STORE_MANAGER.get_destination_emails()
|
||||||
|
_mail_list_filtred = []
|
||||||
|
for mail in _mail_list:
|
||||||
|
for _contact in self.list_to_retrieve_mails:
|
||||||
|
if _contact.mail == mail.mail:
|
||||||
|
_mail_list_filtred.append(mail)
|
||||||
|
self.logger.info("will call find_links_to_validate_from_mail_list, size = " + str(len(_mail_list_filtred)))
|
||||||
|
find_links_to_validate_from_mail_list(_mail_list_filtred)
|
||||||
|
|||||||
+5
-2
@@ -57,21 +57,24 @@ def send_appointment_request(message_queue_name, _contact_list):
|
|||||||
_cookiesPublisher = CookiesPublisher(queue_name=message_queue_name)
|
_cookiesPublisher = CookiesPublisher(queue_name=message_queue_name)
|
||||||
_cookiesPublisher.set_up_connection()
|
_cookiesPublisher.set_up_connection()
|
||||||
receiver = AppointmentRequestSender(sub_contact_list=_contact_list, proxy_to_use_list=MOBILE_PROXY_LIST_FR,
|
receiver = AppointmentRequestSender(sub_contact_list=_contact_list, proxy_to_use_list=MOBILE_PROXY_LIST_FR,
|
||||||
queue_name=message_queue_name,
|
queue_name=message_queue_name, just_send=True,
|
||||||
cookiesPublisher=_cookiesPublisher, logger=logger)
|
cookiesPublisher=_cookiesPublisher, logger=logger)
|
||||||
print("count is " + str(count))
|
print("count is " + str(count))
|
||||||
receiver.run()
|
receiver.run()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
contacts_file_path = '~/Desktop/contact_list_2024-03-16.xlsx'
|
contacts_file_path = '~/Desktop/contact_list_2024-03-20.xlsx'
|
||||||
_contact_list = read_contacts(contacts_file_path)
|
_contact_list = read_contacts(contacts_file_path)
|
||||||
_contact_list_to_book = filter_contacts(_contact_list)
|
_contact_list_to_book = filter_contacts(_contact_list)
|
||||||
_segment_number = 2
|
_segment_number = 2
|
||||||
logger.info("{} contacts to book".format(len(_contact_list_to_book)))
|
logger.info("{} contacts to book".format(len(_contact_list_to_book)))
|
||||||
|
last_thread = None
|
||||||
for i in range(0, _segment_number):
|
for i in range(0, _segment_number):
|
||||||
logger.info("segment is {}".format(i))
|
logger.info("segment is {}".format(i))
|
||||||
_step = int(len(_contact_list_to_book) / _segment_number)
|
_step = int(len(_contact_list_to_book) / _segment_number)
|
||||||
_sublist = _contact_list_to_book[i * _step:_step * (i + 1)]
|
_sublist = _contact_list_to_book[i * _step:_step * (i + 1)]
|
||||||
_thread1 = Thread(target=send_appointment_request, args=(TEST_QUEUE, _sublist))
|
_thread1 = Thread(target=send_appointment_request, args=(TEST_QUEUE, _sublist))
|
||||||
|
last_thread = _thread1
|
||||||
_thread1.start()
|
_thread1.start()
|
||||||
|
last_thread.join()
|
||||||
|
|||||||
+2
-1
@@ -73,7 +73,7 @@ class Sender:
|
|||||||
print("send request with cookie:{}".format(self.cookie_str))
|
print("send request with cookie:{}".format(self.cookie_str))
|
||||||
try:
|
try:
|
||||||
proxy_to_use = self.proxy_to_use
|
proxy_to_use = self.proxy_to_use
|
||||||
print(proxy_to_use)
|
# print(proxy_to_use)
|
||||||
response = requests.post(url=url, proxies=proxy_to_use, verify=False, headers=headers, data=data,
|
response = requests.post(url=url, proxies=proxy_to_use, verify=False, headers=headers, data=data,
|
||||||
timeout=15)
|
timeout=15)
|
||||||
self.logger.info(response.status_code)
|
self.logger.info(response.status_code)
|
||||||
@@ -100,6 +100,7 @@ class Sender:
|
|||||||
if response.status_code == 403:
|
if response.status_code == 403:
|
||||||
# try to load seesion
|
# try to load seesion
|
||||||
# session = HTMLSession()
|
# session = HTMLSession()
|
||||||
|
self.logger.info("blocked by hermes, mail:{}".format(contact.mail))
|
||||||
return RequestResult.BLOCKED
|
return RequestResult.BLOCKED
|
||||||
return RequestResult.BLOCKED
|
return RequestResult.BLOCKED
|
||||||
except Exception as Error:
|
except Exception as Error:
|
||||||
|
|||||||
Reference in New Issue
Block a user