Skip to content

Instantly share code, notes, and snippets.

@javierhonduco
Created November 23, 2016 10:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javierhonduco/96533fbd69a5a4a3b48e758045bc07e9 to your computer and use it in GitHub Desktop.
Save javierhonduco/96533fbd69a5a4a3b48e758045bc07e9 to your computer and use it in GitHub Desktop.
classroom_stats.py
import requests
import json
from bs4 import BeautifulSoup
config = {
'login_url': 'https://carpa.aig.uc3m.es/validacion.asp',
'user': '',
'password': ''
}
classrooms = {
'biblio': 'https://carpa.aig.uc3m.es/OcupacionTR/OcupacionTR.asp?campus=B&edificio=Po&nombrecampus=Biblioteca&nombreedificio=Polit%E9cnica&nombreaula=Todas&descrip=Ocupaci%F3n+de+las+aulas',
'juan_benet': 'https://carpa.aig.uc3m.es/OcupacionTR/OcupacionTR.asp?campus=L&edificio=07&nombrecampus=Legan%E9s&nombreedificio=Juan%20Benet&nombreaula=Todas&descrip=Ocupaci%F3n+de+las+aulas',
'torres_quevedo': 'https://carpa.aig.uc3m.es/OcupacionTR/OcupacionTR.asp?campus=L&edificio=04&nombrecampus=Legan%E9s&nombreedificio=Torres%20Quevedo&nombreaula=Todas&descrip=Ocupaci%F3n+de+las+aulas',
'betancourt': 'https://carpa.aig.uc3m.es/OcupacionTR/OcupacionTR.asp?campus=L&edificio=01&nombrecampus=Legan%E9s&nombreedificio=Agust%EDn%20de%20Betancourt&nombreaula=Todas&descrip=Ocupaci%F3n+de+las+aulas',
'aggr': 'https://carpa.aig.uc3m.es/EstadisticaDiaria/estadistica_diaria.asp?mes=5&anio=2015&nombrecampus=Todos&nombreedificio=Todos&nombreaula=Todas&descrip=Estad%EDstica+diaria+de+uso'
}
payload = {'Usuario': config['user'], 'Clave': config['password']}
class Occupation:
def __init__(self, payload):
self.s = requests.Session()
self.s.post(config['login_url'], data=payload)
def retrieve_occupation(self, url):
response = self.s.get(url)
return response.text
def parse_table(self, html):
table = []
soup = BeautifulSoup(html.encode('iso-8859-1'), 'html.parser')
for tables in soup.find_all('table'):
trs_t = []
for trs in tables.find_all('tr'):
tds_t = []
for tds in trs.find_all('td'):
if not tds.text.startswith('\n'):
tds_t.append(tds.text)
trs_t.append(filter(None, tds_t))
table.append(filter(None, trs_t))
return filter(None, table)
def hashify(self, table):
aulas = table[1][1:]
datos = []
for aula in aulas:
datos.append({
aula[0]: {
'libres': int(aula[1]),
'total': int(aula[1])+int(aula[2])
}
})
hash = {
'campus': table[0][1][0],
'edificio': table[0][1][1],
'aula': table[0][1][2],
'aulas': datos
}
return hash
if __name__ == '__main__':
oc = Occupation(payload)
result = oc.retrieve_occupation(classrooms['torres_quevedo'])
parsed_table = oc.parse_table(result)
hash = oc.hashify(parsed_table)
print json.dumps(hash)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment