-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbotfunctions.py
313 lines (251 loc) · 9.12 KB
/
botfunctions.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
from difflib import get_close_matches
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from telegram import Update
from telegram.ext import ContextTypes, CallbackContext, Application
from bs4 import BeautifulSoup
from pandas import DataFrame
import re
def create_df_from_html_table(html: str, columns: list[str]) -> DataFrame:
soup = BeautifulSoup(html, "html.parser")
table = soup.table
table_rows = table.find_all("tr")
res = []
for tr in table_rows:
td = tr.find_all("td")
row = [tr.text for tr in td if tr]
if row:
res.append(row)
df: DataFrame = DataFrame(res, columns=columns)
return df
def write_file(fileName: str, status: dict[int, dict]):
with open(fileName, "w") as f:
for key in status.keys():
s = status[key]
f.write(f"{key},{s['contenido']},{s['mensajeria']},{s['forov2']}\n")
def get_row_idx_from_close_match(materia: str, df: DataFrame, row_name: str) -> int:
index = df.index[df[row_name] == materia]
if len(index) > 0:
return index[0]
else:
close_matches: list[str] = get_close_matches(
materia, df[row_name], cutoff=0.8, n=1
)
if len(close_matches) == 1:
return df.index[df[row_name] == close_matches[0]][0]
else:
return -1
def clean_materia(materia: str) -> str:
materia = materia.lower()
materia = re.sub(r"[àáâãäå]", "a", materia)
materia = re.sub(r"[èéêë]", "e", materia)
materia = re.sub(r"[ìíîï]", "i", materia)
materia = re.sub(r"[òóôõö]", "o", materia)
materia = re.sub(r"[ùúûü]", "u", materia)
materia = materia.replace("1", "i")
materia = materia.replace("2", "ii")
materia = materia.replace("3", "iii")
materia = materia.replace("4", "iv")
materia = materia.replace("5", "v")
materia = materia.replace("6", "vi")
materia = materia.replace("7", "vii")
materia = materia.replace("8", "viii")
materia = materia.replace("9", "ix")
materia = materia.replace("10", "x")
return materia.upper()
async def oferta(
update: Update, context: ContextTypes.DEFAULT_TYPE, html: str, chat_id: int
):
if len(context.args) > 0:
materia = " ".join(context.args)
materia = clean_materia(materia)
else:
await context.bot.send_message(
chat_id=chat_id, text="El comando debe ser: /oferta <nombre-materia>"
)
return
df: DataFrame = create_df_from_html_table(
html,
[
"Código",
"Descripción",
"Cod. Comisión",
"Turno",
"Días",
"Modalidad",
"Sede",
"Observacion",
],
)
df["Código"] = df["Código"].apply(lambda s: int(s) if s.isdigit() else 0)
num_rows: int = df.shape[0]
idx: int = get_row_idx_from_close_match(materia, df, "Descripción")
if idx == -1:
await context.bot.send_message(
chat_id=chat_id, text="No se encontro la materia"
)
return
code: int = int(df.iloc[idx]["Código"])
msg_data = f"""Codigo: {code}\n{df.iloc[idx]["Descripción"]}\nHorarios:\n"""
for i in range(idx, num_rows):
row = df.iloc[i]
cod: str = row["Código"]
if cod != 0 and cod != code:
break
if row["Cod. Comisión"] == "No Ofertada":
msg_data = msg_data + "No Ofertada"
break
msg_data = (
msg_data
+ f"""{"Mañana" if row["Turno"] == "M" else "Tarde" if row["Turno"] == "T" else "Noche"}\t{row["Días"]}\n"""
)
await context.bot.send_message(chat_id=chat_id, text=msg_data)
async def notas(
update: Update, context: ContextTypes.DEFAULT_TYPE, html: str, chat_id: int
):
n: int = 5
df: DataFrame = create_df_from_html_table(
html,
[
"Código",
"Materia",
"Fecha",
"Nota",
"Acta",
],
)
df["Código"] = df["Código"].apply(lambda s: int(s) if s.isdigit() else 0)
df["Materia"] = df["Materia"].apply(lambda s: s.strip())
num_rows: int = df.shape[0]
if len(context.args) > 0:
if context.args[0].isdigit():
n = int(context.args[0])
else:
materia: str = clean_materia(" ".join(context.args))
idx: int = get_row_idx_from_close_match(materia, df, "Materia")
if idx == -1:
await context.bot.send_message(
chat_id=chat_id, text="No se encontro la materia"
)
return
row = df.iloc[idx]
await context.bot.send_message(
chat_id=chat_id, text=f"""{row["Materia"].strip()}\t{row["Nota"]}\n"""
)
return
msg_data: str = f"""Ultimas {n} notas:\n"""
for i in range(num_rows - n, num_rows):
row = df.iloc[i]
msg_data = msg_data + f"""{row["Materia"].strip()}\t{row["Nota"]}\n"""
await context.bot.send_message(chat_id=chat_id, text=msg_data)
async def finales(
update: Update, context: ContextTypes.DEFAULT_TYPE, html: str, chat_id: int
):
if len(context.args) > 0:
materia = " ".join(context.args)
materia = clean_materia(materia)
else:
await context.bot.send_message(
chat_id=chat_id, text="El comando debe ser: /finales <nombre-materia>"
)
return
df: DataFrame = create_df_from_html_table(
html,
[
"Código",
"Descripción",
"Llamado",
"Fecha",
"Hora",
"Modalidad",
"Tipo Mesa",
"Observaciones",
],
)
df["Código"] = df["Código"].apply(lambda s: int(s) if s.isdigit() else 0)
df["Descripción"] = df["Descripción"].apply(lambda s: s.strip())
num_rows: int = df.shape[0]
idx: int = get_row_idx_from_close_match(materia, df, "Descripción")
if idx == -1:
await context.bot.send_message(
chat_id=chat_id, text="No se encontro la materia"
)
return
row = df.iloc[idx]
cod: int = row["Código"]
msg_data: str = f"""Codigo: {cod}\n{row["Descripción"]}\nFechas:\n"""
msg_data = (
msg_data + f"""Llamado: {row["Llamado"]}\t|{row["Fecha"]}||{row["Hora"]}|"""
)
if idx + 1 < num_rows:
row = df.iloc[idx + 1]
if row["Código"] == 0:
msg_data = (
msg_data
+ f"""\nLlamado: {row["Llamado"]}\t|{row["Fecha"]}||{row["Hora"]}|"""
)
await context.bot.send_message(chat_id=chat_id, text=msg_data)
async def send_notification_miel(
notifType: str, materia: str, chatId: int, application: Application
):
await application.bot.send_message(chat_id=chatId, text=f"{materia}: {notifType}")
async def poll_miel(context: CallbackContext):
print("Polling")
driver: webdriver.Firefox = context.job.data["driver"]
status: dict[int, dict[str, int]] = context.job.data["status"]
application: Application = context.job.data["application"]
chatId: int = context.job.data["chatId"]
driver.refresh()
for materiaDiv in driver.find_elements(By.CSS_SELECTOR, "div.w3-light-grey"):
name: str = materiaDiv.find_element(By.CSS_SELECTOR, ".materia-titulo").text
id: str | None = materiaDiv.get_attribute("data-id")
idInt: int = 0
if id is not None:
idInt = int(id)
contenidosCount = 0
mensajeriaCount = 0
foroCount = 0
categories = materiaDiv.find_elements(
By.CSS_SELECTOR, ".materia-herramientas>div>a"
)
if len(categories) < 4:
continue
for i in [0, 2, 3]:
try:
badge = categories[i].find_element(By.CSS_SELECTOR, "span")
except NoSuchElementException:
continue
if badge is not None:
count: int = int(badge.text)
if i == 0:
contenidosCount = count
elif i == 2:
mensajeriaCount = count
elif i == 3:
foroCount = count
if status[idInt]["contenido"] < contenidosCount:
await send_notification_miel(
notifType="contenido",
materia=name,
chatId=chatId,
application=application,
)
status[idInt]["contenido"] = contenidosCount
if status[idInt]["mensajeria"] < mensajeriaCount:
await send_notification_miel(
notifType="mensajeria",
materia=name,
chatId=chatId,
application=application,
)
status[idInt]["mensajeria"] = mensajeriaCount
if status[idInt]["forov2"] < foroCount:
await send_notification_miel(
notifType="foro",
materia=name,
chatId=chatId,
application=application,
)
status[idInt]["forov2"] = foroCount
write_file(".status", status)