-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmatricula.py
177 lines (146 loc) · 5.95 KB
/
matricula.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
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
import json
import time
URL_LOGIN = "https://pre.ufcg.edu.br:8443/ControleAcademicoOnline/"
URL_HORARIO = "https://pre.ufcg.edu.br:8443/ControleAcademicoOnline/Controlador?command=AlunoHorarioConfirmar&ano=2019&periodo=1"
URL_MATRICULA = "https://pre.ufcg.edu.br:8443/ControleAcademicoOnline/Controlador?command=AlunoMatriculaGetForm"
TEMPO_DE_ESPERA = 5
def clear_console():
os.system("cls" if os.name == "nt" else "clear")
def ler_do_json():
with open("dados.json", encoding="utf-8") as json_file:
dados = json.load(json_file)
for d in dados:
matricula = d["matricula"]
senha = d["senha"]
horarioDeAbertura = d["horarioDeAbertura"]
disciplinas = d["disciplinas"]
horarioDeAbertura = time.strptime(horarioDeAbertura, "%H:%M:%S")
return matricula, senha, horarioDeAbertura, disciplinas
class ScriptMatricula:
def __init__(self, matricula, senha, horario_de_abertura, disciplinas_desejadas):
self.matricula = matricula
self.senha = senha
self.horario_de_abertura = horario_de_abertura
self.disciplinas_desejadas = disciplinas_desejadas
self.browser = webdriver.Chrome()
self.codigos_e_posicoes = {}
def faz_login(self):
user = self.browser.find_element("name", "login")
password = self.browser.find_element("name", "senha")
user.clear()
user.send_keys(self.matricula)
password.clear()
password.send_keys(self.senha)
self.browser.find_element("xpath", ".//*[contains(text(), 'OK')]").click()
def faz_logout(self):
XPathBotaoDeSair = '//*[@id="menu"]/ul/li[6]/a'
botaoDeSair = self.browser.find_element("xpath", XPathBotaoDeSair)
botaoDeSair.click()
def aguarda_horario(self):
while horarioDeAbertura > self.pega_horario():
clear_console()
print("⏳ Esperando horário!")
print("⏲️ Pausa de {} segundos...".format(TEMPO_DE_ESPERA))
time.sleep(TEMPO_DE_ESPERA)
def pega_horario(
self,
):
self.browser.get(URL_HORARIO)
xPathData = '//*[@id="conteudo"]/div[4]/div[2]'
infoHora = str(
self.browser.find_element("xpath", xPathData).get_attribute("innerHTML")
)[20:]
horarioAtual = time.strptime(infoHora, "%H:%M:%S")
return horarioAtual
def verifica_abertura(self):
while True:
self.browser.get(URL_MATRICULA)
if (
self.browser.find_element(
"xpath",
'//*[@id="conteudo"]/div[1]'
).get_attribute("class")
== "alert alert-danger"
):
clear_console()
print("❗ Ainda não abriu!")
else:
print("✨ Abriu!")
break
self.faz_logout()
print("⏲️ Pausa de {:.2f} segundos...".format(TEMPO_DE_ESPERA // 3))
time.sleep(TEMPO_DE_ESPERA // 3)
self.faz_login()
def localizador_de_disciplinas(self):
self.browser.refresh()
print("⏳ Lendo as disciplinas da página")
for contador in range(1, 400):
try:
xpath = '//*[@id="tabOferta"]/tbody/tr[{Contador}]/td[2]'.format(
Contador=contador
)
codigo_e_turma_da_disciplina = str(
self.browser.find_element("xpath", xpath).get_attribute("innerHTML")
)
codigo_e_turma_da_disciplina = (
str( # Verificar se precisa desse cast para str
codigo_e_turma_da_disciplina[1:13]
)
)
self.codigos_e_posicoes[codigo_e_turma_da_disciplina] = str(contador)
except:
print("⌛ Foram lidas {} disciplinas".format(contador))
break
def seleciona_disciplinas_desejadas(self):
matriculadas = 0
while matriculadas == 0:
self.localizador_de_disciplinas()
clear_console()
for codigo in disciplinas_para_matricular.keys():
try:
linha = self.codigos_e_posicoes[codigo]
xpath = '//*[@id="tabOferta"]/tbody/tr[{}]/td[6]/input'.format(
linha
)
self.browser.find_element("xpath", xpath).click()
matriculadas += 1
print(
"✅ Selecionado a disciplina {}".format(
disciplinas_para_matricular[codigo]
)
)
except:
print(
"❌ Não foi possível se matricular na disciplina {}".format(
disciplinas_para_matricular[codigo]
)
)
def clica_matricular(self):
self.browser.find_element(
"xpath",
'//*[@id="conteudo"]/form/div[3]/input[3]'
).click()
def faz_matricula(self):
# Acessa a página de login
self.browser.get(URL_LOGIN)
# Faz login
self.faz_login()
# Aguarda o horário da abertura
self.aguarda_horario()
# Acessa a página de registrar e aguarda abertura
self.verifica_abertura()
# Lê e seleciona as disciplinas desejadas na página
self.seleciona_disciplinas_desejadas()
# Clica no botão de fazer matrícula
self.clica_matricular()
# Imprime "Matriculado!"
print("🎆 Matriculado!")
# Carrega informações do json.
matricula, senha, horarioDeAbertura, disciplinas_para_matricular = ler_do_json()
scriptMatricula = ScriptMatricula(
matricula, senha, horarioDeAbertura, disciplinas_para_matricular
)
scriptMatricula.faz_matricula()