53 lines
1.9 KiB
Python
53 lines
1.9 KiB
Python
import os
|
|
from datetime import timedelta, datetime
|
|
from threading import Thread
|
|
from time import sleep
|
|
|
|
import icalevents.icalevents
|
|
from pytz import timezone
|
|
|
|
from buba.bubaanimator import BubaAnimation
|
|
from buba.bubacmd import BubaCmd
|
|
|
|
|
|
class IcalEvents(BubaAnimation):
|
|
def __init__(self, buba, url, title):
|
|
super().__init__(buba)
|
|
self.url = url
|
|
self.title = title
|
|
self.events = []
|
|
Thread(target=self.update, daemon=True).start()
|
|
|
|
def __repr__(self):
|
|
return f"<{type(self).__name__}, {self.url}>"
|
|
|
|
def update(self):
|
|
tz = timezone(os.getenv("TZ", "Europe/Berlin"))
|
|
events = icalevents.icalevents.events(self.url, sort=True)
|
|
for event in events:
|
|
event.start = event.start.astimezone(tz)
|
|
self.events = events
|
|
sleep(600)
|
|
|
|
@staticmethod
|
|
def ellipsis(text, max=28):
|
|
if len(text) > max:
|
|
text = text[:max - 3] + "..."
|
|
return text
|
|
|
|
def run(self):
|
|
for (page, events) in enumerate(self.chunk(self.events, 3)):
|
|
if len(self.events) > 3:
|
|
self.buba.text(page=0, row=0, col_start=0, col_end=119,
|
|
text=f"{self.title} ({page + 1}/{int(len(self.events) / 3)})", align=BubaCmd.ALIGN_LEFT)
|
|
else:
|
|
self.buba.text(page=0, row=0, col_start=0, col_end=119, text=self.title, align=BubaCmd.ALIGN_LEFT)
|
|
for (i, event) in enumerate(events):
|
|
if event.start - datetime.now(event.start.tzinfo) < timedelta(hours=24):
|
|
when = event.start.strftime("%H:%M")
|
|
else:
|
|
when = event.start.strftime("%d.%m.")
|
|
self.buba.text(page=0, row=i + 1, col_start=0, col_end=103, text=self.ellipsis(event.summary))
|
|
self.buba.text(page=0, row=i + 1, col_start=104, col_end=119,
|
|
text=when, align=BubaCmd.ALIGN_RIGHT)
|
|
sleep(10)
|