-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord_bot.py
80 lines (65 loc) · 2.77 KB
/
discord_bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from bs4 import BeautifulSoup
import discord
import asyncio
import requests
import aioconsole
import config
TOKEN = config.TOKEN
CHANNEL_ID = config.CHANNEL_ID
intents = discord.Intents.default() # Use default intents
client = discord.Client(intents=intents)
async def login():
# Setup webdriver
webdriver_service = Service(ChromeDriverManager().install())
chrome_options = Options()
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-web-security")
chrome_options.add_argument("--allow-running-insecure-content")
chrome_options.add_argument("--enable-features=SameSiteByDefaultCookies,CookiesWithoutSameSiteMustBeSecure")
driver = webdriver.Chrome(service=webdriver_service, options=chrome_options)
# Open login page
driver.get('https://avenue.mcmaster.ca/login.php?target=%2Fd2l%2Fhome%2F6605')
# Wait for user to manually login
await aioconsole.ainput("Press Enter to continue after you have logged in...")
# Get cookies from the session and quit the driver
cookies = driver.get_cookies()
driver.quit()
# Create a requests session and add the cookies to it
session = requests.Session()
for cookie in cookies:
session.cookies.set(cookie['name'], cookie['value'])
return session
previous_row_count = 2
def check_for_updates(session):
global previous_row_count
try:
# Get the page and parse it with BeautifulSoup
response = session.get('https://avenue.cllmcmaster.ca/d2l/lms/quizzing/user/quizzes_list.d2l?ou=600882')
soup = BeautifulSoup(response.text, 'html.parser')
rows = soup.find_all('tr')
# If there are more rows than before, send a message to the discord channel
if len(rows) > previous_row_count:
asyncio.run_coroutine_threadsafe(client.get_channel(CHANNEL_ID).send("A new quiz has been posted!"), client.loop)
# Update the row count
previous_row_count = len(rows)
except Exception as e:
print(f'Error in check_for_updates: {e}')
@client.event
async def on_ready():
try:
# Log the bot in
session = await login()
# Continuously check for updates every 60 seconds
while True:
check_for_updates(session)
await asyncio.sleep(60)
except Exception as e:
print(f'Error in on_ready: {e}')
# Run the bot
client.run(TOKEN)