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 imbox import Imbox
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
messages = mail.messages(sent_from=EMAIL_SCHOOL, unread=True) # Get unread emails from school
for (uid, message) in messages:
mail.mark_seen(uid)
@ -42,6 +40,7 @@ def check_email():
for idx, attachment in enumerate(message.attachments):
try:
att_fn = attachment.get('filename') # Get attachment filename
print(att_fn)
download_path = f"{DOWNLOAD_FOLDER}/{att_fn}"
extension_file = pathlib.Path(att_fn).suffix # Get extension file
print(extension_file)
@ -50,13 +49,14 @@ def check_email():
else:
if collection.find_one({"filename": att_fn}):
print("File already exists")
recheck_email()
else:
with open(download_path, "wb") as fp:
fp.write(attachment.get('content').read())
os.rename(download_path, f"{DOWNLOAD_FOLDER}/school_time.xlsx") # Rename file
collection.delete_many({}) # Delete old file
collection.insert_one({"filename": att_fn}) # Insert filename to MongoDB
update_time_school()
send_xlsx()
except:
print(traceback.print_exc())
@ -66,4 +66,10 @@ def check_email():
print(traceback.print_exc())
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()

View File

@ -17,12 +17,7 @@ database = client["website-class"] #Database name
collection = database["school-time-table"] #Collection school time table current
collection_archive = database["archive-school-time-table"] #Collection school time table archive
number = 1
current_day = None
day_counter = 0
number_day = 0
def update_time_school(number, day_counter, number_day):
def update_time_school():
# Load excel file
namefile_xlsx = "attachments/school_time.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
gagaga_teacher = 0
number_teacher = 1
number = 1
current_day = None
day_counter = 0
number_day = 0
#Search my class in excel file and add in MongoDB
number = 1
gagaga = 0
@ -452,4 +451,4 @@ def update_time_school(number, day_counter, number_day):
if gagaga_teacher == 8:
gagaga_teacher = 0
update_time_school(number, day_counter, number_day)
update_time_school()