From 9f075f2284496e407f670b426ddf8c30887e0074 Mon Sep 17 00:00:00 2001 From: Ashfaaq Rifath Date: Fri, 5 Jul 2024 23:51:00 +0530 Subject: [PATCH] Update moodle_alert_system.pyw --- moodle_alert_system.pyw | 78 +++++++++++++++++++++++++++++++++++------ 1 file changed, 67 insertions(+), 11 deletions(-) diff --git a/moodle_alert_system.pyw b/moodle_alert_system.pyw index db0ad6b..5ae5ddf 100644 --- a/moodle_alert_system.pyw +++ b/moodle_alert_system.pyw @@ -38,6 +38,20 @@ def get_time_element(url): time_list.append(time.text) return time_list +def get_link_element(url): + response = requests.get(url) + bs = BeautifulSoup(response.text, "html.parser") + elements = bs.find_all("a", {"data-region": "post-action"})[:3] + + link_list = [] + for link in elements: + link_list.append(link.get('href')) + + if link_list: + return link_list + else: None + + def telegram_alert(send): bot_token = "BOT TOKEN" my_chatID = "CHAT ID" @@ -46,6 +60,13 @@ def telegram_alert(send): response = requests.get(send_text) return response.json() +def win_notification(title, msg_time): + notification.notify( + title=title, + message=msg_time, + app_icon=None, + timeout=5,) + ########################################################################### pid = os.getpid() @@ -54,7 +75,7 @@ notification.notify( message=f'''Program running in the background Process ID: {pid}''', app_icon=None, - timeout=5,) + timeout=3,) with open("mas_pid.txt", "w") as f: f.write(f"Moodle Alert System process ID: {str(pid)}") @@ -66,12 +87,13 @@ bot = telebot.TeleBot("BOT TOKEN") @bot.message_handler(func=lambda message: True) def command_engine(message): - if message.text.lower() == "start": + if message.text.lower() == "/start": - site_url = "https://sam.sliitacademy.lk/" + site_url = "https://vle.sliitcityuni.lk/" content = get_div_class(site_url) title = get_h3_class(site_url) time_stamp = get_time_element(site_url) + permalink = get_link_element(site_url) if content is not None and title is not None and time_stamp is not None: num = 4 @@ -80,6 +102,8 @@ def command_engine(message): notice_content = str(content[i]) notice_title = str(title[i]) notice_time = str(time_stamp[i]) + notice_link = permalink[i] + output = f''' *SITE ANNOUNCEMENT {num} 🔴* @@ -89,14 +113,43 @@ def command_engine(message): {notice_content} {'-'*50} - _SLIIT Moodle Alert System - v1.5_ - _Copyright (c) Ashfaaq Rifath_''' + [SLIIT Moodle Alert System]({notice_link})''' telegram_alert(output) else: bot.reply_to(message, "Error") - elif message.text.lower() == "stop": + + elif message.text.isdigit(): + + notice_id = message.text + + site_url = f"https://vle.sliitcityuni.lk/mod/forum/discuss.php?d={notice_id}" + + response = requests.get(site_url) + bs = BeautifulSoup(response.text, "html.parser") + element = bs.find('h3', class_="h6 font-weight-bold mb-0") + element2 = bs.find("time") + time_stamp = element2.text + title = element.text + + notice_title = str(title) + notice_time = str(time_stamp) + + output = f''' + *SITE ANNOUNCEMENT 🔴* + + *{notice_title}* + _{notice_time}_ + + {'-'*50} + [SLIIT Moodle Alert System]({site_url})''' + + telegram_alert(output) + + + + elif message.text.lower() == "/stop": notification.notify( title="Moodle Alert System", message="Program Termianted", @@ -119,11 +172,12 @@ def telegram_bot(): def moodle_alert_system(): while True: try: - site_url = "https://sam.sliitacademy.lk/" + site_url = "https://vle.sliitcityuni.lk/" content = get_div_class(site_url) title = get_h3_class(site_url) time_stamp = get_time_element(site_url) + permalink = get_link_element(site_url) if content is not None and title is not None and time_stamp is not None: num = 4 @@ -132,6 +186,7 @@ def moodle_alert_system(): notice_content = str(content[i]) notice_title = str(title[i]) notice_time = str(time_stamp[i]) + notice_link = permalink[i] output = f''' *SITE ANNOUNCEMENT {num} 🔴* @@ -140,19 +195,20 @@ def moodle_alert_system(): _{notice_time}_ {'-'*50} {notice_content} - + {'-'*50} - _SLIIT Moodle Alert System - v1.5_ - _Copyright (c) Ashfaaq Rifath_''' + [SLIIT Moodle Alert System]({notice_link})''' telegram_alert(output) - time.sleep(3600) + win_notification(notice_title, notice_time) + time.sleep(10800) else: quit() except: #print("Working...") time.sleep(300) + telegram_bot_thread = threading.Thread(target=telegram_bot) telegram_bot_thread.start() moodle_alert_system()