Fix bot Discord and add new command for send image school time table - Finish upload homework on MongoDB (there is a small bug but it is not a big deal)

This commit is contained in:
Stefano Assenzo 2023-03-25 16:43:20 +00:00 committed by GitHub
parent 815f3fc0d6
commit 62b9c93a2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 158 additions and 16 deletions

View File

@ -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)
while True:
bot.run(DISCORD_TOKEN)
orario.start()

View File

@ -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()