From 8b65584ac30fbcccf5476f561477a3f99cb9de83 Mon Sep 17 00:00:00 2001 From: Stefano Assenzo Date: Sat, 14 Jan 2023 16:36:29 +0000 Subject: [PATCH] Now it checks if the file has already been downloaded and if it is an .xlsx, and automatically starts the automatic generation of the school schedule --- src/events/school_time/email_read.py | 30 +++++++++++++++++--- src/events/school_time/update_time_school.py | 4 --- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/events/school_time/email_read.py b/src/events/school_time/email_read.py index 9c5c701..4074013 100644 --- a/src/events/school_time/email_read.py +++ b/src/events/school_time/email_read.py @@ -1,6 +1,11 @@ +from update_time_school import update_time_school + from dotenv import load_dotenv from imbox import Imbox +import urllib.parse import traceback +import pathlib +import pymongo import time import os @@ -12,9 +17,15 @@ USERNAME = os.getenv('EMAIL') #Username (ex . test@example.com) PASSWORD = os.getenv('PWD_EMAIL') #IMAP Password DOWNLOAD_FOLDER = os.getenv('DOWNLOAD_FOLDER') #Download folder for xlsx file EMAIL_SCHOOL = os.getenv('EMAIL_SCHOOL') #Email school +PASSWORD_MONGODB = os.getenv('PASSWORD_MONGODB') #Password for MongoDB +URL_MONGODB = os.getenv('URL_MONGODB') #URL for MongoDB +mongo_url = "mongodb+srv://elci:" + urllib.parse.quote_plus(PASSWORD_MONGODB) + URL_MONGODB #URL for MongoDB (with password) +client = pymongo.MongoClient(mongo_url) #Connect to MongoDB +database = client["website-class"] #Database name +collection = database["email"] #Collection school time table current def recheck_email(): # Every 10 seconds check if there is a new email - time.sleep(10) + time.sleep(600) check_email() def check_email(): @@ -33,9 +44,20 @@ def check_email(): try: att_fn = attachment.get('filename') # Get attachment filename download_path = f"{DOWNLOAD_FOLDER}/{att_fn}" - 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 + extension_file = pathlib.Path(att_fn).suffix # Get extension file + print(extension_file) + if extension_file != ".xlsx": # Check if file is xlsx + print("Check file") + else: + if collection.find_one({"filename": att_fn}): + print("File already exists") + 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() # Update school time table except: print(traceback.print_exc()) diff --git a/src/events/school_time/update_time_school.py b/src/events/school_time/update_time_school.py index 313a7d6..abc5ca1 100644 --- a/src/events/school_time/update_time_school.py +++ b/src/events/school_time/update_time_school.py @@ -16,10 +16,6 @@ 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 -def recheck(): - time.sleep(10) - update_time_school() - def update_time_school(): # Load excel file namefile_xlsx = "attachments/school_time.xlsx"