-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
260 lines (187 loc) · 8.48 KB
/
app.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
import os
import sys
import datetime
from base64 import b64encode, b64decode
import requests
import re
import threading
import urllib.request
import getpass
import zipfile
from bs4 import BeautifulSoup
from progress.spinner import PixelSpinner
from progress.bar import Bar
from tqdm import tqdm
from os import environ
session = requests.Session()
globalError = False
semestres_final = []
dirname = os.getcwd()
class DownloadProgressBar(tqdm):
def update_to(self, b=1, bsize=1, tsize=None):
if tsize is not None:
self.total = tsize
self.update(b * bsize - self.n)
def download_url(url, output_path):
with DownloadProgressBar(unit='B', unit_scale=True,
miniters=1, desc=url.split('/')[-1]) as t:
urllib.request.urlretrieve(url, filename=output_path, reporthook=t.update_to)
def print_error(msg):
global globalError
globalError = True
print("\n\nERROR: " + msg)
sys.exit(2)
def td_login(username, password):
global session
# Obtiene los tokens de login iniciales
initial_request = requests.get('https://tecdigital.tec.ac.cr/register/?return_url=%2fdotlrn%2f', timeout=10)
try:
soup = BeautifulSoup(initial_request.content, features='lxml')
time = soup.find('input', {'name': 'time'}).get('value')
token_id = soup.find('input', {'name': 'token_id'}).get('value')
tdhash = soup.find('input', {'name': 'hash'}).get('value')
except:
print_error('No se ha podido iniciar sesión, el TEC Digital debe estar caído. Por favor inténtelo de nuevo más tarde.')
# Ahora sí hace el request del login
data = f'form%3Aid=login&return_url=%2Fdotlrn%2F&time={time}&token_id={token_id}&hash={tdhash}&retoken=allow&username={username}&password={password}'
session.post('https://tecdigital.tec.ac.cr/register/', data=data, allow_redirects=False, timeout=10)
# Verifica login
response = session.get('https://tecdigital.tec.ac.cr/dotlrn/courses', allow_redirects=False, timeout=10)
if response.status_code != 200:
print_error('Los datos son incorrectos o el TEC Digital está caído.')
return session
def obtener_cursos():
global session, globalError, semestres_final
base = "https://tecdigital.tec.ac.cr"
cursos_page = session.get('https://tecdigital.tec.ac.cr/dotlrn/courses', allow_redirects=True, timeout=10)
soup = BeautifulSoup(cursos_page.content, features='lxml')
cursos_html = soup.select_one("#main-content .portlet ul")
semestres_html = cursos_html.find_all("li")
for semestre in tqdm(semestres_html):
titulo_semestre = semestre.find(text=True, recursive=False).strip()
if "2020" in titulo_semestre or not titulo_semestre:
continue
cursos = []
cursos_soup = semestre.find_all("li")
for curso in cursos_soup:
titulo_curso = curso.text.strip()
url = base + curso.find("a", href=True)['href'].strip() + "file-storage"
folder_page = session.get(url, allow_redirects=True, timeout=10)
regex = re.compile(r"\$rootScope\.GL_FOLDER_ID = ([0-9]*);")
folder_id = re.search(regex, folder_page.content.decode('utf-8')).group(1)
cursos.append({"titulo": titulo_curso, "url": url, "folder_id": folder_id})
data_semestre = {"titulo": titulo_semestre, "cursos": cursos}
semestres_final.append(data_semestre)
# Obtenido de https://stackoverflow.com/questions/3041986/apt-command-line-interface-like-yes-no-input
def query_yes_no(question, default="yes"):
"""Ask a yes/no question via raw_input() and return their answer.
"question" is a string that is presented to the user.
"default" is the presumed answer if the user just hits <Enter>.
It must be "yes" (the default), "no" or None (meaning
an answer is required of the user).
The "answer" return value is True for "yes" or False for "no".
"""
valid = {"yes": True, "y": True, "ye": True,
"no": False, "n": False}
if default is None:
prompt = " [y/n] "
elif default == "yes":
prompt = " [Y/n] "
elif default == "no":
prompt = " [y/N] "
else:
raise ValueError("invalid default answer: '%s'" % default)
while True:
sys.stdout.write(question + prompt)
choice = input().lower()
if default is not None and choice == '':
return valid[default]
elif choice in valid:
return valid[choice]
else:
sys.stdout.write("Responda con 'yes' o 'no' "
"(o 'y' o 'n').\n")
def cli_login():
global session, globalError, semestres_final
art = """
_____ __ __ __ ___ ___ __ __ ________ __
||__ / ` | \|/ _`|| /\ | |__ \_/|__)/ \|__)||__ |__)
||___\__, |__/|\__>||/~~\|___ |___/ \| \__/| \||___| \ """
print(art)
print("Exportador de archivos del TEC Digital")
print("Creado por Joseph Vargas - https://twitter.com/JosephTico\n\n")
print("Ingrese sus credenciales del TEC Digital y presione Enter.")
if "TEC_USERNAME" in environ:
username = environ.get('TEC_USERNAME')
else:
username = input("Usuario: ").strip()
if "TEC_PASSWORD" in environ:
password = environ.get('TEC_PASSWORD')
else:
password = getpass.getpass("Contraseña: ")
spinner = PixelSpinner('Iniciando sesión... ')
thread = threading.Thread(target=td_login,args=(username,password))
thread.start()
while thread.is_alive() and globalError == False:
spinner.next()
thread.join()
if globalError:
return
print("\n")
print('Obteniendo cursos... ')
thread = threading.Thread(target=obtener_cursos)
thread.start()
thread.join()
if globalError:
return
print("\n")
print("Se han cargado satisfactoriamente los siguientes cursos:")
for semestre in semestres_final:
print("# " + semestre["titulo"])
for curso in semestre["cursos"]:
print("-- " + curso["titulo"])
print("\n")
if "AUTO_DOWNLOAD" not in environ and not query_yes_no("¿Desea iniciar la descarga de todos los archivos en la carpeta actual?"):
return
for semestre in semestres_final:
print("Descargando cursos de " + semestre["titulo"] + "...")
if not os.path.exists(semestre["titulo"]):
os.makedirs(semestre["titulo"])
for curso in semestre["cursos"]:
for attempt in range(5):
try:
print("Descargando archivos de " + curso["titulo"] + "...")
url = curso["url"] + "/download-archive?object_id=" + curso["folder_id"]
response = session.get(url, stream=True)
total_size_in_bytes= int(response.headers.get('content-length', 0))
block_size = 1024 #1 Kibibyte
progress_bar = tqdm(total=total_size_in_bytes, unit='iB', unit_scale=True)
filename = os.path.join(dirname, semestre["titulo"], curso["titulo"] + ".zip")
with open(filename, 'wb') as file:
for data in response.iter_content(block_size):
progress_bar.update(len(data))
file.write(data)
if total_size_in_bytes != 0 and progress_bar.n != total_size_in_bytes:
raise Exception('Error al descargar el archivo.')
try:
with zipfile.ZipFile(filename,"r") as zip_ref:
zip_ref.extractall(os.path.join(dirname, semestre["titulo"]))
except:
print("\nHa ocurrido un error al descomprimir los contenidos de este curso. Se mantendrá el archivo comprimido")
else:
os.remove(filename)
if os.path.exists(filename):
os.remove(filename)
progress_bar.close()
except KeyboardInterrupt:
sys.exit()
except:
print("\n\nERROR: Error al descargar el archivo. \nReintentando...")
else:
break
else:
print("Ha ocurrido un error al descargar el curso " + curso["titulo"] + ".\nSaltando...")
print("\n")
print("Proceso finalizado.")
if __name__ == '__main__':
cli_login()