Created
February 26, 2019 15:27
-
-
Save karolba/12aad8217eb60e63aab742ae5421295f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import icalendar | |
import json | |
import requests | |
from dateutil import parser | |
from datetime import timedelta, datetime | |
GRUPA_WYKŁAD = 0.0 | |
GRUPA_ĆW = 5.0 # 1.0 dla grupy 1, 2.0 dla grupy 2, itd | |
GRUPA_AUDYTORYJNA = 5.1 # 5.1 dla "5a", 5.2 dla "5b" | |
# Przedmioty których nie dodawać na plan | |
TO_IGNORE = [ | |
'Podstawy grafiki komputerowej, Wykład', | |
'Algorytmy i struktury danych, Wykład', | |
'Skład dokumentów w środowisku LaTeX, Wykład', | |
'Języki i metody programowania 2, Wykład', | |
'Język obcy, Wykład', | |
'Fizyka 1, Wykład' | |
] | |
URL = 'http://planzajec.eaiib.agh.edu.pl/view/timetable/488/events?start=2019-02-25&end=2020-03-02&_={}'.format( | |
int(datetime.now().timestamp() * 1000)) | |
def parse_datetime(s): | |
return parser.parse(s) | |
def process_data_event(cal, data_event): | |
if data_event['group'] not in (GRUPA_WYKŁAD, GRUPA_ĆW, GRUPA_AUDYTORYJNA): | |
return | |
title, summary = data_event['title'].split('<br/>', 1) | |
for to_ignore in TO_IGNORE: | |
if title.startswith(to_ignore): | |
return | |
summary = summary.replace('<br/>', ' ') | |
if ", Wykład" in title: | |
title = "[w] " + title.replace(", Wykład", "") | |
elif ", Ćwicz. lab" in title: | |
title = "[lab] " + title.replace(", Ćwicz. lab", "") | |
elif ", Ćwicz. aud" in title: | |
title = "[aud] " + title.replace(", Ćwicz. aud", "") | |
title = title.replace(", Inne", "") | |
calendar_event = icalendar.Event() | |
calendar_event.add('summary', title) | |
calendar_event.add('description', summary) | |
calendar_event.add('dtstart', parse_datetime(data_event['start'])) | |
calendar_event.add('dtend', parse_datetime(data_event['end'])) | |
print("Dodaję {} ({})".format(title, data_event['start'])) | |
cal.add_component(calendar_event) | |
def main(): | |
#with open('events.json', 'r') as f: | |
# json_data = json.load(f) | |
json_data = requests.get(URL).json() | |
cal = icalendar.Calendar() | |
for event in json_data: | |
process_data_event(cal, event) | |
with open('kalendarz_eaiib.ics', 'wb') as f: | |
f.write(cal.to_ical()) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment