blob: cffd88097ab2af24262562cece362b867220ff7d [file] [log] [blame]
import requests
from bs4 import BeautifulSoup
import pyodbc
import re
from datetime import datetime, timedelta
import pytz
class TableParser:
TIMEZONE = 'Europe/Madrid'
EMPTY_CELL_CLASSES = ['row_labels', 'even_row', 'odd_row']
def __init__(self, baseUrl):
self.baseUrl = baseUrl
def parse(self, year, month, day, area, db = None):
url = self.baseUrl
params = {
'year': year,
'month': month,
'day': day,
'area': area
}
page = requests.get(url, params=params)
soup = BeautifulSoup(page.content, 'html.parser')
table = soup.find(id="day_main")
hores = []
implicitClasses = []
for h in range(8,22):
newhour = ""
if h < 10:
newhour += "0"
newhour += str(h)
hores.append(newhour + ":00")
implicitClasses.append([])
hores.append(newhour + ":30")
implicitClasses.append([])
p = re.compile(r"Aula (\S+) ?\(\d*\)", re.IGNORECASE)
horaActual = 0
for hora in hores:
td_hora = table.find(text=hora).findNext('td')
column = 1
while hora not in td_hora.get_text():
while column in implicitClasses[horaActual]:
column += 1
classes = td_hora['class'];
if td_hora.has_attr('class') and not td_hora['class'][0] in self.EMPTY_CELL_CLASSES:
assignatura = td_hora.get_text().strip()
aulaRaw = table.find_all("th")[column].get_text().strip()
aula = p.match(aulaRaw).group(1)
files = int(td_hora.get("rowspan"))
durada = files*30
timeSplit = hora.split(':')
beginsDateTime = datetime(year, month, day, int(timeSplit[0]), int(timeSplit[1]))
beginsDateTime = pytz.timezone(self.TIMEZONE).localize(beginsDateTime)
begins = int(beginsDateTime.timestamp())
endsDateTime = beginsDateTime + timedelta(minutes=durada)
ends = int(endsDateTime.timestamp())
print(("Afegint " if db != None else "") + assignatura
+ ", " + hora
+ ", " + str(durada) + "mins"
+ ", " + aula)
if db != None:
cursor1 = db.cursor()
cursor1.execute("SELECT id FROM classes WHERE calendar_name = ? AND room = ? AND begins = ? AND ends = ?",
assignatura, aula, begins, ends)
row = cursor1.fetchone()
if row:
print("[WARNING] Ja estava a la DB (id " + str(row.id) + ")")
else:
cursor2 = db.cursor()
cursor2.execute("INSERT INTO classes (calendar_name, room, begins, ends) VALUES (?, ?, ?, ?)",
assignatura, aula, begins, ends)
for i in range(1, files - 1):
if i < len(implicitClasses):
implicitClasses[horaActual + i].append(column)
td_hora = td_hora.findNext('td')
column = column + 1
horaActual = horaActual + 1
if db != None:
db.commit()