-
Notifications
You must be signed in to change notification settings - Fork 0
/
uc3mics
executable file
·99 lines (80 loc) · 2.76 KB
/
uc3mics
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2020 Daniel Alcaide Nombela [email protected]
# This script is under MIT license
#
# Version: 2020.09.11
# You can find new versions and fixes of this script over the time at
# https://github.com/danoloan10/uc3mics
import http.cookiejar as cookielib
from bs4 import BeautifulSoup as bs
import cgi
import mechanize
import os
import getpass
import sys
import base64
# uncomment for debug
# import cgitb
# cgitb.enable(display=0, logdir="/var/log/uc3mics"))
print("Content-Type: text/calendar\r\n\r\n")
user = None # NIA
passwd = None # passwd
route = None # route to calendar
# get user and passwd from 'Authorization' header, in base64 (ICSx⁵ style)
if 'HTTP_AUTHORIZATION' in os.environ:
auth = os.environ['HTTP_AUTHORIZATION'].split(" ")[1]
auth = auth.encode("ascii")
auth = base64.b64decode(auth)
auth = auth.decode("ascii")
user = auth.split(":")[0]
passwd = auth.split(":")[1]
# build route
form = cgi.FieldStorage()
# these values are the ones used in the URL of the ICS file in:
# https://aplicaciones.uc3m.es/horarios-web/alumno/alumno.page
exp = form.getvalue('exp')
per = form.getvalue('per')
if exp is not None and per is not None:
route = 'https://aplicaciones.uc3m.es/horarios-web/alumno/verHorario.page?exp='+exp+'&per='+per+'&fmt=ics'
# interactive shell
if user is None:
user = input("Enter NIA: ")
if passwd is None:
passwd = getpass.getpass(prompt="Enter password: ")
if route is None:
route = input("Enter route: ")
# Magic copied from https://github.com/tairosonloa/Aula_Global_UC3M
# Copyright (C) 2018 Aitor Alonso at [email protected]
# This script is under MIT license
# Set the browser for the web crawler
br = mechanize.Browser()
cookiejar = cookielib.LWPCookieJar()
br.set_cookiejar(cookiejar)
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time = 1)
br.addheaders = [( 'User-agent', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36' )]
br.open(route)
# Submit login form
br.select_form(nr=0)
br.form['adAS_username'] = user
br.form['adAS_password'] = passwd
br.submit(id="submit_ok")
# Check if success
url = br.open(route)
login = url.get('X-Frame-Options', None)
if login is not None:
status, _ = cgi.parse_header(login)
if status.upper() == "DENY":
print("Login failed. Check your NIA and password and try again")
exit(1)
# Donwload file and get filename from response header
cdheader = url.get('Content-Disposition', None)
if cdheader is not None:
print(url.read().decode("utf-8"))
else:
not_downloaded.append((href, h1))