add mail_scheduler
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
from apscheduler.executors.pool import ThreadPoolExecutor, ProcessPoolExecutor
|
||||
from apscheduler.schedulers.blocking import BlockingScheduler
|
||||
|
||||
from src.mail.mail_reader import read_mails
|
||||
|
||||
|
||||
def start_check_mail_job(sched):
|
||||
# sched.add_job(start_book_appointment, 'cron', day_of_week='mon-sat', hour='10',
|
||||
# minute='30',
|
||||
# misfire_grace_time=10,
|
||||
# second='0', timezone='Europe/Paris', max_instances=1)
|
||||
sched.add_job(read_mails, 'cron', day_of_week='mon-sat', hour='11',
|
||||
minute='30',
|
||||
misfire_grace_time=10,
|
||||
second='0', timezone='Europe/Paris', max_instances=1)
|
||||
sched.add_job(read_mails, 'cron', day_of_week='mon-sat', hour='12',
|
||||
minute='30',
|
||||
misfire_grace_time=10,
|
||||
second='0', timezone='Europe/Paris', max_instances=1)
|
||||
sched.add_job(read_mails, 'cron', day_of_week='mon-sat', hour='14',
|
||||
minute='30',
|
||||
misfire_grace_time=10,
|
||||
second='0', timezone='Europe/Paris', max_instances=1)
|
||||
sched.add_job(read_mails, 'cron', day_of_week='mon-sat', hour='17',
|
||||
minute='30',
|
||||
misfire_grace_time=10,
|
||||
second='0', timezone='Europe/Paris', max_instances=1)
|
||||
|
||||
|
||||
def config_and_start_jobs():
|
||||
executors = {
|
||||
'default': ThreadPoolExecutor(30),
|
||||
'processpool': ProcessPoolExecutor(12)
|
||||
}
|
||||
sched = BlockingScheduler(executors=executors)
|
||||
# start_waiting_sms_job(sched)
|
||||
start_check_mail_job(sched)
|
||||
sched.print_jobs()
|
||||
sched.start()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
config_and_start_jobs()
|
||||
+14
-3
@@ -1,3 +1,4 @@
|
||||
import datetime
|
||||
import email
|
||||
import imaplib
|
||||
import re
|
||||
@@ -14,10 +15,14 @@ from src.proxy.proxy_type import ProxyType
|
||||
from src.workers.link_validator import LinkValidator
|
||||
|
||||
AOL_IMAP_SERVER = "imap.aol.com"
|
||||
IMAP_SERVER_163 = "imap.163.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"
|
||||
|
||||
DOMAIN_163 = "163.com"
|
||||
date_format = "%d-%b-%Y" # DD-Mon-YYYY e.g., 3-Mar-2014
|
||||
|
||||
|
||||
class MailReader():
|
||||
def __init__(self, login, password):
|
||||
@@ -30,13 +35,17 @@ class MailReader():
|
||||
l = i.decode().split(' "/" ')
|
||||
print(l[0] + " = " + l[1])
|
||||
|
||||
def read_emails(self, email_number=0) -> list:
|
||||
def read_emails(self) -> list:
|
||||
# create an IMAP4 class with SSL
|
||||
if DOMAIN_163 in self.login:
|
||||
imap = imaplib.IMAP4_SSL(IMAP_SERVER_163)
|
||||
else:
|
||||
imap = imaplib.IMAP4_SSL(AOL_IMAP_SERVER)
|
||||
# authenticate
|
||||
imap.login(self.login, self.password)
|
||||
mail_list = []
|
||||
self.show_folders(imap)
|
||||
print("read mails from {}".format(self.login))
|
||||
# self.show_folders(imap)
|
||||
# total number of emails
|
||||
# get mails from inbox
|
||||
# (\Archive \HasNoChildren) = "Archive"
|
||||
@@ -55,7 +64,9 @@ class MailReader():
|
||||
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))
|
||||
typ, data = imap.search(None, '(SUBJECT "{}" SINCE "{}")'.format(VALIDATION_URL_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)")
|
||||
|
||||
Reference in New Issue
Block a user