Compare commits

...

2 Commits

Author SHA1 Message Date
Stefano Assenzo f7ce81ef5d Add logging (work in progress...) 2023-01-07 11:44:09 +00:00
Stefano Assenzo 7c30cf9b8f Fixed the .env and started Nuvola web-scraping to take homework 2023-01-07 11:38:10 +00:00
5 changed files with 133 additions and 28 deletions

View File

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

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
test.xlsx test.xlsx
.env .env
geckodriver.log geckodriver.log
all.log

16
app.py
View File

@ -1,14 +1,26 @@
from flask import Flask, render_template, request, session, jsonify from flask import Flask, render_template, request, session, jsonify
import requests import requests
import logging
import bcrypt import bcrypt
import sys
import os import os
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[
logging.FileHandler("src/log/all.log"),
logging.StreamHandler(sys.stdout)
]
)
app = Flask(__name__) app = Flask(__name__)
IMAGE_FOLDER = os.path.join('static', 'images') IMAGE_FOLDER = os.path.join('static', 'images')
app.config['UPLOAD_FOLDER'] = IMAGE_FOLDER app.config['UPLOAD_FOLDER'] = IMAGE_FOLDER
@app.route('/') @app.route('/')
def homepage(): def homepage():
logging.info("A user went up: Homepage")
return render_template('html/index.html') return render_template('html/index.html')
#imageList = os.listdir('static/images') #imageList = os.listdir('static/images')
#imageList = ['images/' + image for image in imageList] #imageList = ['images/' + image for image in imageList]
@ -16,20 +28,24 @@ def homepage():
@app.route('/orario') @app.route('/orario')
def orario(): def orario():
logging.info("A user went up: Orario")
if 'username' in session: if 'username' in session:
return "You are logged in as " + session['username'] return "You are logged in as " + session['username']
#return render_template('html/orario.html') #return render_template('html/orario.html')
@app.route('/calendario') @app.route('/calendario')
def calendario(): def calendario():
logging.info("A user went up: Calendario")
return render_template('html/calendario.html') return render_template('html/calendario.html')
# Da sistemare # Da sistemare
@app.route('/api', methods = ['GET', 'POST']) @app.route('/api', methods = ['GET', 'POST'])
def api(): def api():
logging.info("A user went up: API")
if(request.method == 'GET'): if(request.method == 'GET'):
data = "hello world" data = "hello world"
return jsonify({'data': data}) return jsonify({'data': data})
if __name__ == '__main__': if __name__ == '__main__':
logging.info("Web server started!")
app.run() app.run()

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 import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support.ui import WebDriverWait
import pymongo # Libraries for MongoDB and .env file
from pymongo import MongoClient from pymongo import MongoClient
from dotenv import load_dotenv
import urllib.parse import urllib.parse
import pymongo
import time
import os import os
USERNAME = "" #Load .env file
PASSWORD = "" load_dotenv()
options = Options() USERNAME = os.getenv('USERNAME_NUVOLA') #Username for Nuvola
options.add_argument("--headless") PASSWORD = os.getenv('PASSWORD_NUVOLA') #Password for Nuvola
options.add_argument('--disable-gpu') PASSWORD_MONGODB = os.getenv('PASSWORD_MONGODB') #Password for MongoDB
options.add_argument('--disable-software-rasterizer') 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 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. def recheck():
username = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "username"))) time.sleep(10)
password = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "password"))) check_homework()
# 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) 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

View File