diff --git a/src/bot/discord/discord_bot.py b/src/bot/discord/discord_bot.py index 2479e9b..631ac0c 100644 --- a/src/bot/discord/discord_bot.py +++ b/src/bot/discord/discord_bot.py @@ -44,13 +44,14 @@ bot = discord.Bot() async def on_ready(): print('We have logged in as {0.user}'.format(bot)) bot.loop.create_task(orario()) + orario.start() # Search on MongoDB the subject and send a message on Discord if the subject is found -@tasks.loop(seconds=1) +@tasks.loop(seconds=10) async def orario(): documents = collection.find() send_screenshot = 0 - + print("Searching for subject...") # Iterate through the documents for document in documents: for day in document['School Subject']: @@ -64,7 +65,6 @@ async def orario(): driver = webdriver.Firefox(options=options) driver.get('http://127.0.0.1:4999/orario') time.sleep(5) - driver.get_screenshot_as_file("screenshot.png") driver.quit() channel = bot.get_channel(int(GENERAL_ID)) @@ -159,7 +159,17 @@ async def change_school_time( @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"}}) + options = Options() # Set options + options.add_argument("--headless") # Headless mode (so you don't see the browser) + options.add_argument('--disable-gpu') # Disable GPU + driver = webdriver.Firefox(options=options) + driver.get('http://127.0.0.1:4999/orario') + driver.get_screenshot_as_file("screenshot.png") + driver.quit() + channel = bot.get_channel(int(GENERAL_ID)) + await channel.send(file=discord.File("screenshot.png")) + os.remove("screenshot.png") + #@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)): @@ -176,4 +186,6 @@ async def confirm(ctx : ApplicationContext): #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 +while True: + bot.run(DISCORD_TOKEN) + orario.start() \ No newline at end of file diff --git a/src/events/homework/check_homework.py b/src/events/homework/check_homework.py index ea3f6e7..9b9e461 100644 --- a/src/events/homework/check_homework.py +++ b/src/events/homework/check_homework.py @@ -58,32 +58,162 @@ WebDriverWait(browser, 250).until(EC.element_to_be_clickable((By.XPATH, "/html/b # Click on the "Next Day" button to go to the next day's tasks next_day_button = WebDriverWait(browser, 250).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div/div/main/div/div/div[1]/div[1]/div[1]/button[3]'))) -def wait_recheck(): +def wait_homework(): + browser.quit() time.sleep(10800) + check_homework() def check_homework(): - for i in range(1, 10): + for i in range(0, 10): try: + print(i) + date = str(WebDriverWait(browser, 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) + subject = str(WebDriverWait(browser, 250).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div/div/main/div/div[2]/div/ul/li[1]/h2"))).text) + teacher = str(WebDriverWait(browser, 250).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div/div/main/div/div[2]/div/ul/li[1]/span"))).text) text1 = str(WebDriverWait(browser, 250).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div/div/main/div/div[2]/div/ul/li[1]/div/ul/li/p"))).text) + print(date) + print(subject) print(text1) + # Search on MongoDB if the date is already present on the database + if collection.find_one({"Date": date}) is None: + collection.insert_one({"Date": date, "Homework": + { + "Subject": subject, + "Description": text1, + "Teacher": teacher} + } + ) + else: + collection.update_one( + {"Date": date}, + {"$addToSet": {"Homework": + { + "Subject": subject, + "Description": text1, + "Teacher": teacher} + }} + ) + + subject = str(WebDriverWait(browser, 250).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div/div/main/div/div[2]/div/ul/li[2]/h2"))).text) + teacher = str(WebDriverWait(browser, 250).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div/div/main/div/div[2]/div/ul/li[2]/span"))).text) text1 = str(WebDriverWait(browser, 250).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div/div/main/div/div[2]/div/ul/li[2]/div/ul/li/p"))).text) + print(subject) + print(teacher) print(text1) - text1 = str(WebDriverWait(browser, 250).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div/div/main/div/div[2]/div/ul/li[3]/div/ul/li/p"))).text) - print(text1) - text1 = str(WebDriverWait(browser, 250).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div/div/main/div/div[2]/div/ul/li[4]/div/ul/li/p"))).text) - print(text1) - text1 = str(WebDriverWait(browser, 250).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div/div/main/div/div[2]/div/ul/li[5]/div/ul/li/p"))).text) - print(text1) - WebDriverWait(browser, 250).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div/div/main/div/div/div[1]/div[1]/div[1]/button[3]'))).click() # Click on next day button + if collection.find_one({"Date": date}) is None: + collection.insert_one({"Date": date, "Homework": + { + "Subject": subject, + "Description": text1, + "Teacher": teacher} + } + ) + else: + collection.update_one( + {"Date": date}, + {"$addToSet": {"Homework": + { + "Subject": subject, + "Description": text1, + "Teacher": teacher} + }} + ) + try: + subject = str(WebDriverWait(browser, 250).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div/div/main/div/div[2]/div/ul/li[3]/h2"))).text) + teacher = str(WebDriverWait(browser, 250).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div/div/main/div/div[2]/div/ul/li[3]/span"))).text) + text1 = str(WebDriverWait(browser, 250).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div/div/main/div/div[2]/div/ul/li[3]/div/ul/li/p"))).text) + print(subject) + print(teacher) + print(text1) + if collection.find_one({"Date": date}) is None: + collection.insert_one({"Date": date, "Homework": + { + "Subject": subject, + "Description": text1, + "Teacher": teacher} + } + ) + else: + collection.update_one( + {"Date": date}, + {"$addToSet": {"Homework": + { + "Subject": subject, + "Description": text1, + "Teacher": teacher} + }} + ) + except: + subject = str(WebDriverWait(browser, 250).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div/div/main/div/div[2]/div/ul/li[4]/h2"))).text) + teacher = str(WebDriverWait(browser, 250).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div/div/main/div/div[2]/div/ul/li[4]/span"))).text) + text1 = str(WebDriverWait(browser, 250).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div/div/main/div/div[2]/div/ul/li[4]/div/ul/li/p"))).text) + print(subject) + print(teacher) + print(text1) + if collection.find_one({"Date": date}) is None: + collection.insert_one({"Date": date, "Homework": + { + "Subject": subject, + "Description": text1, + "Teacher": teacher} + } + ) + else: + collection.update_one( + {"Date": date}, + {"$addToSet": {"Homework": + { + "Subject": subject, + "Description": text1, + "Teacher": teacher} + }} + ) + + try: + subject = str(WebDriverWait(browser, 250).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div/div/main/div/div[2]/div/ul/li[5]/h2"))).text) + teacher = str(WebDriverWait(browser, 250).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div/div/main/div/div[2]/div/ul/li[5]/span"))).text) + text1 = str(WebDriverWait(browser, 250).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div/div/main/div/div[2]/div/ul/li[5]/div/ul/li/p"))).text) + print(subject) + print(teacher) + print(text1) + if collection.find_one({"Date": date}) is None: + collection.insert_one({"Date": date, "Homework": + { + "Subject": subject, + "Description": text1, + "Teacher": teacher} + } + ) + else: + collection.update_one( + {"Date": date}, + {"$addToSet": {"Homework": + { + "Subject": subject, + "Description": text1, + "Teacher": teacher} + }} + ) + WebDriverWait(browser, 250).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div/div/main/div/div/div[1]/div[1]/div[1]/button[3]'))).click() # Click on next day button + except: + WebDriverWait(browser, 250).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div/div/main/div/div/div[1]/div[1]/div[1]/button[3]'))).click() except: try: + date = str(WebDriverWait(browser, 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) text = str(WebDriverWait(browser, 250).until(EC.visibility_of_element_located((By.XPATH, "/html/body/div/div/main/div/p"))).text) + print(date) print(text) + if collection.find_one({"Date": date}) is None: + collection.insert_one({"Date": date, "Homework": [text]}) + else: + collection.update_one( + {"Date": date}, + {"$addToSet": {"Homework": text}} + ) WebDriverWait(browser, 250).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div/div/main/div/div/div[1]/div[1]/div[1]/button[3]'))).click() # Click on next day button except: WebDriverWait(browser, 250).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div/div/main/div/div/div[1]/div[1]/div[1]/button[3]'))).click() # Click on next day button - browser.quit() - wait_recheck() + wait_homework() check_homework() \ No newline at end of file