-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
212 lines (165 loc) · 6.27 KB
/
main.py
File metadata and controls
212 lines (165 loc) · 6.27 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
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
import signal
from functools import partial
from types import FrameType
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.firefox.options import Options
from telegram.ext import ApplicationBuilder, CommandHandler, Application
from botfunctions import poll_miel, oferta, notas, finales, write_file
async def post_init(application: Application):
await application.bot.set_my_commands(
[
("oferta", "Realiza una consulta a la oferta de materias"),
("notas", "Realiza una consulta a tus notas"),
("finales", "Realiza una consulta a las fechas de finales"),
]
)
def sig_close(sig: int, frame: FrameType | None):
print("Closing")
if frame is not None:
status = frame.f_locals.get("status")
driver = frame.f_locals.get("driver")
if status is not None:
print("Writing file")
write_file(".status", status)
if driver is not None:
print("Closing browser")
driver.quit()
exit(0)
def init_driver(dni: str, password: str) -> webdriver.Firefox:
print("Initializing driver")
options: Options = Options()
options.add_argument("--headless")
driver: webdriver.Firefox = webdriver.Firefox(options=options)
driver.get("https://miel.unlam.edu.ar")
ActionChains(driver=driver).send_keys_to_element(
driver.find_element(By.ID, "usuario"), dni
).send_keys_to_element(driver.find_element(By.ID, "clave"), password).send_keys(
Keys.RETURN
).perform()
WebDriverWait(driver, 15).until(lambda e: e.find_element(By.ID, "menu-principal"))
if driver.current_url != "https://miel.unlam.edu.ar/principal/interno/":
print("No se pudo iniciar sesion")
driver.close()
exit(1)
return driver
def load_status(driver: webdriver.Firefox) -> dict[int, dict[str, int]]:
print("loading status")
element_ids = [
element.get_attribute("data-id")
for element in driver.find_elements(
By.CSS_SELECTOR, "body main div.curso-sortable div[data-id]"
)
]
status: dict[int, dict[str, int]] = {}
for id in element_ids:
if id is not None:
id_int = int(id)
status[id_int] = {
"contenido": 0,
"mensajeria": 0,
"forov2": 0,
}
with open(".status", "+a") as f:
f.seek(0)
for line in f.readlines():
parts: list[str] = line.split(",")
if len(parts) == 4:
currentID = int(parts[0])
if currentID in status.keys():
status[currentID]["contenido"] = int(parts[1])
status[currentID]["mensajeria"] = int(parts[2])
status[currentID]["forov2"] = int(parts[3])
else:
f.truncate(0)
break
return status
def update_intraconsulta(
driver: webdriver.Firefox, dni: int, password: str
) -> tuple[str, str, str]:
original_window = driver.current_window_handle
driver.switch_to.new_window("tab")
driver.get("https://alumno2.unlam.edu.ar/")
ActionChains(driver=driver).send_keys_to_element(
driver.find_element(By.ID, "usuario"), dni
).send_keys_to_element(driver.find_element(By.ID, "clave"), password).send_keys(
Keys.RETURN
).perform()
WebDriverWait(driver, 10).until(
lambda e: e.find_element(By.CSS_SELECTOR, ".nav-pills")
)
driver.execute_script("document.querySelector('#link03').click()")
WebDriverWait(driver, 10).until(lambda e: e.find_element(By.TAG_NAME, "table"))
oferta_table: str = driver.find_element(By.TAG_NAME, "table").get_attribute(
"outerHTML"
)
driver.execute_script("document.querySelector('#link11').click()")
WebDriverWait(driver, 10).until(
lambda e: e.find_element(By.PARTIAL_LINK_TEXT, "Consultar Finales Desaprobados")
)
notas_table: str = driver.find_element(By.TAG_NAME, "table").get_attribute(
"outerHTML"
)
driver.execute_script("document.querySelector('#link04').click()")
WebDriverWait(driver, 10).until(
lambda e: e.find_element(By.PARTIAL_LINK_TEXT, "Cerrar ventana")
)
finales_table: str = driver.find_elements(By.TAG_NAME, "table")[1].get_attribute(
"outerHTML"
)
driver.close()
driver.switch_to.window(original_window)
return (oferta_table, notas_table, finales_table)
if __name__ == "__main__":
signal.signal(signalnum=signal.SIGINT, handler=sig_close)
dni: str = ""
password: str = ""
token: str = ""
chat_id: int = 0
with open(".token") as f:
token = f.readline().strip()
chat_id = int(f.readline())
with open(".credentials") as f:
dni = f.readline().strip()
password = f.readline()
if not dni.isdigit() or int(dni) < 1000000 or int(dni) > 99999999:
print("DNI incorrecto")
exit(1)
if len(password) < 8:
print("Contraseña incorrecta")
exit(1)
driver = init_driver(dni=dni, password=password)
status = load_status(driver=driver)
(oferta_table, notas_table, finales_table) = update_intraconsulta(
driver=driver, dni=dni, password=password
)
application = ApplicationBuilder().token(token).post_init(post_init).build()
oferta_handler = CommandHandler(
"oferta", partial(oferta, html=oferta_table, chat_id=chat_id)
)
notas_handler = CommandHandler(
"notas", partial(notas, html=notas_table, chat_id=chat_id)
)
finales_handler = CommandHandler(
"finales", partial(finales, html=finales_table, chat_id=chat_id)
)
application.add_handler(oferta_handler)
application.add_handler(notas_handler)
application.add_handler(finales_handler)
job_queue = application.job_queue
job_queue.run_repeating(
poll_miel,
interval=60,
first=0,
data={
"driver": driver,
"status": status,
"application": application,
"chatId": chat_id,
},
)
print("Bot listening to commands")
application.run_polling()