-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskISBot.py
More file actions
165 lines (125 loc) · 5.13 KB
/
TaskISBot.py
File metadata and controls
165 lines (125 loc) · 5.13 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import datetime
import multiprocessing as mp
import os
import telebot
import time
import requests
from bs4 import BeautifulSoup
from typing import List
TOKEN = os.environ["TOKEN_BOT"]
TASKS_FILENAME = "tasks.txt"
CHATID_FILENAME = "chatIds.txt"
session = requests.Session()
bot = telebot.TeleBot(TOKEN)
def get_tasks_names() -> List[str]:
"""
This function get all tasks names and return they as a list of string
"""
is_url = "http://elearning.informatica.unisa.it/el-platform/course/view.php?id=464"
response = session.get(is_url)
site_dom = BeautifulSoup(response.content, "html.parser")
task_li_element = site_dom.find(id="section-2")
tasks_list = task_li_element.find_all("li", {"class": "activity assign modtype_assign"})
tasks_names = []
for task in tasks_list:
task_span = task.find("span", {"class": "instancename"})
tasks_names.append(task_span.text)
return tasks_names
def do_login(username: str = None, password: str = None) -> str:
"""
This function perform a login action on e-leraning platform
"""
if username is None or password is None:
return "E zumbat"
login_url = "http://elearning.informatica.unisa.it/el-platform/login/index.php"
response = session.get(login_url)
site_dom = BeautifulSoup(response.content, "html.parser")
login_form = site_dom.find(id="guestlogin")
data = {
"logintoken": login_form.input["value"],
"username": username,
"password": password
}
response = session.post(login_url, data=data)
print("Valore del login eseguito: ", os.environ["NAME_SURNAME"] in response.content.__str__())
def check_if_there_is_new_task() -> str:
"""
This function check if there are new tasks
"""
tasks_names = get_tasks_names()
task_file = open(TASKS_FILENAME, "r")
tasks_names_read_from_file = task_file.readlines()
task_file.close()
len_task_names = len(tasks_names)
len_task_names_file = len(tasks_names_read_from_file)
if len_task_names > len_task_names_file:
new_tasks = []
for i in range(len_task_names_file, len_task_names):
new_tasks.append(tasks_names[i])
message = "Ci sono nuove task da svolgere: "
for task in new_tasks:
message += "\n\t- " + task
return message
return None
def initialize_tasks_file():
"""
This function read the current tasks and save them in a file
"""
tasks_names = get_tasks_names()
tasks_file = open(TASKS_FILENAME, "w")
tasks_file.writelines("\n".join(tasks_names))
tasks_file.close()
def do_logout():
"""
This function perform a logout action on e-leraning platform
"""
logout_url = "http://elearning.informatica.unisa.it/el-platform/login/logout.php"
response = session.get(logout_url)
site_dom = BeautifulSoup(response.content, "html.parser")
form = site_dom.find("form", {"action": logout_url})
sesskey = form.input["value"]
response = session.post(logout_url, data={"sesskey": sesskey})
print("Valore di logout eseguito: ", "Non sei collegato" in response.content.__str__())
def save_chat_id(chatId):
"""
This function save chat ids in a file to save who to send message
"""
chat_id_file = open(CHATID_FILENAME, "a")
chat_id_file.write(str(chatId) + "\n")
chat_id_file.close()
def check_tasks(login_function, args):
"""
This function check if there are new tasks every 3 hours, except from midnight to 10 AM
"""
while True:
current = datetime.datetime.now()
current_time = current.time()
midnight = datetime.datetime(year=current.year, month=current.month, day=current.day, hour=00, minute=00, second=00).time()
tenAM = datetime.datetime(year=current.year, month=current.month, day=current.day, hour=10, minute=00, second=00).time()
if not midnight < current_time < tenAM:
login_function(*args)
tasks_message = check_if_there_is_new_task()
do_logout()
if tasks_message:
while not os.path.exists(CHATID_FILENAME):
time.sleep(1)
chat_id_file = open(CHATID_FILENAME, "r")
chat_ids = chat_id_file.readlines()
chat_id_file.close()
for chatId in chat_ids:
bot.send_message(chatId.replace("\n", ""), tasks_message)
time.sleep(60*60*3)
@bot.message_handler(commands=["start"])
def check_messages(message):
bot.send_message(message.chat.id, "Da adesso in poi riceverai le notifiche ogni qualvolta verrà caricata una task!")
save_chat_id(message.chat.id)
@bot.message_handler(func=lambda message: message != "/start")
def generic_answer(message):
bot.send_message(message.chat.id, "Comando non riconosciuto")
username, password = os.environ["USERNAME"], os.environ["PASSWORD"]
do_login(username=username, password=password)
initialize_tasks_file()
do_logout()
check_tasks_process = mp.Process(name="Check Tasks", target=check_tasks, args=(do_login, [username, password]))
check_tasks_process.start()
bot.polling(none_stop=False)