From e856c71eff07844b950a67b5ba71a6d49ee82b70 Mon Sep 17 00:00:00 2001 From: Stefano Assenzo Date: Fri, 10 Feb 2023 15:40:16 +0000 Subject: [PATCH] Now let us know if the two electrical and electronics lab rooms are occupied or not --- src/bot/discord/discord_bot.py | 38 ++- src/bot/whatsapp/whatsapp_bot.py | 30 -- src/events/school_time/email_read.py | 5 +- src/events/school_time/update_time_school.py | 179 ++++++++++- templates/orario/orario.html | 316 ++++++++++++++++++- 5 files changed, 513 insertions(+), 55 deletions(-) delete mode 100644 src/bot/whatsapp/whatsapp_bot.py diff --git a/src/bot/discord/discord_bot.py b/src/bot/discord/discord_bot.py index 0deb84c..4abde02 100644 --- a/src/bot/discord/discord_bot.py +++ b/src/bot/discord/discord_bot.py @@ -27,6 +27,7 @@ mongo_url = "mongodb+srv://elci:" + urllib.parse.quote_plus(PASSWORD_MONGODB) + client = pymongo.MongoClient(mongo_url) # Connect to MongoDB database = client["website-class"] # Database name collection = database["school-time-table"] # Collection school time table current +collection_email = database["email"] # Collection email #Date current_time = datetime.datetime.now() # Current time @@ -66,7 +67,8 @@ async def orario(): driver.get_screenshot_as_file("screenshot.png") driver.quit() - await channel.send(file=discord.File('screenshot.png')) + channel = bot.get_channel(GENERAL_ID) + await channel.send(file=discord.File("screenshot.png")) os.remove("screenshot.png") send_screenshot += 1 else: @@ -119,20 +121,24 @@ async def change_school_time( {"$set": {f"School Subject.{day}.{int(hour_school)}.Subject": text}} ) -@bot.slash_command() -@discord.default_permissions(ban_members = True, administrator = True) -async def ban(ctx, member: Option(discord.Member, description="Select a member", required=True), reason: Option(str, description="Reason", required=True)): - ban.ban_user(bot, ctx, member, reason) - -@bot.slash_command() -@discord.default_permissions(kick_members = True, administrator = True) -async def kick(ctx, member: Option(discord.Member, description="Select a member", required=True), reason: Option(str, description="Reason", required=True)): - kick.kick_user(bot, ctx, member, reason) - -@bot.slash_command(name= 'clear', description= 'Clears messages from a channel') -@commands.has_permissions(manage_messages=True, administrator=True) -@commands.cooldown(1, 5, commands.BucketType.user) -async def clear(ctx, messages: Option(int, description="Amount of messages to delete", required=True)): - clear_msg.clear_messages(ctx, amount = messages) +@bot.slash_command(name='confirm', description='Confirm change school time') +async def confirm(ctx : ApplicationContext): + await ctx.respond(f"Confirm") + collection_email.update_one({}, {"$set": {"Send on Whatsapp": "yes"}}) +#@bot.slash_command() +#@discord.default_permissions(ban_members = True, administrator = True) +#async def ban(ctx, member: Option(discord.Member, description="Select a member", required=True), reason: Option(str, description="Reason", required=True)): +# ban.ban_user(bot, ctx, member, reason) +# +#@bot.slash_command() +#@discord.default_permissions(kick_members = True, administrator = True) +#async def kick(ctx, member: Option(discord.Member, description="Select a member", required=True), reason: Option(str, description="Reason", required=True)): +# kick.kick_user(bot, ctx, member, reason) +# +#@bot.slash_command(name= 'clear', description= 'Clears messages from a channel') +#@commands.has_permissions(manage_messages=True, administrator=True) +#@commands.cooldown(1, 5, commands.BucketType.user) +#async def clear(ctx, messages: Option(int, description="Amount of messages to delete", required=True)): +# clear_msg.clear_messages(ctx, amount = messages) bot.run(DISCORD_TOKEN) \ No newline at end of file diff --git a/src/bot/whatsapp/whatsapp_bot.py b/src/bot/whatsapp/whatsapp_bot.py deleted file mode 100644 index 835e64b..0000000 --- a/src/bot/whatsapp/whatsapp_bot.py +++ /dev/null @@ -1,30 +0,0 @@ -from selenium.webdriver.firefox.options import Options # Selenium -from selenium import webdriver # Selenium -from dotenv import load_dotenv -from twilio.rest import Client -import os - -load_dotenv() -SID = os.getenv('SID') -AUTH_TOKEN = os.getenv('AUTHTOKEN') -PHONE_NUMBER_BOT = os.getenv('PHONE_NUMBER_BOT') -PHONE_NUMBER_PERSONAL = os.getenv('PHONE_NUMBER_PERSONAL') - -client = Client(SID, AUTH_TOKEN) - -options = Options() # Set options -options.add_argument("--headless") # Headless mode (so you don't see the browser) -options.add_argument('--disable-gpu') # Disable GPU -options.add_argument('window-size=1024x768') -driver = webdriver.Firefox(options=options) -driver.get('http://127.0.0.1:4999/orario') -driver.get_screenshot_as_file("screenshot.png") -driver.quit() -message = client.messages \ - .create( - body="Test", - media_url=['IMAGE URL'], - from_=PHONE_NUMBER_BOT, - to=PHONE_NUMBER_PERSONAL - ) -print(message.sid) \ No newline at end of file diff --git a/src/events/school_time/email_read.py b/src/events/school_time/email_read.py index 269eacf..0a283bd 100644 --- a/src/events/school_time/email_read.py +++ b/src/events/school_time/email_read.py @@ -47,7 +47,7 @@ def check_email(): if extension_file != ".xlsx": # Check if file is xlsx print("Check file") else: - if collection.find_one({"filename": att_fn}): + if collection.find_one({"Filename": att_fn, "Send on Whatsapp": "no"}): print("File already exists") recheck_email() else: @@ -55,7 +55,7 @@ def check_email(): 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 + collection.insert_one({"Filename": att_fn, "Send on Whatsapp": "no"}) # Insert filename to MongoDB send_xlsx() except: print(traceback.print_exc()) @@ -70,7 +70,6 @@ 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() \ No newline at end of file diff --git a/src/events/school_time/update_time_school.py b/src/events/school_time/update_time_school.py index aa8b212..af17f60 100644 --- a/src/events/school_time/update_time_school.py +++ b/src/events/school_time/update_time_school.py @@ -44,48 +44,64 @@ def update_time_school(): "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, ], "Tuesday": [ @@ -94,48 +110,64 @@ def update_time_school(): "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, ], "Wednesday": [ @@ -144,48 +176,64 @@ def update_time_school(): "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, ], "Thursday": [ @@ -194,48 +242,64 @@ def update_time_school(): "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, ], "Friday": [ @@ -244,48 +308,64 @@ def update_time_school(): "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, ], "Saturday": [ @@ -294,48 +374,64 @@ def update_time_school(): "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, { "Subject": "", "Teacher": "", "Room": "", "PE with": "", + "le is busy": "", + "le3 is busy": "", }, ], } @@ -392,6 +488,23 @@ def update_time_school(): 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"] + if remove_things_in_front == "MISURE ELETTRICHE": + remove_things_in_front = "MISURE" + elif remove_things_in_front == " EDUCAZIONE ATTIVITA' MOTORIE": + remove_things_in_front = "MOTORIA" + elif remove_things_in_front == "EDUCAZIONE ATTIVITA' MOTORIE": + remove_things_in_front = "Motoria" + elif remove_things_in_front == "DIRITTO ED ECONOMIA": + remove_things_in_front = "DIRITTO" + elif remove_things_in_front == "INGLESE PROFESSIONALE": + remove_things_in_front = "INGLESE PRO." + elif remove_things_in_front == "LABORATORIO ELETTRICO": + remove_things_in_front = "LAB. ELETTRICO" + elif remove_things_in_front == "LINGUA INGLESE": + remove_things_in_front = "INGLESE" + elif remove_things_in_front == "SCIENZE INTEGRATE - FISICA": + remove_things_in_front = "FISICA" + # Add school subject in MongoDB beacause school subject is not 0 collection.update_one( { "_id": ObjectId(array_document_school_time_table)}, @@ -421,7 +534,6 @@ def update_time_school(): array_test.append(convert_date_to_day) number_day += 1 if school_subject == 0: #If school subject is 0, add "" in MongoDB - print(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"] @@ -429,7 +541,7 @@ def update_time_school(): collection.update_one( { "_id": ObjectId(array_document_school_time_table)}, { "$set": { - "School Subject." + array_test[0] + "." + str(gagaga) + ".Subject": school_subject, + "School Subject." + array_test[0] + "." + str(gagaga) + ".Subject": remove_things_in_front, } } ) @@ -450,7 +562,29 @@ def update_time_school(): 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] + if school_subject == "21PM1 MISURE ELETTRICHE": + school_subject = "MISURE" + elif school_subject == " CEAM EDUCAZIONE ATTIVITA' MOTORIE": + school_subject = "MOTORIA" + elif school_subject == "CEAM EDUCAZIONE ATTIVITA' MOTORIE": + school_subject = "MOTORIA" + elif school_subject == "CSGGE1 DIRITTO ED ECONOMIA": + school_subject = "DIRITTO" + elif school_subject == "PR1 INGLESE PROFESSIONALE": + school_subject = "INGLESE PRO." + elif school_subject == "PR1 LABORATORIO ELETTRICO": + school_subject = "LAB. ELETTRICO" + elif school_subject == "CLING LINGUA INGLESE": + school_subject = "INGLESE" + elif school_subject == "CMST2 SCIENZE INTEGRATE - FISICA": + school_subject = "FISICA" + elif school_subject == "PR1 DISEGNO": + school_subject = "DISEGNO" + elif school_subject == "CSGGE2 STORIA": + school_subject = "STORIA" + elif school_subject == "CMST1 MATEMATICA": + school_subject = "MATEMATICA" + 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"] @@ -459,14 +593,14 @@ def update_time_school(): collection.update_one( { "_id": ObjectId(array_document_school_time_table)}, { "$set": { - "School Subject." + array_test[0] + "." + str(gagaga)+ ".Subject": remove_things_in_front, + "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": remove_things_in_front, + "School Subject." + array_test[0] + "." + str(gagaga)+ ".Subject": school_subject, } } ) @@ -645,4 +779,39 @@ def update_time_school(): else: pass + # Search if class le3 is busy + for c in range(1,100): + search_other_subject = ws.cell(row=i, column=c).value + search_room = ws.cell(row=i, column=c).value + if c == column: + pass + else: + if teacher == "Martin": + if search_other_subject == "le3" or search_other_subject == "le": + search_class = ws.cell(row=3, column=c-2).value + if search_class == "2elci": + pass + else: + if c == column: + pass + else: + if search_other_subject == "le3": + collection.update_one( + { "_id": ObjectId(array_document_school_time_table)}, + { "$set": { + "School Subject." + array_test[0] + "." + str(gagaga_room-1)+ ".le3 is busy": search_class, + } + } + ) + if search_other_subject == "le": + collection.update_one( + { "_id": ObjectId(array_document_school_time_table)}, + { "$set": { + "School Subject." + array_test[0] + "." + str(gagaga_room-1)+ ".le is busy": search_class, + } + } + ) + else: + pass + update_time_school() \ No newline at end of file diff --git a/templates/orario/orario.html b/templates/orario/orario.html index a310521..3fa4795 100644 --- a/templates/orario/orario.html +++ b/templates/orario/orario.html @@ -1,6 +1,5 @@ -