Something went wrong on our end
-
Dirk Sarpe authored
- refactored script into functions - functions can take arguments (step to be more configurable) - more telling variable names - import order
Dirk Sarpe authored- refactored script into functions - functions can take arguments (step to be more configurable) - more telling variable names - import order
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
event_display.py 2.73 KiB
#!/usr/bin/python
def main(url, timezone, future_weeks, filename):
"""parse ical calendar and insert values into html template.
"""
html_list = ics2list(url, timezone, future_weeks)
calendarlist2jinja(html_list, filename)
def ics2list(url, timezone, future_weeks):
"""parse ical calendar into a list
"""
import requests
import arrow
from ics import Calendar
now = arrow.now(timezone)
period = (now.floor('day'), now.replace(weeks=+future_weeks))
calendar = Calendar(requests.get(url).text)
html_list = []
for event in calendar.events:
if event.end > period[0] and event.begin < period[1]:
# only display events which have not ended yet
# and start earlier than defined above
duration = event.end - event.begin
# short, full day and multi day events display datetime differently
if duration.days < 1:
event_date = (event.begin.format('DD.MM.YYYY, HH:mm')
+ ' – '
+ event.end.format('HH:mm'))
elif duration.days == 1:
event_date = event.begin.format('DD.MM.YYYY') + ', all day'
elif duration.days > 1:
# in ics file the end date is the day after end date at 00:00:00
corrected_end_date = event.end.replace(days=- 1)
event_date = (event.begin.format('DD.MM.YYYY')
+ ' – '
+ corrected_end_date.format('DD.MM.YYYY'))
else:
event_date = ''
# create list of dictionaries for jinja2 template
event = dict(date=event_date,
name=event.name,
location=event.location)
html_list.append(event)
return html_list
def calendarlist2jinja(html_list, filename):
"""write list of events to jinja html template
"""
import os
from jinja2 import Environment, FileSystemLoader
# Capture our current directory
this_dir = os.path.dirname(os.path.abspath(__file__))
# Create the jinja2 environment.
# Notice the use of trim_blocks, which greatly helps control whitespace.
j2_env = Environment(loader=FileSystemLoader(this_dir),
trim_blocks=True)
output = j2_env.get_template('template.html').render(html_list=html_list)
out_file = open(this_dir + filename, "w")
out_file.write(output.encode("utf-8"))
out_file.close()
if __name__ == '__main__':
main(url='https://portal.idiv.de/ssf/ical/basic.ics?bi=393&ui=1026&pd=171497477d81eb659bd832ffe26c8bfe39d65fc3&v=1',
timezone='Europe/Berlin',
future_weeks=4,
filename="/website/events.html")