From fbc6fe31dd6130b3e39f783241e3f0659a74d357 Mon Sep 17 00:00:00 2001 From: Daniel Molkentin Date: Sat, 14 Jul 2018 22:53:59 +0200 Subject: [PATCH] Add calendar generator --- README.md | 1 + build.sh | 4 +- config.yaml | 2 +- content/_index.md | 8 ++++ tools/gen_upcoming.py | 88 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 101 insertions(+), 2 deletions(-) create mode 100755 tools/gen_upcoming.py diff --git a/README.md b/README.md index a23fd9f..b85f550 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ Running a dev setup * run hugo webserver: `hugo serve` * Point your browser to `http://localhost:1313/s/` * To ready your site for upload, run "./build.sh", which also generates all.ics + and adds the calendar table to index.html Every change you make on the project will be reflected in your browser as long as `hugo serve` is running. diff --git a/build.sh b/build.sh index 4334054..37fad3e 100755 --- a/build.sh +++ b/build.sh @@ -1,4 +1,6 @@ #!/usr/bin/env sh -tools/merge_cals.py hugo +tools/merge_cals.py +upcoming="$(tools/gen_upcoming.py static/all.ics 7 7|tr '\n' ' ')" +sed -i "s#CALENDAR#$upcoming#g" public/index.html diff --git a/config.yaml b/config.yaml index 71665e7..06d25a5 100644 --- a/config.yaml +++ b/config.yaml @@ -1,6 +1,6 @@ baseURL: "https://staging.berlin.ccc.de/s" languageCode: "de-de" -title: "Chaos Computer Club Berlin e.V." +title: "Chaos Computer Club Berlin" theme: "beautifulhugo" RelativeURLs: true CanonifyURLs: true diff --git a/content/_index.md b/content/_index.md index e69de29..ccfb285 100644 --- a/content/_index.md +++ b/content/_index.md @@ -0,0 +1,8 @@ +--- +description: "Startseite CCCB mit Kurzkalender" +--- +### Nächste Veranstaltungen: + +CALENDAR + +Keinen Termin mehr verpeilen? Einfach den [Veranstaltungskalender abonnieren](all.ics)! diff --git a/tools/gen_upcoming.py b/tools/gen_upcoming.py new file mode 100755 index 0000000..d611668 --- /dev/null +++ b/tools/gen_upcoming.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python3 + +import sys +from datetime import datetime, timedelta +from dateutil.parser import parse +from dateutil.rrule import rruleset, rrulestr +import icalendar +import locale +import logging + +from pprint import pprint + +def vevent_to_event(event, rrstart=None): + if rrstart == None: + begin = parse(event['DTSTART'].to_ical()) + else: + begin = rrstart + return { "name": event['SUMMARY'].to_ical(), "url": event['URL'].to_ical(), "begin": begin } + +def parse_single_event(event, start, end): + logging.info("Processing single event %s" % event['SUMMARY'].to_ical().decode('utf-8')) + dtstart = parse(event['DTSTART'].to_ical()) + if dtstart >= start and dtstart < end: + return vevent_to_event(event) + else: + return None + +def parse_recurring_event(event, start, end): + logging.info("Processing recurring event %s" % event['SUMMARY'].to_ical().decode('utf-8')) + dtstart = parse(event['DTSTART'].to_ical()) + rs = rruleset() + rs.rrule(rrulestr(event['RRULE'].to_ical().decode('utf-8'), dtstart=dtstart)) + if 'EXDATE' in event.keys(): + exdates = event['EXDATE'] + for exdate in exdates: + rs.exdate(parse(exdate.to_ical())) + + dates = list(rs) + events = [] + for date in dates: + if date >= start and date < end: + events.append(vevent_to_event(event, date)) + return events + + +def find_events(icsfilestr, start, end, num): + with open(icsfilestr, 'r') as icsfile: + cal=icalendar.Calendar.from_ical(icsfile.read()) + + events=[] + for event in cal.subcomponents: + if event.name == 'VEVENT': + if 'RRULE' in event.keys(): + events = events + parse_recurring_event(event, start, end) + else: + ev = parse_single_event(event, start, end) + if ev != None: + events.append(ev) + + events = sorted(events, key=lambda k: k['begin']) + events = events[0:num] + return events + + +def format_events(events): + print('') + for event in events: + dateStr = event['begin'].strftime("%A, %d.%m um %H:%M Uhr") + #print("
  • %s: %s
  • " % (event['url'].decode('utf-8'), dateStr, event['name'].decode('utf-8'))) + print("" + % (dateStr, event['url'].decode('utf-8'), event['name'].decode('utf-8'))) + print('
    %s%s
    ') + +def main(): + if len(sys.argv) < 3: + print("Usage: %s calendar max_days max_items" % sys.argv[0]) + sys.exit(-1) + + locale.setlocale(locale.LC_TIME, "de_DE.UTF-8") + calendar=sys.argv[1] + max_days=int(sys.argv[2]) + max_items=int(sys.argv[3]) + + events=find_events(calendar, datetime.now(), datetime.now() + timedelta(days=max_days), max_items) + format_events(events) + +if __name__ == "__main__": + main()