Commit 9913eb92 authored by Maxime Bombar's avatar Maxime Bombar

POC, with parser and pretty print

parents
#!/usr/bin/python3
#-*- mode:python3 -*-
"""
Dependencies :
- BeautifulSoup
- icalendar
"""
import bs4 as BeautifulSoup
import urllib3
import datetime
import icalendar
import argparse
# today's week beginning date
today = datetime.datetime.strptime(
datetime.datetime.now().strftime('%y-W%W') + '-1', "%y-W%W-%w"
).strftime('%y-%m-%d')
def get_string(tag):
return tag.string
def format(days):
"""
days: list of list.
Each day is a sublist which contains the classes for this day.
Format everything from BeautifulSoup tags to strings, into a list of list of dict
"""
pretty_days = []
for i in range(len(days)):
day = []
for cls in days[i]:
clas = {}
clas['time'] = cls[0].string
clas['type'] = cls[1].string
clas['topic'] = cls[2].string
clas['prof'] = cls[3].string
clas['room'] = cls[4].string
clas['title'] = cls[5].string.replace('č', 'è')
day.append(clas)
pretty_days.append(day)
return pretty_days
def pretty_print(days, date):
"""
Display the week schedule
"""
for i in range(1,6):
day_date = datetime.datetime.strptime(
datetime.datetime.strptime(
date, '%y-%m-%d'
).strftime('%y-W%W') + '-%s' % i, '%y-W%W-%w'
).strftime("%A %B %d, %Y")
print("----------------------------------")
print(" %s:" % day_date)
print("----------------------------------")
for clas in days[i-1]:
print(clas['time'])
print(clas['type'])
print(clas['topic'])
print(clas['prof'])
print(clas['room'])
print(clas['title'])
print("\n")
print('\n')
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",
action="store",
default = today
)
args = parser.parse_args()
date = datetime.datetime.strptime(
datetime.datetime.strptime(
args.date, '%y-%m-%d'
).strftime('%y-W%W') + '-1', "%y-W%W-%w").strftime('%y-%m-%d')
http = urllib3.PoolManager()
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
def get_schedule(a):
"""
returns a list of the schedule for the 5 days of the week
"""
sched = [[]]*5 # 5 days
for i in range(5):
sched[i] = a[i*36:(i+1)*36]
return(sched)
import re
regex = re.compile('[0-9]*h[0-9]*-[0-9]*h[0-9]*') # regex time
days = []
schedule = get_schedule(a)
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
days.append(cls)
days = format(days)
pretty_print(days, date)
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