Compare commits

...

3 Commits

Author SHA1 Message Date
Stefano Assenzo bb8a5d2dbc
Fix email check 2023-01-22 13:59:47 +00:00
Stefano Assenzo a945d9e72b
Upload all document on archive collection 2023-01-22 13:44:00 +00:00
Stefano Assenzo bf1f7bbf9b
Now loads teachers and school subjects correctly - I rearranged the JSON in preparation for the Discord bot. 2023-01-22 13:35:13 +00:00
3 changed files with 413 additions and 71 deletions

2
.gitignore vendored
View File

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

View File

@ -1,5 +1,3 @@
from update_time_school import update_time_school
from dotenv import load_dotenv
from imbox import Imbox
import urllib.parse
@ -35,7 +33,6 @@ def check_email():
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:
mail.mark_seen(uid)
@ -43,6 +40,7 @@ def check_email():
for idx, attachment in enumerate(message.attachments):
try:
att_fn = attachment.get('filename') # Get attachment filename
print(att_fn)
download_path = f"{DOWNLOAD_FOLDER}/{att_fn}"
extension_file = pathlib.Path(att_fn).suffix # Get extension file
print(extension_file)
@ -51,13 +49,14 @@ def check_email():
else:
if collection.find_one({"filename": att_fn}):
print("File already exists")
recheck_email()
else:
with open(download_path, "wb") as fp:
fp.write(attachment.get('content').read())
os.rename(download_path, f"{DOWNLOAD_FOLDER}/school_time.xlsx") # Rename file
collection.delete_many({}) # Delete old file
collection.insert_one({"filename": att_fn}) # Insert filename to MongoDB
update_time_school() # Update school time table
send_xlsx()
except:
print(traceback.print_exc())
@ -66,3 +65,11 @@ def check_email():
except:
print(traceback.print_exc())
recheck_email()
def send_xlsx():
from update_time_school import update_time_school
update_time_school()
os.remove(f"{DOWNLOAD_FOLDER}/school_time.xlsx") # Delete file
recheck_email()
check_email()

View File

