Skip to content

Instantly share code, notes, and snippets.

@JosephTico
Last active September 14, 2020 06:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JosephTico/c15074d27434ae4c1d2ea1ef1d0420e6 to your computer and use it in GitHub Desktop.
Save JosephTico/c15074d27434ae4c1d2ea1ef1d0420e6 to your computer and use it in GitHub Desktop.
Convierte su calendario del TEC Digital a formato iCalendar (compatible con Google Calendar y cualquier otra app de calendario). Debe rellenar las variables USER y PASS con sus datos de inicio de sesión. Necesita instalar las siguientes bibliotecas de Python: requests, ics, beautifulsoup4
import sys
import datetime
import requests
from bs4 import BeautifulSoup
from ics import Calendar, Event
import arrow
def td_login(username, password):
# Obtiene los tokens de login iniciales
initial_request = requests.get('https://tecdigital.tec.ac.cr/register/?return_url=%2fdotlrn%2f')
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')
hash = soup.find('input', {'name': 'hash'}).get('value')
# Ahora sí hace el request del login
data = f"form%3Aid=login&return_url=%2Fdotlrn%2F&time={time}&token_id={token_id}&hash={hash}&retoken=allow&username={username}&password={password}"
session = requests.Session()
response = session.post('https://tecdigital.tec.ac.cr/register/',data=data, allow_redirects=False)
return session
# Rellenar sus datos acá
USER = ""
PASS = ""
# Verifica inicio de sesión correcto
SESSION = td_login(USER, PASS)
try:
r = SESSION.get("https://tecdigital.tec.ac.cr/dotlrn/?date=" + date.strftime("%Y-%m-%d") + "&view=list&page_num=1&period_days=30",
allow_redirects=False)
except requests.exceptions.RequestException as e:
raise SystemExit(e)
if r.status_code != 200:
sys.exit("Los datos son incorrectos o el TEC Digital está caído.")
# Parsea eventos del HTML
events = []
soup = BeautifulSoup(r.content, features="lxml")
table = soup.find('table', attrs={'class':'list-table'})
table_body = table.find('tbody')
rows = table_body.find_all('tr')
for row in rows:
cols = row.find_all('td')
cols = [ele.text.strip() for ele in cols]
events.append([ele for ele in cols if ele]) # Get rid of empty values
# Crea el iCal
cal = Calendar()
for event_data in events:
e = Event()
e.name = f"{event_data[2]} - {event_data[3]}"
e.description = event_data[4]
date = arrow.get(event_data[0], "DD MMMM YYYY", locale="es").replace(tzinfo='America/Costa_Rica')
e.begin = date
if event_data[1] == "Evento para todo el día":
e.make_all_day()
else:
e.begin = arrow.get(event_data[0] + " " + event_data[1][0:5], "DD MMMM YYYY HH:mm", locale="es").replace(tzinfo='America/Costa_Rica')
e.end = arrow.get(event_data[0] + " " + event_data[1][8:], "DD MMMM YYYY HH:mm", locale="es").replace(tzinfo='America/Costa_Rica')
cal.events.add(e)
print(cal)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment