2025-09-21 06:54:25 +02:00
|
|
|
import pathlib, ics, requests, arrow, frontmatter
|
|
|
|
|
import utils
|
|
|
|
|
|
|
|
|
|
DFMT = "YYYY-MM-DD"
|
|
|
|
|
DHFMT = "YYYY-MM-DD HH:mm"
|
|
|
|
|
HFMT = "HH:mm"
|
|
|
|
|
|
|
|
|
|
CEND = '\33[0m'
|
|
|
|
|
CRED = '\33[31m'
|
|
|
|
|
CGREEN = '\33[32m'
|
|
|
|
|
CVIOLET = '\33[35m'
|
|
|
|
|
CBLUE = '\33[34m'
|
|
|
|
|
|
|
|
|
|
def format_event(title:str, date:str, location:str, desc:str):
|
|
|
|
|
c = frontmatter.Post(content=desc)
|
|
|
|
|
c['title'] = title
|
|
|
|
|
c['date'] = date
|
|
|
|
|
c['location'] = location
|
|
|
|
|
c['type'] = 'event'
|
|
|
|
|
return c
|
|
|
|
|
|
|
|
|
|
def compare_events(path:pathlib.PosixPath, title:str, date:str, location:str, desc:str):
|
|
|
|
|
p = frontmatter.load(path)
|
|
|
|
|
pd = p.to_dict()
|
|
|
|
|
pd['content'] = pd['content'].strip()
|
|
|
|
|
return p, (pd == {'title': title, 'date': date, 'location': location, 'type': 'event', 'content': desc.strip()})
|
|
|
|
|
|
|
|
|
|
def update_event(previous:frontmatter.Post, new:frontmatter.Post):
|
|
|
|
|
keys = set(previous.keys()).union(set(new.keys()))
|
|
|
|
|
for k in keys:
|
|
|
|
|
if not str(previous[k]).strip() == str(new[k]).strip():
|
|
|
|
|
print(f"Update '{k}' (y/n)?\n\tprev: {CRED}{previous[k]}{CEND}\n\tnew: {CGREEN}{new[k]}{CEND}")
|
|
|
|
|
c = input()
|
|
|
|
|
if c == 'y':
|
|
|
|
|
previous[k] = new[k]
|
|
|
|
|
|
|
|
|
|
if not previous.content.strip() == new.content.strip():
|
|
|
|
|
print(f"Update 'content' (y/n)?\n\tprev: {CRED}{previous.content}{CEND}\n\tnew: {CGREEN}{new.content}{CEND}")
|
|
|
|
|
c = input()
|
|
|
|
|
if c == 'y':
|
|
|
|
|
previous.content = new.content
|
|
|
|
|
|
|
|
|
|
return previous
|
|
|
|
|
|
|
|
|
|
def read_date(p:frontmatter.Post):
|
|
|
|
|
date = p['date']
|
|
|
|
|
# 2024-10-06 10:00-12:00
|
|
|
|
|
date = "-".join(date.split("-")[:-1])
|
|
|
|
|
return arrow.get(date, DHFMT)
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
conf = utils.load_conf()
|
|
|
|
|
|
|
|
|
|
req = requests.get(conf['ics_url']).text
|
|
|
|
|
c = ics.Calendar(req)
|
|
|
|
|
|
|
|
|
|
for e in c.events:
|
|
|
|
|
|
|
|
|
|
# file path + name
|
|
|
|
|
name = e.name.replace(' ', '-')
|
|
|
|
|
fm_date = e.begin.format(DFMT)
|
|
|
|
|
fm_date_interval = f"{e.begin.format(DHFMT)}-{e.end.format(HFMT)}"
|
|
|
|
|
filename = f'{fm_date}-{name}.md'
|
2025-09-21 07:32:21 +02:00
|
|
|
filepath = pathlib.Path(conf['content']) / 'events' / filename
|
2025-09-21 06:54:25 +02:00
|
|
|
|
|
|
|
|
if not filepath.exists():
|
|
|
|
|
print(f"new event: {e.name}")
|
|
|
|
|
new = format_event(title=e.name, date=fm_date_interval, location=e.location, desc=e.description)
|
|
|
|
|
utils.save_file(filepath, frontmatter.dumps(new), mkdirs=True)
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
prev, eq = compare_events(filepath, e.name, fm_date_interval, e.location, e.description)
|
|
|
|
|
|
|
|
|
|
if eq:
|
|
|
|
|
print(f"event {CVIOLET}{e.name}{CEND} already exists... continuing")
|
|
|
|
|
continue
|
|
|
|
|
print(f"updating event: {CBLUE}{e.name}{CEND}")
|
|
|
|
|
|
|
|
|
|
## selective update
|
|
|
|
|
new = format_event(title=e.name, date=fm_date_interval, location=e.location, desc=e.description)
|
|
|
|
|
|
|
|
|
|
updated = update_event(prev, new)
|
|
|
|
|
utils.save_file(filepath, frontmatter.dumps(updated), overwrite=True)
|
|
|
|
|
|
|
|
|
|
print(f"event {e.name} updated")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|