Commit ebdcd614 authored by Maxime Bombar's avatar Maxime Bombar

Fix everything, it works git add main.py

parent 9857a06d
......@@ -7,6 +7,7 @@ Dependencies :
- icalendar
"""
import sys
import bs4 as BeautifulSoup
import urllib3
......@@ -25,34 +26,52 @@ today = datetime.datetime.strptime(
).strftime('%y-%m-%d')
def get_string(tag):
return tag.string
def get_schedule(a):
"""
returns a list of the schedule for the 5 days of the week
from schedule website
"""
sched = [[]]*5 # 5 days
for i in range(5):
sched[i] = a[i*36:(i+1)*36]
return(sched)
schedule = []
for day in a:
schedule.append(day.find_all("big"))
return schedule
def get_string(tag):
""" Get string from BeautifulSoup tag, itering on children if there are more than 1"""
if len(list(tag.children)) > 1:
s = ''
for x in tag.children:
if x.string.replace('\xa0', ''):
s += x.string
s += ' '
else:
s = tag.string
return s
def pretty_schedule(schedule):
"""Prettify the schedule"""
days = []
for day in schedule:
cls = []
for i in range(len(day)):
try:
if regex.match(day[i].string):
cls.append(day[i:i+6])
except: # schedule bad format
pass
i = 0
while i<len(day):
j = 1
s = get_string(day[i])
if s and regex.match(s) or s == 'Problème': # Beginning of a class
tmp = [day[i]]
try:
while get_string(day[i+j]) and not regex.match(get_string(day[i+j])):
if get_string(day[i+j]).replace('\xa0', ''):
tmp.append(day[i+j])
j+=1
except IndexError:
pass
cls.append(tmp)
i+=j
days.append(cls)
return format(days)
def format(days):
"""
days: list of list.
......@@ -65,29 +84,30 @@ def format(days):
for cls in days[i]:
clas = {}
try:
clas['time'] = cls[0].string
except:
pass
try:
clas['type'] = cls[1].string
clas['time'] = get_string(cls[0])
except:
pass
try:
clas['topic'] = cls[2].string
clas['type'] = get_string(cls[1])
except:
pass
try:
clas['prof'] = cls[3].string
clas['topic'] = get_string(cls[2])
except:
pass
try:
clas['room'] = cls[4].string
clas['prof'] = get_string(cls[3])
except:
pass
try:
clas['title'] = cls[5].string.replace('č', 'è')
clas['room'] = get_string(cls[4])
except:
pass
for i in range(len(cls[5:])):
try:
clas['title_%s' % i] = get_string(cls[5:][i]).replace('č', 'è')
except:
pass
day.append(clas)
pretty_days.append(day)
return pretty_days
......@@ -108,12 +128,32 @@ def pretty_print(days, date):
print(" %s:" % day_date)
print("----------------------------------")
for clas in days[i]:
print(clas['time'])
print(clas['type'])
print(clas['topic'])
print(clas['prof'])
print(clas['room'])
print(clas['title'])
try:
print(clas['time'])
except KeyError:
pass
try:
print(clas['type'])
except KeyError:
pass
try:
print(clas['topic'])
except KeyError:
pass
try:
print(clas['prof'])
except KeyError:
pass
try:
print(clas['room'])
except KeyError:
pass
try:
for cat in clas:
if 'title' in cat:
print(clas[cat])
except KeyError:
pass
print("\n")
print('\n')
......@@ -161,22 +201,12 @@ def fill_calendar(c, days, date):
except:
pass
summary = ''
try:
summary += clas['type'] + '\n'
except:
pass
try:
summary += clas['topic'] + '\n'
except:
pass
try:
summary += clas['prof'] + '\n'
except:
pass
try:
summary += clas['title'] + '\n'
except:
pass
for cat in clas:
if cat not in ['time', 'room']:
try:
summary += clas[cat] + '\n'
except:
pass
e.add('summary', summary)
......@@ -187,11 +217,23 @@ def fill_calendar(c, days, date):
c.add_component(e)
def parse_schedule(request):
"""
Parse the schedule found in the request
"""
html = request.data
soup = BeautifulSoup.BeautifulSoup(html, 'lxml') # Create the soup
a = soup.find_all('table', cellspacing="0")[2:]
schedule = get_schedule(a)
days = pretty_schedule(schedule)
return days
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Fetch calendar from agreg.cmla.ens-cachan.fr', add_help=True)
parser.add_argument(
"-d", "--date",
help="From",
help="Date From in format yy-mm-dd",
action="store",
default = today
)
......@@ -214,9 +256,22 @@ if __name__ == '__main__':
action="store_true",
)
parser.add_argument(
"-o", "--option",
help="specifies the option. Available are pr, cs, ae and in",
action="store",
default = "in",
)
options = ["pr", "cs", "ae", "in"]
args = parser.parse_args()
if args.option not in options:
parser.print_help(sys.stderr)
sys.exit(42)
date = datetime.datetime.strptime(
datetime.datetime.strptime(
args.date, '%y-%m-%d'
......@@ -228,17 +283,9 @@ if __name__ == '__main__':
if not args.all:
date = date.strftime('%y-%m-%d')
request = http.request('GET', 'http://agreg.cmla.ens-cachan.fr/edt.php?option=in&sem=%s&suff=' % (date,))
html = request.data
soup = BeautifulSoup.BeautifulSoup(html, 'lxml') # Create the soup
a = soup.find_all('big')[57:] # Fetch "Big" tags, begins from first class on Monday
schedule = get_schedule(a)
days = pretty_schedule(schedule)
request = http.request('GET', 'http://agreg.cmla.ens-cachan.fr/edt.php?option=%(option)s&sem=%(date)s&suff=' % {'option': args.option, 'date': date} )
days = parse_schedule(request)
if args.pretty:
pretty_print(days, date)
......@@ -254,18 +301,9 @@ if __name__ == '__main__':
while date.month < 7 or date.year == 2018:
date_str = date.strftime('%y-%m-%d')
request = http.request('GET', 'http://agreg.cmla.ens-cachan.fr/edt.php?option=in&sem=%s&suff=' % (date_str,))
html = request.data
soup = BeautifulSoup.BeautifulSoup(html, 'lxml') # Create the soup
a = soup.find_all('big')[57:] # Fetch "Big" tags, begins from first class on Monday
schedule = get_schedule(a)
request = http.request('GET', 'http://agreg.cmla.ens-cachan.fr/edt.php?option=%(option)s&sem=%(date)s&suff=' % {'option': args.option, 'date': date_str} )
days = pretty_schedule(schedule)
days = parse_schedule(request)
fill_calendar(c, days, date_str)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment