Fixed the .env and started Nuvola web-scraping to take homework

This commit is contained in:
Stefano Assenzo 2023-01-07 11:38:10 +00:00
parent 607d371534
commit 7c30cf9b8f
2 changed files with 115 additions and 27 deletions

View File

@ -1,8 +1,15 @@
# Databse (MongoDB)
PASSWORD_MONGODB = ""
URL_MONGODB = ""
PWD_EMAIL = ""
EMAIL = "
SMTP_SERVER = ""
# Email
IMAP_SERVER = ""
SMTP_PORT = ""
EMAIL = ""
PWD_EMAIL = ""
EMAIL_SCHOOL = ""
DOWNLOAD_FOLDER = ""
# Nuvola
USERNAME_NUVOLA = ""
PASSWORD_NUVOLA = ""

View File

@ -5,34 +5,115 @@ 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
# Libraries for MongoDB and .env file
from pymongo import MongoClient
from dotenv import load_dotenv
import urllib.parse
import pymongo
import time
import os
USERNAME = ""
PASSWORD = ""
options = Options()
options.add_argument("--headless")
options.add_argument('--disable-gpu')
options.add_argument('--disable-software-rasterizer')
#Load .env file
load_dotenv()
USERNAME = os.getenv('USERNAME_NUVOLA') #Username for Nuvola
PASSWORD = os.getenv('PASSWORD_NUVOLA') #Password for Nuvola
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["school-time-table"] #Collection school time table current
driver = webdriver.Firefox(options=options)
# Settings for Firefox
options = Options()
options.add_argument("--headless") # Headless mode (so you don't see the browser)
options.add_argument('--disable-gpu') # Disable GPU
driver = webdriver.Firefox(options=options) # Open Firefox and set 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
def recheck():
time.sleep(10)
check_homework()
print(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div/div/main/div/p"))).text)
def connect_to_nuvola():
try:
username = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "username"))) # Wait for the username input
password = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "password"))) # Wait for the password input
username.click()
username.clear()
username.send_keys(USERNAME) # Insert username
password.click()
password.clear()
password.send_keys(PASSWORD) # Insert password
WebDriverWait(driver, 50).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 Homework
WebDriverWait(driver, 100).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div/div/div[1]/nav/div/div/a[6]"))).click() # Click on homework button
except:
print("Error in login")
def check_homework():
next_homework = 0
if next_homework == 10:
print("ao basta")
else:
date = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div/div/main/div/div/div[1]/div[1]/div[1]/button[1]"))) # Print the date
print(date.text())
try:
homework_1 = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div/div/main/div/div[2]/div/ul/li[1]/div/ul/li/p")))
print(homework_1.text())
try:
homework_2 = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div/div/main/div/div[2]/div/ul/li[2]/div/ul/li/p")))
print(homework_2.text())
try:
homework_3 = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div/div/main/div/div[2]/div/ul/li[3]/div/ul/li/p")))
print(homework_3.text())
try:
homework_4 = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div/div/main/div/div[2]/div/ul/li[4]/div/ul/li/p")))
print(homework_4.text())
try:
homework_5 = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div/div/main/div/div[2]/div/ul/li[5]/div/ul/li/p")))
print(homework_5.text())
except:
print("Homework 5 not found")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div/div/main/div/div/div[1]/div[1]/div[1]/button[3]/svg"))).click() # Click on next day button
next_homework += 1
check_homework()
except:
print("Homework 4 not found")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div/div/main/div/div/div[1]/div[1]/div[1]/button[3]/svg"))).click() # Click on next day button
next_homework += 1
check_homework()
except:
print("Homework 3 not found")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div/div/main/div/div/div[1]/div[1]/div[1]/button[3]/svg"))).click() # Click on next day button
next_homework += 1
check_homework()
except:
print("Homework 2 not found")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div/div/main/div/div/div[1]/div[1]/div[1]/button[3]/svg"))).click() # Click on next day button
next_homework += 1
check_homework()
except:
print("No homework")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div/div/main/div/div/div[1]/div[1]/div[1]/button[3]/svg"))).click() # Click on next day button
next_homework += 1
check_homework()
#Date
# /html/body/div/div/main/div/div/div[1]/div[1]/div[1]/button[1]
# /html/body/div/div/main/div/div[1]/div[1]/div[1]/div[1]/button[1]
#Button for next day
# /html/body/div/div/main/div/div/div[1]/div[1]/div[1]/button[3]/svg
# /html/body/div/div/main/div/div/div[1]/div[1]/div[1]/button[3]/svg
#Compiti
# /html/body/div/div/main/div/div[2]/div/ul/li[1]/div/ul/li/p
# /html/body/div/div/main/div/div[2]/div/ul/li[2]/div/ul/li/p
# /html/body/div/div/main/div/div[2]/div/ul/li[1]/div/ul/li/p
#Se non ci sono compiti
# /html/body/div/div/main/div/p