Fix email check

This commit is contained in:
Stefano Assenzo 2023-01-22 13:59:47 +00:00 committed by GitHub
parent a945d9e72b
commit bb8a5d2dbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 10 deletions

View File

@ -1,4 +1,3 @@
from update_time_school import update_time_school
from dotenv import load_dotenv from dotenv import load_dotenv
from imbox import Imbox from imbox import Imbox
import urllib.parse import urllib.parse
@ -34,7 +33,6 @@ def check_email():
mail = Imbox(HOST, username=USERNAME, password=PASSWORD, ssl=True, ssl_context=None, starttls=False) # Connect to IMAP Server mail = Imbox(HOST, username=USERNAME, password=PASSWORD, ssl=True, ssl_context=None, starttls=False) # Connect to IMAP Server
messages = mail.messages(sent_from=EMAIL_SCHOOL, unread=True) # Get unread emails from school messages = mail.messages(sent_from=EMAIL_SCHOOL, unread=True) # Get unread emails from school
for (uid, message) in messages: for (uid, message) in messages:
mail.mark_seen(uid) mail.mark_seen(uid)
@ -42,6 +40,7 @@ def check_email():
for idx, attachment in enumerate(message.attachments): for idx, attachment in enumerate(message.attachments):
try: try:
att_fn = attachment.get('filename') # Get attachment filename att_fn = attachment.get('filename') # Get attachment filename
print(att_fn)
download_path = f"{DOWNLOAD_FOLDER}/{att_fn}" download_path = f"{DOWNLOAD_FOLDER}/{att_fn}"
extension_file = pathlib.Path(att_fn).suffix # Get extension file extension_file = pathlib.Path(att_fn).suffix # Get extension file
print(extension_file) print(extension_file)
@ -50,13 +49,14 @@ def check_email():
else: else:
if collection.find_one({"filename": att_fn}): if collection.find_one({"filename": att_fn}):
print("File already exists") print("File already exists")
recheck_email()
else: else:
with open(download_path, "wb") as fp: with open(download_path, "wb") as fp:
fp.write(attachment.get('content').read()) fp.write(attachment.get('content').read())
os.rename(download_path, f"{DOWNLOAD_FOLDER}/school_time.xlsx") # Rename file os.rename(download_path, f"{DOWNLOAD_FOLDER}/school_time.xlsx") # Rename file
collection.delete_many({}) # Delete old file collection.delete_many({}) # Delete old file
collection.insert_one({"filename": att_fn}) # Insert filename to MongoDB collection.insert_one({"filename": att_fn}) # Insert filename to MongoDB
update_time_school() send_xlsx()
except: except:
print(traceback.print_exc()) print(traceback.print_exc())
@ -66,4 +66,10 @@ def check_email():
print(traceback.print_exc()) print(traceback.print_exc())
recheck_email() recheck_email()
def send_xlsx():
from update_time_school import update_time_school
update_time_school()
os.remove(f"{DOWNLOAD_FOLDER}/school_time.xlsx") # Delete file
recheck_email()
check_email() check_email()

View File

@ -17,12 +17,7 @@ database = client["website-class"] #Database name
collection = database["school-time-table"] #Collection school time table current collection = database["school-time-table"] #Collection school time table current
collection_archive = database["archive-school-time-table"] #Collection school time table archive collection_archive = database["archive-school-time-table"] #Collection school time table archive
number = 1 def update_time_school():
current_day = None
day_counter = 0
number_day = 0
def update_time_school(number, day_counter, number_day):
# Load excel file # Load excel file
namefile_xlsx = "attachments/school_time.xlsx" namefile_xlsx = "attachments/school_time.xlsx"
workbook = xl.load_workbook(filename=namefile_xlsx) workbook = xl.load_workbook(filename=namefile_xlsx)
@ -258,6 +253,10 @@ def update_time_school(number, day_counter, number_day):
dont_repeat_teacher = 0 dont_repeat_teacher = 0
gagaga_teacher = 0 gagaga_teacher = 0
number_teacher = 1 number_teacher = 1
number = 1
current_day = None
day_counter = 0
number_day = 0
#Search my class in excel file and add in MongoDB #Search my class in excel file and add in MongoDB
number = 1 number = 1
gagaga = 0 gagaga = 0
@ -452,4 +451,4 @@ def update_time_school(number, day_counter, number_day):
if gagaga_teacher == 8: if gagaga_teacher == 8:
gagaga_teacher = 0 gagaga_teacher = 0
update_time_school(number, day_counter, number_day) update_time_school()