Skip to content
Snippets Groups Projects
Commit 6a98de04 authored by Dirk Sarpe's avatar Dirk Sarpe
Browse files

fix linter warnings and errors

- refactored script into functions
- functions can take arguments (step to be more configurable)
- more telling variable names
- import order
parent 3cf6537e
Branches
No related tags found
No related merge requests found
#!/usr/bin/python #!/usr/bin/python
# parse ical calendar and insert values into html template def main(url, timezone, future_weeks, filename):
"""parse ical calendar and insert values into html template.
import requests """
import arrow html_list = ics2list(url, timezone, future_weeks)
from ics import Calendar calendarlist2jinja(html_list, filename)
from jinja2 import Environment, FileSystemLoader
import os def ics2list(url, timezone, future_weeks):
"""parse ical calendar into a list
"""
url = 'https://portal.idiv.de/ssf/ical/basic.ics?bi=393&ui=1026&pd=171497477d81eb659bd832ffe26c8bfe39d65fc3&v=1' import requests
tz = 'Europe/Berlin' import arrow
now = arrow.now(tz) from ics import Calendar
period = (now.floor('day'), now.replace(weeks=+4))
now = arrow.now(timezone)
period = (now.floor('day'), now.replace(weeks=+future_weeks))
c = Calendar(requests.get(url).text)
html_list = [] calendar = Calendar(requests.get(url).text)
for e in c.events: html_list = []
if e.end > period[0] and e.begin < period[1]: for event in calendar.events:
# only display events which have not ended yet if event.end > period[0] and event.begin < period[1]:
# and start earlier than defined above # only display events which have not ended yet
duration = e.end - e.begin # and start earlier than defined above
# short, full day and multi day events display datetime differently duration = event.end - event.begin
if duration.days < 1: # short, full day and multi day events display datetime differently
event_date = (e.begin.format('DD.MM.YYYY, HH:mm') if duration.days < 1:
+ ' &ndash; ' event_date = (event.begin.format('DD.MM.YYYY, HH:mm')
+ e.end.format('HH:mm')) + ' &ndash; '
elif duration.days == 1: + event.end.format('HH:mm'))
event_date = e.begin.format('DD.MM.YYYY') + ', all day' elif duration.days == 1:
elif duration.days > 1: event_date = event.begin.format('DD.MM.YYYY') + ', all day'
# in ics file the end date is the day after end date at 00:00:00 elif duration.days > 1:
corrected_end_date = e.end.replace(days = - 1) # in ics file the end date is the day after end date at 00:00:00
event_date = (e.begin.format('DD.MM.YYYY') corrected_end_date = event.end.replace(days=- 1)
+ ' &ndash; ' event_date = (event.begin.format('DD.MM.YYYY')
+ corrected_end_date.format('DD.MM.YYYY')) + ' &ndash; '
else: + corrected_end_date.format('DD.MM.YYYY'))
event_date = '' else:
# create list of dictionaries for jinja2 template event_date = ''
event = dict(date = event_date, name = e.name, location = e.location) # create list of dictionaries for jinja2 template
html_list.append(event) event = dict(date=event_date,
name=event.name,
# Capture our current directory location=event.location)
this_dir = os.path.dirname(os.path.abspath(__file__)) html_list.append(event)
return html_list
# Create the jinja2 environment.
# Notice the use of trim_blocks, which greatly helps control whitespace. def calendarlist2jinja(html_list, filename):
j2_env = Environment(loader=FileSystemLoader(this_dir), """write list of events to jinja html template
trim_blocks=True) """
output = j2_env.get_template('template.html').render(html_list = html_list)
out_file = open(this_dir + "/website/events.html", "w") import os
out_file.write(output.encode("utf-8")) from jinja2 import Environment, FileSystemLoader
out_file.close()
# 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")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment