I fixed the email auto check and added the comments
This commit is contained in:
parent
af806c4f16
commit
02afe8bace
|
@ -0,0 +1,38 @@
|
|||
# Libraries for open and use Firefox
|
||||
from selenium import webdriver
|
||||
from selenium.webdriver.common.by import By
|
||||
from selenium.webdriver.firefox.options import Options
|
||||
from selenium.webdriver.support import expected_conditions as EC
|
||||
from selenium.webdriver.support.ui import WebDriverWait
|
||||
|
||||
import pymongo
|
||||
from pymongo import MongoClient
|
||||
import urllib.parse
|
||||
import os
|
||||
|
||||
USERNAME = ""
|
||||
PASSWORD = ""
|
||||
options = Options()
|
||||
options.add_argument("--headless")
|
||||
options.add_argument('--disable-gpu')
|
||||
options.add_argument('--disable-software-rasterizer')
|
||||
|
||||
driver = webdriver.Firefox(options=options)
|
||||
driver.get("https://nuvola.madisoft.it/login") # Open Nuvola website
|
||||
|
||||
# Click on the username field and insert the username, then click on the password field and insert the password.
|
||||
username = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "username")))
|
||||
password = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "password")))
|
||||
# Insert username and password
|
||||
username.click()
|
||||
username.clear()
|
||||
username.send_keys(USERNAME)
|
||||
password.click()
|
||||
password.clear()
|
||||
password.send_keys(PASSWORD)
|
||||
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div[2]/div/div[2]/form/button"))).click() # Click on login button
|
||||
# Section Vote School
|
||||
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(
|
||||
(By.XPATH, "/html/body/div/div/div[1]/nav/div/div/a[6]"))).click() # Click on the Compiti button
|
||||
|
||||
print(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div/div/main/div/p"))).text)
|
|
@ -1,31 +1,46 @@
|
|||
import os
|
||||
from imbox import Imbox
|
||||
from dotenv import load_dotenv
|
||||
import traceback
|
||||
import time
|
||||
|
||||
# Load .env file
|
||||
load_dotenv()
|
||||
|
||||
host = os.getenv('SMTP_SERVER')
|
||||
username = os.getenv('EMAIL')
|
||||
password = os.getenv('PWD_EMAIL')
|
||||
download_folder = os.getenv('DOWNLOAD_FOLDER')
|
||||
EMAIL_SCHOOL = os.getenv('EMAIL_SCHOOL')
|
||||
HOST = os.getenv('IMAP_SERVER') #IMAP server
|
||||
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
|
||||
|
||||
if not os.path.isdir(download_folder):
|
||||
os.makedirs(download_folder, exist_ok=True)
|
||||
def recheck_email(): # Every 10 seconds check if there is a new email
|
||||
time.sleep(10)
|
||||
check_email()
|
||||
|
||||
mail = Imbox(host, username=username, password=password, ssl=True, ssl_context=None, starttls=False)
|
||||
messages = mail.messages(sent_from=EMAIL_SCHOOL)
|
||||
def check_email():
|
||||
try:
|
||||
if not os.path.isdir(DOWNLOAD_FOLDER):
|
||||
os.makedirs(DOWNLOAD_FOLDER, exist_ok=True)
|
||||
|
||||
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) # optional, mark message as read
|
||||
mail.mark_seen(uid)
|
||||
|
||||
# Download attachments
|
||||
for idx, attachment in enumerate(message.attachments):
|
||||
try:
|
||||
att_fn = attachment.get('filename')
|
||||
download_path = f"{download_folder}/{att_fn}"
|
||||
print(download_path)
|
||||
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
|
||||
except:
|
||||
print(traceback.print_exc())
|
||||
|
||||
mail.logout()
|
||||
mail.logout() # Logout from email
|
||||
recheck_email()
|
||||
except:
|
||||
print(traceback.print_exc())
|
||||
recheck_email()
|
|
@ -1,10 +1,9 @@
|
|||
from bson.objectid import ObjectId
|
||||
from dotenv import load_dotenv
|
||||
import openpyxl as xl
|
||||
import urllib
|
||||
import pymongo
|
||||
import os
|
||||
import datetime
|
||||
from bson.objectid import ObjectId
|
||||
import pymongo
|
||||
import urllib
|
||||
import os
|
||||
|
||||
load_dotenv() #Load .env file
|
||||
|
@ -13,16 +12,17 @@ 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["school-time-table"]
|
||||
collection_archive = database["archive-school-time-table"]
|
||||
collection = database["school-time-table"] #Collection school time table current
|
||||
collection_archive = database["archive-school-time-table"] #Collection school time table archive
|
||||
|
||||
x = collection.delete_many({})
|
||||
|
||||
#using read_excel() method to read our excel file and storing the same in the variable named "df "
|
||||
workbook = xl.load_workbook(filename="school_time.xlsx")
|
||||
x = collection.delete_many({}) #Delete all documents in collection (school-time-table)
|
||||
|
||||
# Load excel file
|
||||
namefile_xlsx = "attachments/school_time.xlsx"
|
||||
workbook = xl.load_workbook(filename=namefile_xlsx)
|
||||
ws = workbook.active
|
||||
|
||||
#Date
|
||||
current_time = datetime.datetime.now()
|
||||
day = str(current_time.day)
|
||||
month = str(current_time.month)
|
||||
|
@ -30,14 +30,17 @@ year = str(current_time.year)
|
|||
hour = str(current_time.hour)
|
||||
minute = str(current_time.minute)
|
||||
long_date = day + "-" + month + "-" + year + " " + hour + ":" + minute
|
||||
|
||||
#Create collection
|
||||
mydict = {
|
||||
"Date": long_date,
|
||||
"School Subject": [],
|
||||
"Teacher": [],
|
||||
}
|
||||
x = collection.insert_one(mydict)
|
||||
x = collection_archive.insert_one(mydict)
|
||||
x = collection.insert_one(mydict) # Add collection on collection (school-time-table)
|
||||
x = collection_archive.insert_one(mydict) # Add collection on collection (archive-school-time-table)
|
||||
|
||||
#Search my class in excel file and add in MongoDB
|
||||
for row in range (1, 100):
|
||||
# column B ~ column F
|
||||
for column in range (1, 100):
|
||||
|
@ -46,33 +49,33 @@ for row in range (1, 100):
|
|||
ws.cell(row=cell.row, column=column).value
|
||||
#Search school time table
|
||||
for i in range(4,80):
|
||||
school_subject = ws.cell(row=i, column=column).value
|
||||
if school_subject == 0:
|
||||
find_document_username = list(collection.find({}, {"Date": long_date}))
|
||||
school_subject = ws.cell(row=i, column=column).value # Get school subject from excel file
|
||||
if school_subject == 0: #If school subject is 0, add "null" in MongoDB
|
||||
find_document_username = list(collection.find({}, {"Date": long_date})) #Find document in MongoDB
|
||||
array_username = find_document_username[0]["_id"]
|
||||
collection.update_one(
|
||||
collection.update_one( # Add "null" in MongoDB beacause school subject is 0
|
||||
{ "_id": ObjectId(array_username)},
|
||||
{
|
||||
"$push": { "School Subject": "null" }
|
||||
}
|
||||
)
|
||||
collection_archive.update_one(
|
||||
collection_archive.update_one( # Add "null" in MongoDB beacause school subject is 0
|
||||
{ "_id": ObjectId(array_username)},
|
||||
{
|
||||
"$push": { "School Subject": "null" }
|
||||
}
|
||||
)
|
||||
else:
|
||||
else: #If school subject is not 0, add school subject in MongoDB
|
||||
#remove_things_in_front = school_subject.split(' ', 1)[1]
|
||||
find_document_username = list(collection.find({}, {"Date": long_date}))
|
||||
find_document_username = list(collection.find({}, {"Date": long_date})) #Find document in MongoDB
|
||||
array_username = find_document_username[0]["_id"]
|
||||
collection.update_one(
|
||||
collection.update_one( # Add school subject in MongoDB beacause school subject is not 0
|
||||
{ "_id": ObjectId(array_username)},
|
||||
{
|
||||
"$push": { "School Subject": school_subject }
|
||||
}
|
||||
)
|
||||
collection_archive.update_one(
|
||||
collection_archive.update_one( # Add school subject in MongoDB beacause school subject is not 0
|
||||
{ "_id": ObjectId(array_username)},
|
||||
{
|
||||
"$push": { "School Subject": school_subject }
|
||||
|
@ -83,35 +86,35 @@ for row in range (1, 100):
|
|||
for i in range(4, 80):
|
||||
teacher = ws.cell(row=i, column=column+1).value
|
||||
column = column
|
||||
if teacher == 0:
|
||||
find_document_username = list(collection.find({}, {"Date": long_date}))
|
||||
if teacher == 0: #If teacher is 0, add "null" in MongoDB
|
||||
find_document_username = list(collection.find({}, {"Date": long_date})) #Find document in MongoDB
|
||||
array_username = find_document_username[0]["_id"]
|
||||
collection.update_one(
|
||||
collection.update_one( # Add "null" in MongoDB beacause teacher is 0
|
||||
{ "_id": ObjectId(array_username)},
|
||||
{
|
||||
"$push": { "Teacher": "null" }
|
||||
}
|
||||
)
|
||||
collection_archive.update_one(
|
||||
collection_archive.update_one( # Add "null" in MongoDB beacause teacher is 0
|
||||
{ "_id": ObjectId(array_username)},
|
||||
{
|
||||
"$push": { "Teacher": teacher }
|
||||
}
|
||||
)
|
||||
else:
|
||||
else: #If teacher is not 0, add teacher in MongoDB
|
||||
find_document_username = list(collection.find({}, {"Date": long_date}))
|
||||
array_username = find_document_username[0]["_id"]
|
||||
collection.update_one(
|
||||
collection.update_one( # Add teacher in MongoDB beacause teacher is not 0
|
||||
{ "_id": ObjectId(array_username)},
|
||||
{
|
||||
"$push": { "Teacher": teacher }
|
||||
}
|
||||
)
|
||||
collection_archive.update_one(
|
||||
collection_archive.update_one( # Add teacher in MongoDB beacause teacher is not 0
|
||||
{ "_id": ObjectId(array_username)},
|
||||
{
|
||||
"$push": { "Teacher": teacher }
|
||||
}
|
||||
)
|
||||
|
||||
os.remove("school_time.xlsx")
|
||||
os.remove(namefile_xlsx) #Delete excel file
|
Loading…
Reference in New Issue