From 9bbe45958559d673cf520823e8ac086896c7480d Mon Sep 17 00:00:00 2001 From: Stefano Assenzo Date: Fri, 31 Mar 2023 16:20:24 +0000 Subject: [PATCH] Fix little things --- app.py | 7 ++ src/bot/discord/discord_bot.py | 4 +- src/bot/telegram/main.py | 9 +- src/bot/telegram/requirements.txt | 1 + src/events/school_time/update_time_school.py | 98 ++++++++------------ 5 files changed, 55 insertions(+), 64 deletions(-) diff --git a/app.py b/app.py index f70c0a4..2edf01e 100644 --- a/app.py +++ b/app.py @@ -1,6 +1,9 @@ +#import src.bot.discord.discord_bot + from flask import Flask, render_template, url_for, request, redirect, session from pymongo import MongoClient from dotenv import load_dotenv +import threading import logging import urllib import bcrypt @@ -136,6 +139,10 @@ def logout(): else: return render_template('index.html') +# Threading discord_bot.py +#import src.events.school_time.email_read # Start email_read.py +#x = threading.Thread(target=os.system('python src/bot/discord/discord_bot.py')).start() + if __name__ == '__main__': logging.info("Web server started!") app.run(port=4999, debug=True) \ No newline at end of file diff --git a/src/bot/discord/discord_bot.py b/src/bot/discord/discord_bot.py index 631ac0c..7676bb8 100644 --- a/src/bot/discord/discord_bot.py +++ b/src/bot/discord/discord_bot.py @@ -1,4 +1,4 @@ -from commands.moderation import ban, kick, clear_msg +#from commands.moderation import ban, kick, clear_msg from discord.commands.context import ApplicationContext # Discord from selenium.webdriver.firefox.options import Options # Selenium from discord.commands import Option # Discord @@ -56,7 +56,7 @@ async def orario(): for document in documents: for day in document['School Subject']: for i, subject in enumerate(document['School Subject'][day]): - if subject['Subject'] == "LINGUA ITALIANA": + if subject['Subject'] == "LINGUA ITALIANA" or "ITALIANA": # Send a message on channel #general with the subject found and the index of the subject options = Options() # Set options options.add_argument("--headless") # Headless mode (so you don't see the browser) diff --git a/src/bot/telegram/main.py b/src/bot/telegram/main.py index efe7253..b1e85f3 100644 --- a/src/bot/telegram/main.py +++ b/src/bot/telegram/main.py @@ -8,6 +8,7 @@ import time import schedule import pymongo import urllib +import pytz import os load_dotenv() @@ -87,16 +88,18 @@ def send_notification(): for b in array_username: bot.send_message(b, i['School Subject'][tomorrow][0]['Subject'] + ", " + i['School Subject'][tomorrow][0]['Teacher'] + "\n" + i['School Subject'][tomorrow][1]['Subject'] + ", " + i['School Subject'][tomorrow][1]['Teacher'] + "\n" + i['School Subject'][tomorrow][2]['Subject'] + ", " + i['School Subject'][tomorrow][2]['Teacher'] + "\n" + i['School Subject'][tomorrow][3]['Subject'] + ", " + i['School Subject'][tomorrow][3]['Teacher'] + "\n" + i['School Subject'][tomorrow][4]['Subject'] + ", " + i['School Subject'][tomorrow][4]['Teacher'] + "\n" + i['School Subject'][tomorrow][5]['Subject'] + ", " + i['School Subject'][tomorrow][5]['Teacher']) +schedule.every().day.at("05:50").do(send_notification) schedule.every().day.at("06:50").do(send_notification) schedule.every().day.at("07:50").do(send_notification) -schedule.every().day.at("08:50").do(send_notification) +schedule.every().day.at("9:05").do(send_notification) schedule.every().day.at("10:05").do(send_notification) schedule.every().day.at("11:05").do(send_notification) -schedule.every().day.at("12:05").do(send_notification) schedule.every().day.at("20:00").do(send_notification) -now = datetime.datetime.now() +tz = pytz.timezone("Europe/Rome") +now = datetime.datetime.now(tz) t1 = threading.Thread(target=bot.polling).start() while True: time.sleep(10) + print(now) schedule.run_pending() diff --git a/src/bot/telegram/requirements.txt b/src/bot/telegram/requirements.txt index 6eac50c..229e35c 100644 --- a/src/bot/telegram/requirements.txt +++ b/src/bot/telegram/requirements.txt @@ -10,3 +10,4 @@ telebot==0.0.5 schedule==1.1.0 pymongo==4.3.3 urllib3==1.26.13 +pytz==2022.6 \ 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 936d71a..807f456 100644 --- a/src/events/school_time/update_time_school.py +++ b/src/events/school_time/update_time_school.py @@ -826,64 +826,44 @@ def update_time_school(): ) else: pass +# + docs = collection.find() - #for i in range(1,5): - # # Search on Mongodb every first hour of the day if is egual to CALF1 LINGUA ITALIANA - # print(i) - # find_document_school_time_table = list(collection.find({}, {"Date": long_date})) - # array_document_school_time_table = find_document_school_time_table[0]["_id"] - # print([array_test[0]]) - # # Print Subject 0 Monday - # print(array_document_school_time_table[0]["School Subject"][array_test[0]][0]["Subject"]) - - for i in range(0 , 5): - for x in collection.find({},{ "Date": long_date, "School Subject": 1, "_id": 0}): - testgaga = x['School Subject'][array_test[0]][0]['Subject'] - print(testgaga) - if testgaga == "0" or testgaga == 0 or testgaga == None: - collection.update_one( - { "_id": ObjectId(array_document_school_time_table)}, - { "$set": { - "School Subject." + array_test[0] + "." + str(i)+ ".Subject": "", - } - } - ) - collection.update_one( - { "_id": ObjectId(array_document_school_time_table)}, - { "$set": { - "School Subject." + array_test[0] + "." + str(i)+ ".Teacher": "", - } - } - ) - collection.update_one( - { "_id": ObjectId(array_document_school_time_table)}, - { "$set": { - "School Subject." + array_test[0] + "." + str(i)+ ".Room": "", - } - } - ) - else: - try: - remove_things_in_front = testgaga.split(' ', 1)[1] - if remove_things_in_front == "ITALIANA" or remove_things_in_front == "ELETTRICO": - if remove_things_in_front == "ELETTRICO": - remove_things_in_front = "LAB. ELETTRICO" - collection.update_one( - { "_id": ObjectId(array_document_school_time_table)}, - { "$set": { - "School Subject." + array_test[0] + "." + str(i)+ ".Subject": remove_things_in_front, - } - } - ) - pass - else: - collection.update_one( - { "_id": ObjectId(array_document_school_time_table)}, - { "$set": { - "School Subject." + array_test[0] + "." + str(i)+ ".Subject": remove_things_in_front, - } - } - ) - except: - pass + # Iterate over each document + for doc in docs: + # Access the ObjectId field of the document + doc_id = doc["_id"] + day = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] + for i in range(0, 5): + pomate = doc["School Subject"][day[i]][0]['Subject'] + try: + remove_things_in_front = pomate.split(' ', 1)[1] + if remove_things_in_front == "ITALIANA": + collection.update_one( + { "_id": ObjectId(doc_id)}, + { "$set": { + "School Subject." + day[i] + ".0.Subject": "LINGUA ITALIANA", + } + } + ) + elif remove_things_in_front == "ELETTRICO": + collection.update_one( + { "_id": ObjectId(doc_id)}, + { "$set": { + "School Subject." + day[i] + ".0.Subject": "LAB. ELETTRICO", + } + } + ) + else: + collection.update_one( + { "_id": ObjectId(doc_id)}, + { "$set": { + "School Subject." + day[i] + ".0.Subject": remove_things_in_front, + } + } + ) + except: + pass + + update_time_school() \ No newline at end of file