Now loads teachers and school subjects correctly - I rearranged the JSON in preparation for the Discord bot.

This commit is contained in:
Stefano Assenzo 2023-01-22 13:35:13 +00:00 committed by GitHub
parent 8b65584ac3
commit bf1f7bbf9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 368 additions and 76 deletions

2
.gitignore vendored
View File

@ -3,3 +3,5 @@ test.xlsx
geckodriver.log geckodriver.log
all.log all.log
__pycache__ __pycache__
test.py
school_time.xlsx

View File

@ -1,5 +1,4 @@
from update_time_school import update_time_school from update_time_school import update_time_school
from dotenv import load_dotenv from dotenv import load_dotenv
from imbox import Imbox from imbox import Imbox
import urllib.parse import urllib.parse
@ -57,7 +56,7 @@ def check_email():
os.rename(download_path, f"{DOWNLOAD_FOLDER}/school_time.xlsx") # Rename file os.rename(download_path, f"{DOWNLOAD_FOLDER}/school_time.xlsx") # Rename file
collection.delete_many({}) # Delete old file collection.delete_many({}) # Delete old file
collection.insert_one({"filename": att_fn}) # Insert filename to MongoDB collection.insert_one({"filename": att_fn}) # Insert filename to MongoDB
update_time_school() # Update school time table update_time_school()
except: except:
print(traceback.print_exc()) print(traceback.print_exc())
@ -66,3 +65,5 @@ def check_email():
except: except:
print(traceback.print_exc()) print(traceback.print_exc())
recheck_email() recheck_email()
check_email()

View File

@ -1,10 +1,11 @@
from bson.objectid import ObjectId from bson.objectid import ObjectId
from openpyxl.styles import NamedStyle
from dotenv import load_dotenv from dotenv import load_dotenv
import openpyxl as xl import openpyxl as xl
import datetime import datetime
import pymongo import pymongo
import urllib import urllib
import time import datetime
import os import os
load_dotenv() #Load .env file load_dotenv() #Load .env file
@ -14,12 +15,18 @@ mongo_url = "mongodb+srv://elci:" + urllib.parse.quote_plus(PASSWORD_MONGODB) +
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 school time table current collection = database["school-time-table"] #Collection school time table current
collection_archive = database["archive-school-time-table"] #Collection school time table archive
def update_time_school(): number = 1
current_day = None
day_counter = 0
number_day = 0
def update_time_school(number, day_counter, number_day):
# Load excel file # Load excel file
namefile_xlsx = "attachments/school_time.xlsx" namefile_xlsx = "attachments/school_time.xlsx"
workbook = xl.load_workbook(filename=namefile_xlsx) workbook = xl.load_workbook(filename=namefile_xlsx)
date_style = NamedStyle(name='date_style',number_format='dd/mm/yy')
workbook.add_named_style(date_style)
ws = workbook.active ws = workbook.active
#Date #Date
@ -34,88 +41,370 @@ def update_time_school():
#Create collection #Create collection
mydict = { mydict = {
"Date": long_date, "Date": long_date,
"School Subject": [], "School Subject": {
"Teacher": [], "Monday": [
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
],
"Tuesday": [
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
],
"Wednesday": [
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
],
"Thursday": [
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
],
"Friday": [
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
],
"Saturday": [
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
{
"Subject": "null",
"Teacher": "null",
},
],
} }
}
x = collection.delete_many({}) #Delete all documents in collection (school-time-table) x = collection.delete_many({}) #Delete all documents in collection (school-time-table)
x = collection.insert_one(mydict) # Add collection on collection (school-time-table) 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) check_repeat = 0
check_repeat_teacher = 0
dont_repeat = 0
dont_repeat_teacher = 0
gagaga_teacher = 0
number_teacher = 1
#Search my class in excel file and add in MongoDB #Search my class in excel file and add in MongoDB
number = 1
gagaga = 0
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):
cell = ws.cell(row, column) cell = ws.cell(row, column)
if cell.value == "2elci": if cell.value == "2elci":
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):
day = str(ws.cell(row=i, column=3).value) # Get day from excel file
school_subject = ws.cell(row=i, column=column).value # Get school subject from excel file 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( # Add "null" in MongoDB beacause school subject is 0
{ "_id": ObjectId(array_username)},
{
"$push": { "School Subject": "null" }
}
)
collection_archive.update_one( # Add "null" in MongoDB beacause school subject is 0
{ "_id": ObjectId(array_username)},
{
"$push": { "School Subject": "null" }
}
)
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 in MongoDB
array_username = find_document_username[0]["_id"]
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( # Add school subject in MongoDB beacause school subject is not 0
{ "_id": ObjectId(array_username)},
{
"$push": { "School Subject": school_subject }
}
)
#Search teacher
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 if dont_repeat == 9:
if teacher == 0: #If teacher is 0, add "null" in MongoDB check_repeat += 1 # 13
find_document_username = list(collection.find({}, {"Date": long_date})) #Find document in MongoDB if check_repeat == 5:
array_username = find_document_username[0]["_id"] check_repeat = 0
collection.update_one( # Add "null" in MongoDB beacause teacher is 0 dont_repeat = 0
{ "_id": ObjectId(array_username)}, #number = 1
{ else:
"$push": { "Teacher": "null" } if day == "None":
if school_subject == 0: #If school subject is 0, add "null" in MongoDB
number += 1
gagaga += 1
dont_repeat += 1
if number == 9:
number = 1
if gagaga == 9:
gagaga = 0
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 in MongoDB
array_username = find_document_username[0]["_id"]
# Add school subject in MongoDB beacause school subject is not 0
collection.update_one(
{ "_id": ObjectId(array_username)},
{ "$set": {
"School Subject." + array_test[0] + "." + str(gagaga) + ".Subject": school_subject,
}
} }
) )
collection_archive.update_one( # Add "null" in MongoDB beacause teacher is 0 number += 1
{ "_id": ObjectId(array_username)}, gagaga += 1
{ dont_repeat += 1
"$push": { "Teacher": teacher } if number == 9:
} number = 1
) if gagaga == 8:
else: #If teacher is not 0, add teacher in MongoDB gagaga = 0
find_document_username = list(collection.find({}, {"Date": long_date})) else:
array_username = find_document_username[0]["_id"] datetime_obj = datetime.datetime.strptime(day, "%Y-%m-%d %H:%M:%S").strftime("%d %m %Y")
collection.update_one( # Add teacher in MongoDB beacause teacher is not 0 convert_date_to_day = datetime.datetime.strptime(datetime_obj, '%d %m %Y').strftime('%A')
{ "_id": ObjectId(array_username)}, array_test = []
{ #print(test)
"$push": { "Teacher": teacher } array_test.append(convert_date_to_day)
} number_day += 1
) # "School Subject." + array_test[0]: school_subject,
collection_archive.update_one( # Add teacher in MongoDB beacause teacher is not 0 if school_subject == 0: #If school subject is 0, add "null" in MongoDB
{ "_id": ObjectId(array_username)}, find_document_username = list(collection.find({}, {"Date": long_date})) #Find document in MongoDB
{ array_username = find_document_username[0]["_id"]
"$push": { "Teacher": teacher } collection.update_one(
{ "_id": ObjectId(array_username)},
{ "$set": {
"School Subject." + array_test[0] + "." + str(gagaga) + ".Subject": school_subject,
}
} }
) )
os.remove(namefile_xlsx) #Delete excel file number += 1
gagaga += 1
dont_repeat += 1
number = 0
if number == 9:
number = 1
if gagaga == 8:
gagaga = 0
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 in MongoDB
array_username = find_document_username[0]["_id"]
# Add school subject in MongoDB beacause school subject is not 0
collection.update_one(
{ "_id": ObjectId(array_username)},
{ "$set": {
"School Subject." + array_test[0] + "." + str(gagaga)+ ".Subject": school_subject,
}
}
)
number += 1
gagaga += 1
dont_repeat += 1
if number == 9:
number = 1
if gagaga == 8:
gagaga = 0
#Search teacher
if dont_repeat_teacher == 9:
check_repeat_teacher += 1 # 13
if check_repeat_teacher == 5:
check_repeat_teacher = 0
dont_repeat_teacher = 0
#number = 1
else:
if day == "None":
if teacher == 0:
number_teacher += 1
gagaga_teacher += 1
dont_repeat_teacher += 1
if number_teacher == 9:
number_teacher = 1
if gagaga_teacher == 9:
gagaga_teacher = 0
else:
find_document_username = list(collection.find({}, {"Date": long_date}))
array_username = find_document_username[0]["_id"]
collection.update_one(
{ "_id": ObjectId(array_username)},
{ "$set": {
"School Subject." + array_test[0] + "." + str(gagaga_teacher) + ".Teacher": teacher,
}
}
)
number_teacher += 1
gagaga_teacher += 1
dont_repeat_teacher += 1
if number_teacher == 9:
number_teacher = 1
if gagaga_teacher == 8:
gagaga_teacher = 0
else:
datetime_obj = datetime.datetime.strptime(day, "%Y-%m-%d %H:%M:%S").strftime("%d %m %Y")
convert_date_to_day = datetime.datetime.strptime(datetime_obj, '%d %m %Y').strftime('%A')
array_test = []
array_test.append(convert_date_to_day)
find_document_username = list(collection.find({}, {"Date": long_date}))
array_username = find_document_username[0]["_id"]
collection.update_one(
{ "_id": ObjectId(array_username)},
{ "$set": {
"School Subject." + array_test[0] + "." + str(gagaga_teacher)+ ".Teacher": teacher,
}
}
)
number_teacher += 1
gagaga_teacher += 1
dont_repeat_teacher += 1
if number_teacher == 9:
number_teacher = 1
if gagaga_teacher == 8:
gagaga_teacher = 0
update_time_school(number, day_counter, number_day)