initial commit - WIP

This commit is contained in:
rra 2021-07-09 10:38:58 +02:00
commit 944965d16e
4 changed files with 115 additions and 0 deletions

74
event_feed.py Normal file
View File

@ -0,0 +1,74 @@
#!/bin/python3
#lumbung.space calendar feed generator
#© 2021 roel roscam abbing gplv3 etc
from ics import Calendar
import requests
import jinja2
import os
from slugify import slugify
from natural import date
from event_feed_config import calendar_url, output_dir
cal = Calendar(requests.get(calendar_url).text)
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.curdir)
)
if not os.path.exists(output_dir):
os.mkdir(output_dir)
template = env.get_template('event_template.md')
existing_posts = os.listdir(output_dir)
# Dates need to be displayed for the various TZs
# 3 PM Kassel, Germany, 4 PM Ramallah/Jerusalem, Palestina (QoF),
# 8 AM Bogota, Colombia (MaMa), 8 PM Jakarta, Indonesia (Gudskul),
# 1 PM (+1day) Wellington, New Zealand (Fafswag), 9 AM Havana, Cuba (Instar).
tzs = [
('Kassel','Europe/Berlin'),
('Bamako', 'Europe/London'),
('Palestine','Asia/Jerusalem'),
('Bogota','America/Bogota'),
('Jakarta','Asia/Jakarta'),
('Makassar','Asia/Makassar'),
('Wellington', 'Pacific/Auckland')
]
for event in list(cal.events):
localized_begins =[]
for i in tzs:
localized_begins.append( #javascript formatting because of string creation from hell
'__{}__ {}'.format(
str(i[0]),
str(event.begin.to(i[1]).format("YYYY-MM-DD HH:mm"))
)
)
event_metadata = {
'name':event.name,
'created':event.created.format(),
'description': event.description,
'begin': '        '.join(localized_begins), #non-breaking space characters to defeat markdown
'end': event.end.format(),
'duration': date.compress(event.duration),
'location': event.location
}
#FIXME: There is no deletion logic
#FIXME: figure out how to use legible yet unique post names
#FIXME: An update logic?
post_dir = os.path.join(output_dir, slugify(event.name)) + '-' + slugify(event.begin.format("YYYY-MM-DD"))
if not os.path.exists(post_dir):
os.mkdir(post_dir)
with open(os.path.join(post_dir,'index.md'),'w') as f:
post = template.render(event = event_metadata)
f.write(post)

6
event_feed_config.py Normal file
View File

@ -0,0 +1,6 @@
# a publicly accessible ICS calendar
calendar_url= ''
# your Hugo content directory
output_dir = ''

19
event_template.md Normal file
View File

@ -0,0 +1,19 @@
---
title: "{{ event.name }}"
date: "{{ event.created }}" #2021-06-10T10:46:33+02:00
draft: false
category: "calendar"
event_begin: "{{ event.begin }}"
event_end: "{{ event.end }}"
duration: "{{ event.duration }}"
{% if event.location %}
location: "{{ event.location }}"
{% endif %}
---
{% if event.description %}
{{ event.description }}
{% endif %}

16
requirements.txt Normal file
View File

@ -0,0 +1,16 @@
# Automatically generated by https://github.com/damnever/pigar.
# calendar-feed/event_feed.py: 3
Jinja2 == 2.10
# calendar-feed/event_feed.py: 1
ics == 0.7
# calendar-feed/event_feed.py: 6
natural == 0.2.0
# calendar-feed/event_feed.py: 5
python_slugify == 5.0.2
# calendar-feed/event_feed.py: 2
requests == 2.21.0