@ -1,10 +1,11 @@
from bson.objectid import ObjectId
from openpyxl.styles import NamedStyle
from dotenv import load_dotenv
import openpyxl as xl
import datetime
import pymongo
import urllib
import time
import datetime
import os
load_dotenv() #Load .env file
@ -20,6 +21,8 @@ def update_time_school():
# Load excel file
namefile_xlsx = "attachments/school_time.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
#Date
@ -34,88 +37,418 @@ def update_time_school():
#Create collection
mydict = {
"Date": long_date,
"School Subject": [],
"Teacher": [],
"School Subject": {
"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.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
number = 1
current_day = None
day_counter = 0
number_day = 0
#Search my class in excel file and add in MongoDB
number = 1
gagaga = 0
for row in range (1, 100):
# column B ~ column F
for column in range (1, 100):
cell = ws.cell(row, column)
if cell.value == "2elci":
ws.cell(row=cell.row, column=column).value
#Search school time table
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
teacher = ws.cell(row=i, column=column+1).value
if dont_repeat == 9:
check_repeat += 1 # 13
if check_repeat == 5:
check_repeat = 0
dont_repeat = 0
#number = 1
else:
if day == "None":
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" }
}
)
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"]
collection.update_one( # Add school subject in MongoDB beacause school subject is not 0
{ "_id": ObjectId(array_username)},
{
"$push": { "School Subject": school_subject }
find_document_school_time_table = list(collection.find({}, {"Date": long_date}))
find_document_archive_school_time_table = list(collection_archive.find({}, {"Date": long_date}))
array_document_school_time_table = find_document_school_time_table[0]["_id"]
array_document_archive_school_time_table = find_document_archive_school_time_table[0]["_id"]
# Add school subject in MongoDB beacause school subject is not 0
collection.update_one(
{ "_id": ObjectId(array_document_school_time_table)},
{ "$set": {
"School Subject." + array_test[0] + "." + str(gagaga) + ".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 }
collection_archive.update_one(
{ "_id": ObjectId(array_document_archive_school_time_table)},
{ "$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
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)
number_day += 1
if school_subject == 0: #If school subject is 0, add "null" in MongoDB
find_document_school_time_table = list(collection.find({}, {"Date": long_date}))
find_document_archive_school_time_table = list(collection_archive.find({}, {"Date": long_date}))
array_document_school_time_table = find_document_school_time_table[0]["_id"]
array_document_archive_school_time_table = find_document_archive_school_time_table[0]["_id"]
collection.update_one(
{ "_id": ObjectId(array_document_school_time_table)},
{ "$set": {
"School Subject." + array_test[0] + "." + str(gagaga) + ".Subject": school_subject,
}
}
)
collection_archive.update_one(
{ "_id": ObjectId(array_document_archive_school_time_table)},
{ "$set": {
"School Subject." + array_test[0] + "." + str(gagaga) + ".Subject": school_subject,
}
}
)
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_school_time_table = list(collection.find({}, {"Date": long_date}))
find_document_archive_school_time_table = list(collection_archive.find({}, {"Date": long_date}))
array_document_school_time_table = find_document_school_time_table[0]["_id"]
array_document_archive_school_time_table = find_document_archive_school_time_table[0]["_id"]
# Add school subject in MongoDB beacause school subject is not 0
collection.update_one(
{ "_id": ObjectId(array_document_school_time_table)},
{ "$set": {
"School Subject." + array_test[0] + "." + str(gagaga)+ ".Subject": school_subject,
}
}
)
collection_archive.update_one(
{ "_id": ObjectId(array_document_archive_school_time_table)},
{ "$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
for i in range(4, 80):
teacher = ws.cell(row=i, column=column+1).value
column = column
if teacher == 0: #If teacher 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 teacher is 0
{ "_id": ObjectId(array_username)},
{
"$push": { "Teacher": "null" }
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_school_time_table = list(collection.find({}, {"Date": long_date}))
find_document_archive_school_time_table = list(collection_archive.find({}, {"Date": long_date}))
array_document_school_time_table = find_document_school_time_table[0]["_id"]
array_document_archive_school_time_table = find_document_archive_school_time_table[0]["_id"]
collection.update_one(
{ "_id": ObjectId(array_document_school_time_table)},
{ "$set": {
"School Subject." + array_test[0] + "." + str(gagaga_teacher) + ".Teacher": teacher,
}
}
)
collection_archive.update_one( # Add "null" in MongoDB beacause teacher is 0
{ "_id": ObjectId(array_username)},
{
"$push": { "Teacher": teacher }
collection_archive.update_one(
{ "_id": ObjectId(array_document_archive_school_time_table)},
{ "$set": {
"School Subject." + array_test[0] + "." + str(gagaga)+ ".Subject": school_subject,
}
}
)
else: #If teacher is not 0, add teacher in MongoDB
find_document_username = list(collection.find({}, {"Date": long_date}))
array_username = find_document_username[0]["_id"]
collection.update_one( # Add teacher in MongoDB beacause teacher is not 0
{ "_id": ObjectId(array_username)},
{
"$push": { "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_school_time_table = list(collection.find({}, {"Date": long_date}))
find_document_archive_school_time_table = list(collection_archive.find({}, {"Date": long_date}))
array_document_school_time_table = find_document_school_time_table[0]["_id"]
array_document_archive_school_time_table = find_document_archive_school_time_table[0]["_id"]
collection.update_one(
{ "_id": ObjectId(array_document_school_time_table)},
{ "$set": {
"School Subject." + array_test[0] + "." + str(gagaga_teacher)+ ".Teacher": teacher,
}
}
)
collection_archive.update_one( # Add teacher in MongoDB beacause teacher is not 0
{ "_id": ObjectId(array_username)},
{
"$push": { "Teacher": teacher }
collection_archive.update_one(
{ "_id": ObjectId(array_document_archive_school_time_table)},
{ "$set": {
"School Subject." + array_test[0] + "." + str(gagaga)+ ".Subject": school_subject,
}
}
)
number_teacher += 1
gagaga_teacher += 1
dont_repeat_teacher += 1
if number_teacher == 9:
number_teacher = 1
if gagaga_teacher == 8:
gagaga_teacher = 0
os.remove(namefile_xlsx) #Delete excel file
update_time_school()