New structure JSON for DB and split date (no current test)

This commit is contained in:
Stefano Assenzo 2023-01-13 20:37:18 +00:00
parent cfb9c229f9
commit 092e3c4f84
3 changed files with 80 additions and 18 deletions

View File

@ -9,6 +9,7 @@ from selenium import webdriver
# Libraries for MongoDB and .env file # Libraries for MongoDB and .env file
from dotenv import load_dotenv from dotenv import load_dotenv
import urllib.parse import urllib.parse
import datetime
import pymongo import pymongo
import os import os
@ -32,14 +33,28 @@ def giorno_cinque():
#Giorno cinque #Giorno cinque
try: try:
date = str(WebDriverWait(driver, 250).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div/div/main/div/div/div[1]/div[1]/div[1]/button[1]"))).text) # Date date = str(WebDriverWait(driver, 250).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div/div/main/div/div/div[1]/div[1]/div[1]/button[1]"))).text) # Date
split_date = date.split() # Split date
description = str(WebDriverWait(driver, 250).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div/div/main/div/p"))).text) # Homework 1 or no homework description = str(WebDriverWait(driver, 250).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div/div/main/div/p"))).text) # Homework 1 or no homework
mydict = { mydict = {
"Date": date, "subjects": [
"School Subject": "No school subject", {
"Description": description, "name": "No school subject",
} "homework": [
{
"date": {
"day": split_date[0],
"month": split_date[1],
"year": split_date[2],
},
"description": description,
}
]
}
]
},
x = collection.insert_many(mydict) # Insert data in MongoDB
x = collection.insert_one(mydict) # Insert data in MongoDB
school_subject = str(WebDriverWait(driver, 250).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div/div/main/div/div[2]/div/ul/li/h2"))).text) # School subject 1 school_subject = str(WebDriverWait(driver, 250).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div/div/main/div/div[2]/div/ul/li/h2"))).text) # School subject 1
new_school_subject = {"$set": {"School Subject": school_subject}} # Update school subject new_school_subject = {"$set": {"School Subject": school_subject}} # Update school subject
collection.update_one(mydict, new_school_subject) # Update school subject collection.update_one(mydict, new_school_subject) # Update school subject

View File

@ -0,0 +1,58 @@
# Libraries for MongoDB and .env file
from dotenv import load_dotenv
import urllib.parse
import pymongo
import os
import datetime
#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["homework"] #Collection school time table current
mydict = {
"subjects": [
{
"name": "Stefanologia",
"homework": [
{
"date": {
"day": str(12),
"month": str(1),
"year": str(2023),
},
"description": "Niente compiti yeeee",
}
]
}
]
},
string = "11 january 2023"
a = string.split()
b = a[0]
print(b)
#x = collection.insert_many(mydict) # Insert data in MongoDB
# {
# "subjects": [
# {
# "name": "Disegno",
# "homework": [
# {
# "date": {
# "day": 12,
# "month": 1,
# "year": 2023
# },
# "description": "Per le vacanze di natale..."
# }
# ]
# }
# ]
#}

View File

@ -18,16 +18,5 @@ app = Flask(__name__)
def get_homework(): def get_homework():
homework = collection.find({}, {"_id": 0}) homework = collection.find({}, {"_id": 0})
return jsonify(list(homework)) return jsonify(list(homework))
if __name__ == '__main__': if __name__ == '__main__':
app.run() app.run()
#{
# "date": {
# "day": 12,
# "month": 1,
# "year": 2023
# },
# "description": "Per le vacanze di natale...",
# "subject": "Disegno"
#}