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

This commit is contained in:
Stefano Assenzo 2023-01-14 16:36:29 +00:00
parent 0f8a83c9c5
commit 8b65584ac3
2 changed files with 26 additions and 8 deletions

View File

@ -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())

View File

@ -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"