This repository has been archived by the owner on Sep 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawlers.py
384 lines (339 loc) · 16.5 KB
/
crawlers.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import json
import requests
from bs4 import BeautifulSoup
from sqlalchemy.orm import sessionmaker
from telegram import ParseMode
import db
import messages
import util
from proxy import headers, proxies
Session = sessionmaker(bind=db.gen_engine(db.get_database_url()))
def get_login(user, password):
session = requests.session()
try:
sapu = session.post('https://sapu.ucpel.edu.br/engine.php?class=LoginForm&method=onLogin', data={
"login": user,
"password": password,
}, headers=headers(), proxies=proxies())
return BeautifulSoup(sapu.content, 'html.parser')
except:
try:
sapu = session.post('https://sapu.ucpel.edu.br/engine.php?class=LoginForm&method=onLogin', data={
"login": user,
"password": password,
}, headers=headers(), proxies=proxies())
return BeautifulSoup(sapu.content, 'html.parser')
except:
return []
def get_login_completo(email, password):
try:
session = requests.session()
sapu = session.post('https://sapu.ucpel.edu.br/engine.php?class=LoginForm&method=onLogin', data={
"login": email,
"password": password,
}, headers=headers(), proxies=proxies())
soup = BeautifulSoup(sapu.content, 'html.parser')
home = session.get("https://sapu.ucpel.edu.br/index.php?class=Dashboard&message=1")
for index in soup.find_all('script'):
if str(index.get_text().lstrip()).split("'")[1] == "Erro":
return False, "", ""
else:
home_soup = BeautifulSoup(home.content, 'html.parser')
chave = str(home_soup.find(class_='div_matricula').get_text().lstrip()).split(" ")[1]
curso = util.formata_curso(str(home_soup.find(class_='div_curso').get_text().lstrip()).split(": ")[1])
return True, chave, curso
except:
session = requests.session()
sapu = session.post('https://sapu.ucpel.edu.br/engine.php?class=LoginForm&method=onLogin', data={
"login": email,
"password": password,
}, headers=headers(), proxies=proxies())
soup = BeautifulSoup(sapu.content, 'html.parser')
home = session.get("https://sapu.ucpel.edu.br/index.php?class=Dashboard&message=1")
for index in soup.find_all('script'):
if str(index.get_text().lstrip()).split("'")[1] == "Erro":
return False, "", ""
else:
home_soup = BeautifulSoup(home.content, 'html.parser')
chave = str(home_soup.find(class_='div_matricula').get_text().lstrip()).split(" ")[1]
curso = util.formata_curso(str(home_soup.find(class_='div_curso').get_text().lstrip()).split(": ")[1])
return True, chave, curso
def get_session(email, password):
try:
session = requests.session()
session.post('https://sapu.ucpel.edu.br/engine.php?class=LoginForm&method=onLogin', data={
"login": email,
"password": password,
}, headers=headers(), proxies=proxies())
session.get("https://sapu.ucpel.edu.br/index.php?class=Dashboard&message=1")
return session
except:
session = requests.session()
session.post('https://sapu.ucpel.edu.br/engine.php?class=LoginForm&method=onLogin', data={
"login": email,
"password": password,
}, headers=headers(), proxies=proxies())
session.get("https://sapu.ucpel.edu.br/index.php?class=Dashboard&message=1")
return session
def get_notas(user, bot):
try:
try:
session = get_session(user.sapu_username, user.sapu_password)
notas = session.get("https://sapu.ucpel.edu.br/engine.php?class=AvaliacaoFormList", headers=headers(), proxies=proxies())
except:
session = get_session(user.sapu_username, user.sapu_password)
notas = session.get("https://sapu.ucpel.edu.br/engine.php?class=AvaliacaoFormList", headers=headers(), proxies=proxies())
soup = BeautifulSoup(notas.content, 'html.parser')
if not str(soup.find('script').get_text().lstrip()).split("'")[1] == "Erro":
tdatagrid = soup.find_all(class_='tdatagrid_body')
count = 0
notas_resumo = []
resumo = []
for index in tdatagrid[0].find_all('td'):
resumo.append(index.get_text().lstrip())
count += 1
if count == 6:
notas_resumo.append(resumo)
resumo = []
count = 0
count = 0
materia = ""
materias = []
notas_detalhe = []
detalhe = []
for index in tdatagrid[1].find_all(class_='tdatagrid_group'):
materias.append(index.get_text().lstrip())
for index in tdatagrid[1].find_all('td'):
if str(index.get_text().lstrip() + "\n") in materias:
materia = index.get_text().lstrip()
else:
count += 1
detalhe.append(index.get_text().lstrip())
if count == 9:
if "(" not in detalhe[0]:
detalhe.append(materia)
detalhe.pop(0)
notas_detalhe.append(detalhe)
detalhe = []
count = 0
return notas_resumo, notas_detalhe
else:
return [], []
except Exception as e:
session = Session()
admins = session.query(db.Admins).all()
for admin in admins:
bot.send_message(chat_id=admin.user_id, text="<b>Erro</b>\n\n/{} | Erro: {}".format(user.telegram_id, e),
parse_mode=ParseMode.HTML)
session.close()
return [], []
def get_frequencia(user, bot):
try:
try:
session = get_session(user.sapu_username, user.sapu_password)
frequencia = session.get("https://sapu.ucpel.edu.br/engine.php?class=FrequenciaFormList", headers=headers(), proxies=proxies())
except:
session = get_session(user.sapu_username, user.sapu_password)
frequencia = session.get("https://sapu.ucpel.edu.br/engine.php?class=FrequenciaFormList", headers=headers(), proxies=proxies())
soup = BeautifulSoup(frequencia.content, 'html.parser')
if not str(soup.find('script').get_text().lstrip()).split("'")[1] == "Erro":
count = 0
table = []
td = []
for index in soup.find(class_='tdatagrid_body').find_all('td'):
td.append(index.get_text().lstrip())
count += 1
if count == 4:
table.append(td)
td = []
count = 0
return table
else:
return []
except Exception as e:
session = Session()
admins = session.query(db.Admins).all()
for admin in admins:
bot.send_message(chat_id=admin.user_id, text="<b>Erro</b>\n\n/{} | Erro: {}".format(user.telegram_id, e),
parse_mode=ParseMode.HTML)
session.close()
return []
def get_horarios(user):
session = get_session(user.sapu_username, user.sapu_password)
horarios = session.get("https://sapu.ucpel.edu.br/engine.php?class=HorarioFormList", headers=headers(), proxies=proxies())
soup = BeautifulSoup(horarios.content, 'html.parser')
if not str(soup.find('script').get_text().lstrip()).split("'")[1] == "Erro":
tdatagrid_body = soup.find(class_='tdatagrid_body')
dias = []
msg = "<b>Horários</b>"
for index in tdatagrid_body.find_all(class_='tdatagrid_group'):
dias.append([index.get_text().lstrip()[:-1], util.find_between(str(index), 'level="', '">')])
for dia, i in dias:
msg += "\n\n<b>{}</b>".format(str(dia).capitalize())
for index in tdatagrid_body.find_all(childof=i):
materia, inicio, fim, predio, sala = util.formata_horarios(index)
msg += messages.formata_horario(materia, inicio, fim, predio, sala)
if not dias:
msg = messages.horarios_empty(user.first_name)
else:
msg = messages.perfil_errado(user.first_name)
return msg
def get_disciplinas(user):
session = get_session(user.sapu_username, user.sapu_password)
disciplinas = session.get("https://sapu.ucpel.edu.br/engine.php?class=MatriculaFormList", headers=headers(), proxies=proxies())
soup = BeautifulSoup(disciplinas.content, 'html.parser')
if not str(soup.find('script').get_text().lstrip()).split("'")[1] == "Erro":
tdatagrid_body = soup.find(class_='tdatagrid_body')
table = []
td = []
count = 0
for index in tdatagrid_body.find_all('td'):
td.append(index.get_text().lstrip())
count += 1
if count == 5:
table.append(td)
td = []
count = 0
msg = util.formata_disciplinas(table)
if not table:
msg = messages.disciplinas_empty(user.first_name)
else:
msg = messages.perfil_errado(user.first_name)
return msg
def get_curriculo(user):
session = get_session(user.sapu_username, user.sapu_password)
curriculo = session.get("https://sapu.ucpel.edu.br/engine.php?class=MatrizCurricularFormList", headers=headers(), proxies=proxies())
soup = BeautifulSoup(curriculo.content, 'html.parser')
if not str(soup.find('script').get_text().lstrip()).split("'")[1] == "Erro":
tdatagrid_body = soup.find(class_='tdatagrid_body')
semestres = []
msg = "<b>Matriz Curricular</b>"
for index in tdatagrid_body.find_all(class_='tdatagrid_group'):
semestres.append([index.get_text().lstrip()[:-1], util.find_between(str(index), 'level="', '">')])
for semestre, i in semestres:
msg += "\n\n<b>{}</b>".format(str(semestre).capitalize())
for index in tdatagrid_body.find_all(childof=i):
materia, ch, link = util.formata_curriculo(index, session)
msg += messages.formata_curriculo(materia, ch, link)
else:
msg = messages.perfil_errado(user.first_name)
return msg
def get_historico(user):
session = get_session(user.sapu_username, user.sapu_password)
historico = session.get("https://sapu.ucpel.edu.br/engine.php?class=HistoricoFormList&method=imprimir", headers=headers(), proxies=proxies())
soup = BeautifulSoup(historico.content, 'html.parser')
if not str(soup.find('script').get_text().lstrip()).split("'")[1] == "Erro":
index = soup.find('script')
msg = messages.historico(str(index.get_text().lstrip()).split("'")[1])
else:
msg = messages.perfil_errado(user.first_name)
return msg
def get_moodle(user):
session = get_session(user.sapu_username, user.sapu_password)
historico = session.get("https://sapu.ucpel.edu.br/engine.php?class=LoginMoodle&method=index", headers=headers(), proxies=proxies())
soup = BeautifulSoup(historico.content, 'html.parser')
if not str(soup.find('script').get_text().lstrip()).split("'")[1] == "Erro":
index = soup.find('script')
return messages.formata_moodle(str(index.get_text().lstrip()).split("'")[1])
else:
msg = messages.perfil_errado(user.first_name)
return msg
def get_emails(user, args):
session = get_session(user.sapu_username, user.sapu_password)
historico = session.get("https://sapu.ucpel.edu.br/engine.php?class=MensagemForm&method=inbox", headers=headers(), proxies=proxies())
soup = BeautifulSoup(historico.content, 'html.parser')
if not str(soup.find('script').get_text().lstrip()).split("'")[1] == "Erro":
table = []
td = []
count = 0
for index in soup.find('table').find_all('td'):
if index.get_text().lstrip():
td.append(index.get_text().lstrip())
count += 1
if count > 2:
table.append(td)
count = 0
td = []
msg = util.formata_email(table, args)
else:
msg = messages.perfil_errado(user.first_name)
return msg
def get_boleto(user):
session = get_session(user.sapu_username, user.sapu_password)
boleto = session.get("https://sapu.ucpel.edu.br/engine.php?class=EmitirBoletoFormList", headers=headers(), proxies=proxies())
soup = BeautifulSoup(boleto.content, 'html.parser')
if not str(soup.find('script').get_text().lstrip()).split("'")[1] == "Erro":
try:
index = soup.find(class_='tdatagrid_body').find_all('td')[1].find('a')
url = util.find_between(str(index), "href=\"", "\">")
boleto = session.get("https://sapu.ucpel.edu.br/engine.php?class=EmitirBoletoFormList&method=onBoleto&target=1&key={}".format(url.split("key=")[1]), headers=headers(), proxies=proxies())
soup = BeautifulSoup(boleto.content, 'html.parser')
index = soup.find_all(language="JavaScript")
return str(index[2].get_text().lstrip()).split("'")[1], True
except:
return "Você não possui boletos em aberto", False
else:
msg = messages.perfil_errado(user.first_name)
return msg, False
def get_editais(quantidade):
session = requests.session()
editais = session.get("http://www.ucpel.edu.br/portal/?secao=com_editais", headers=headers(), proxies=proxies())
soup = BeautifulSoup(editais.content, 'html.parser')
count = 0
msg = "<b>Editais</b>\n"
for index in soup.find(id='table').find_all(class_='line link'):
count += 1
msg += messages.editais(index.get_text().lstrip()[:-1], str(index).split("'")[1])
if count >= quantidade:
break
return msg
def get_noticias(first=False):
session = requests.session()
editais = session.get("http://www.ucpel.edu.br/portal/?secao=noticias", headers=headers(), proxies=proxies())
soup = BeautifulSoup(editais.content, 'html.parser')
if first:
index = soup.find(class_='not_block')
url = util.find_between(str(index), "href=\"", "\">").replace("amp;", "")
titulo = index.find(class_='not_titulo').get_text().lstrip()
return messages.ultima_noticia(url, titulo)
else:
msg = "<b>Notícias\n</b>"
for index in soup.find_all(class_='not_block'):
url = util.find_between(str(index), "href=\"", "\">").replace("amp;", "")
data = index.find(class_='not_data').get_text().lstrip()
titulo = index.find(class_='not_titulo').get_text().lstrip()
msg += messages.noticia(data, titulo, url)
return msg
def get_minhabiblioteca(user):
session = get_session(user.sapu_username, user.sapu_password)
editais = session.get("https://sapu.ucpel.edu.br/engine.php?class=BibliotecaService&method=loginUser&static=1", headers=headers(), proxies=proxies())
soup = BeautifulSoup(editais.content, 'html.parser')
response = json.loads(str(soup))
return response['message']
def get_atestado_simples(user):
session = get_session(user.sapu_username, user.sapu_password)
boleto = session.get("https://sapu.ucpel.edu.br/engine.php?class=MatriculaFormList&method=imprimirSimples", headers=headers(), proxies=proxies())
soup = BeautifulSoup(boleto.content, 'html.parser')
if not str(soup.find('script').get_text().lstrip()).split("'")[1] == "Erro":
index = soup.find_all('script')
return str(index[1].get_text().lstrip()).split("'")[1]
else:
return "ERRO"
def get_atestado_completo(user):
session = get_session(user.sapu_username, user.sapu_password)
boleto = session.get("https://sapu.ucpel.edu.br/engine.php?class=MatriculaFormList&method=imprimirCompleto", headers=headers(), proxies=proxies())
soup = BeautifulSoup(boleto.content, 'html.parser')
if not str(soup.find('script').get_text().lstrip()).split("'")[1] == "Erro":
index = soup.find_all('script')
return str(index[1].get_text().lstrip()).split("'")[1]
else:
return "ERRO"
def get_atestado_apto(user):
session = get_session(user.sapu_username, user.sapu_password)
boleto = session.get("https://sapu.ucpel.edu.br/engine.php?class=MatriculaFormList&method=imprimirRematricula", headers=headers(), proxies=proxies())
soup = BeautifulSoup(boleto.content, 'html.parser')
if not str(soup.find('script').get_text().lstrip()).split("'")[1] == "Erro":
index = soup.find_all('script')
return str(index[1].get_text().lstrip()).split("'")[1]
else:
return "ERRO"