diff --git a/importer.py b/importer.py index e4e83d18b3bf49cc4485c2e245159dd692c56522..e7f52f5f60602f1a94cc60e4dc57453deb767120 100644 --- a/importer.py +++ b/importer.py @@ -111,20 +111,65 @@ def fetch_co_course_events(i): # it is parsed. return events +def fetch_ro_event_ical(ids): + data = {'pMode': 'T', 'pInclPruef': 'N', 'pInclPruefGepl': 'N', 'pOutputFormat': '99', 'pCharset': 'UTF8', 'pMaskAction': 'DOWNLOAD'} + data = list(data.items()) + for id in ids: + data.append(('pTerminNr', id)) + data = urllib.parse.urlencode(data).encode('utf-8') + r = urllib.request.Request('https://online.rwth-aachen.de/RWTHonline/pl/ui/%24ctx/wbKalender.wbExport', + data=data, method='POST') + with urllib.request.urlopen(r) as f: + return f.read().decode('utf-8') + +def fetch_ro_course_ical(id): + from lxml import html + url = 'https://online.rwth-aachen.de/RWTHonline/pl/ui/%24ctx/wbTermin_List.wbLehrveranstaltung?pStpSpNr='+'%i'%(int(id)) + req = urllib.request.urlopen(url) + dom = html.fromstring(req.read()) + event_ids = [x.value for x in dom.xpath('//input[@name="pTerminNr"]')] + return fetch_ro_event_ical(event_ids) + +def fetch_ro_course_events(item): + import icalendar + # First fix crappy javascript fragment-Paths + url = urllib.parse.urlparse(item['url'].replace('#/', '')) + args = urllib.parse.parse_qs(url.query) + if 'pStpSpNr' in args: # Legacy URLs + id = args['pStpSpNr'][0] + elif url.path.split('/')[-2] == 'courses': # New URLs + id = url.path.split('/')[-1] + else: + flash("Ungültige URL: '"+i['url']+"'") + cal = icalendar.Calendar().from_ical(fetch_ro_course_ical(id)) + events = [] + for comp in cal.subcomponents: + if comp.name != 'VEVENT': + continue + if comp.get('STATUS') != 'CONFIRMED': + continue + e = {} + e['place'] = str(comp.get('LOCATION', '')) + e['time'] = comp['DTSTART'].dt # TODO: tz + e['duration'] = int((comp['DTEND'].dt - comp['DTSTART'].dt).seconds/60) + e['title'] = item['type'] + events.append(e) + return events + @app.route('/internal/import/<int:id>/now', methods=['GET', 'POST']) @mod_required def import_from(id): - courses = query('SELECT * FROM courses WHERE id = ?', id)[0] lectures = query('SELECT * FROM lectures WHERE course_id = ?', courses['id']) - - import_campus = query('SELECT * FROM import_campus WHERE course_id = ?',id) events = [] try: # if u have to port this to anything new, god be with you. for i in import_campus: - events += fetch_co_course_events(i) + if 'www.campus.rwth-aachen.de' in i['url']: + events += fetch_co_course_events(i) + else: + events += fetch_ro_course_events(i) except ImportError: flash('python-lxml not found, campus import will not work.')