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
|
import os
|
||||||
from imbox import Imbox
|
from imbox import Imbox
|
||||||
|
from dotenv import load_dotenv
|
||||||
import traceback
|
import traceback
|
||||||
|
import time
|
||||||
|
|
||||||
|
# Load .env file
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
host = os.getenv('SMTP_SERVER')
|
HOST = os.getenv('IMAP_SERVER') #IMAP server
|
||||||
username = os.getenv('EMAIL')
|
USERNAME = os.getenv('EMAIL') #Username (ex . test@example.com)
|
||||||
password = os.getenv('PWD_EMAIL')
|
PASSWORD = os.getenv('PWD_EMAIL') #IMAP Password
|
||||||
download_folder = os.getenv('DOWNLOAD_FOLDER')
|
DOWNLOAD_FOLDER = os.getenv('DOWNLOAD_FOLDER') #Download folder for xlsx file
|
||||||
EMAIL_SCHOOL = os.getenv('EMAIL_SCHOOL')
|
EMAIL_SCHOOL = os.getenv('EMAIL_SCHOOL') #Email school
|
||||||
|
|
||||||
if not os.path.isdir(download_folder):
|
def recheck_email(): # Every 10 seconds check if there is a new email
|
||||||
os.makedirs(download_folder, exist_ok=True)
|
time.sleep(10)
|
||||||
|
check_email()
|
||||||
|
|
||||||
mail = Imbox(host, username=username, password=password, ssl=True, ssl_context=None, starttls=False)
|
def check_email():
|
||||||
messages = mail.messages(sent_from=EMAIL_SCHOOL)
|
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:
|
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):
|
for idx, attachment in enumerate(message.attachments):
|
||||||
try:
|
try:
|
||||||
att_fn = attachment.get('filename')
|
att_fn = attachment.get('filename') # Get attachment filename
|
||||||
download_path = f"{download_folder}/{att_fn}"
|
download_path = f"{DOWNLOAD_FOLDER}/{att_fn}"
|
||||||
print(download_path)
|
|
||||||
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
|
||||||
except:
|
except:
|
||||||
print(traceback.print_exc())
|
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
|
from dotenv import load_dotenv
|
||||||
import openpyxl as xl
|
import openpyxl as xl
|
||||||
import urllib
|
|
||||||
import pymongo
|
|
||||||
import os
|
|
||||||
import datetime
|
import datetime
|
||||||
from bson.objectid import ObjectId
|
import pymongo
|
||||||
|
import urllib
|
||||||
import os
|
import os
|
||||||
|
|
||||||
load_dotenv() #Load .env file
|
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)
|
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
|
client = pymongo.MongoClient(mongo_url) #Connect to MongoDB
|
||||||
database = client["website-class"] #Database name
|
database = client["website-class"] #Database name
|
||||||
collection = database["school-time-table"]
|
collection = database["school-time-table"] #Collection school time table current
|
||||||
collection_archive = database["archive-school-time-table"]
|
collection_archive = database["archive-school-time-table"] #Collection school time table archive
|
||||||
|
|
||||||
x = collection.delete_many({})
|
x = collection.delete_many({}) #Delete all documents in collection (school-time-table)
|
||||||
|
|
||||||
#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")
|
|
||||||
|
|
||||||
|
# Load excel file
|
||||||
|
namefile_xlsx = "attachments/school_time.xlsx"
|
||||||
|
workbook = xl.load_workbook(filename=namefile_xlsx)
|
||||||
ws = workbook.active
|
ws = workbook.active
|
||||||
|
|
||||||
|
#Date
|
||||||
current_time = datetime.datetime.now()
|
current_time = datetime.datetime.now()
|
||||||
day = str(current_time.day)
|
day = str(current_time.day)
|
||||||
month = str(current_time.month)
|
month = str(current_time.month)
|
||||||
|
@ -30,14 +30,17 @@ year = str(current_time.year)
|
||||||
hour = str(current_time.hour)
|
hour = str(current_time.hour)
|
||||||
minute = str(current_time.minute)
|
minute = str(current_time.minute)
|
||||||
long_date = day + "-" + month + "-" + year + " " + hour + ":" + minute
|
long_date = day + "-" + month + "-" + year + " " + hour + ":" + minute
|
||||||
|
|
||||||
|
#Create collection
|
||||||
mydict = {
|
mydict = {
|
||||||
"Date": long_date,
|
"Date": long_date,
|
||||||
"School Subject": [],
|
"School Subject": [],
|
||||||
"Teacher": [],
|
"Teacher": [],
|
||||||
}
|
}
|
||||||
x = collection.insert_one(mydict)
|
x = collection.insert_one(mydict) # Add collection on collection (school-time-table)
|
||||||
x = collection_archive.insert_one(mydict)
|
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):
|
for row in range (1, 100):
|
||||||
# column B ~ column F
|
# column B ~ column F
|
||||||
for column in range (1, 100):
|
for column in range (1, 100):
|
||||||
|
@ -46,33 +49,33 @@ for row in range (1, 100):
|
||||||
ws.cell(row=cell.row, column=column).value
|
ws.cell(row=cell.row, column=column).value
|
||||||
#Search school time table
|
#Search school time table
|
||||||
for i in range(4,80):
|
for i in range(4,80):
|
||||||
school_subject = ws.cell(row=i, column=column).value
|
school_subject = ws.cell(row=i, column=column).value # Get school subject from excel file
|
||||||
if school_subject == 0:
|
if school_subject == 0: #If school subject is 0, add "null" in MongoDB
|
||||||
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"]
|
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)},
|
{ "_id": ObjectId(array_username)},
|
||||||
{
|
{
|
||||||
"$push": { "School Subject": "null" }
|
"$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)},
|
{ "_id": ObjectId(array_username)},
|
||||||
{
|
{
|
||||||
"$push": { "School Subject": "null" }
|
"$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]
|
#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"]
|
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)},
|
{ "_id": ObjectId(array_username)},
|
||||||
{
|
{
|
||||||
"$push": { "School Subject": school_subject }
|
"$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)},
|
{ "_id": ObjectId(array_username)},
|
||||||
{
|
{
|
||||||
"$push": { "School Subject": school_subject }
|
"$push": { "School Subject": school_subject }
|
||||||
|
@ -83,35 +86,35 @@ for row in range (1, 100):
|
||||||
for i in range(4, 80):
|
for i in range(4, 80):
|
||||||
teacher = ws.cell(row=i, column=column+1).value
|
teacher = ws.cell(row=i, column=column+1).value
|
||||||
column = column
|
column = column
|
||||||
if teacher == 0:
|
if teacher == 0: #If teacher is 0, add "null" in MongoDB
|
||||||
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"]
|
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)},
|
{ "_id": ObjectId(array_username)},
|
||||||
{
|
{
|
||||||
"$push": { "Teacher": "null" }
|
"$push": { "Teacher": "null" }
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
collection_archive.update_one(
|
collection_archive.update_one( # Add "null" in MongoDB beacause teacher is 0
|
||||||
{ "_id": ObjectId(array_username)},
|
{ "_id": ObjectId(array_username)},
|
||||||
{
|
{
|
||||||
"$push": { "Teacher": teacher }
|
"$push": { "Teacher": teacher }
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
else:
|
else: #If teacher is not 0, add teacher in MongoDB
|
||||||
find_document_username = list(collection.find({}, {"Date": long_date}))
|
find_document_username = list(collection.find({}, {"Date": long_date}))
|
||||||
array_username = find_document_username[0]["_id"]
|
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)},
|
{ "_id": ObjectId(array_username)},
|
||||||
{
|
{
|
||||||
"$push": { "Teacher": teacher }
|
"$push": { "Teacher": teacher }
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
collection_archive.update_one(
|
collection_archive.update_one( # Add teacher in MongoDB beacause teacher is not 0
|
||||||
{ "_id": ObjectId(array_username)},
|
{ "_id": ObjectId(array_username)},
|
||||||
{
|
{
|
||||||
"$push": { "Teacher": teacher }
|
"$push": { "Teacher": teacher }
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
os.remove("school_time.xlsx")
|
os.remove(namefile_xlsx) #Delete excel file
|
Loading…
Reference in New Issue