WebDav calendar access implemented

This commit is contained in:
José Carlos Cuevas 2021-12-09 09:38:15 +01:00
parent 04c22c513b
commit 984f3d578f
2 changed files with 65 additions and 7 deletions

View file

@ -18,6 +18,7 @@ import re
import requests
from config import load_config, CONFIG_PATH
from webdav import Client
# Enable logging
@ -335,6 +336,36 @@ def print_image(printer, image):
os.remove("temp.png")
def print_calendar(printer):
"""
Connects to a webdav calendar and retrieves
the events for today
"""
c = Client(config)
events = c.get_todays_events()
printer.set(align="center",
text_type="BU",
smooth=True)
printer.text("Today:\n")
printer.set(align="left",
text_type="NORMAL",
font="b",
smooth=True)
for event in events:
hour = event[1].hour
minutes = event[1].minute
if minutes < 10:
printer.text(f"{hour}:0{minutes}\n")
else:
printer.text(f"{hour}:{minutes}\n")
printer.text(f"{event[0]}\n--\n")
def printing_script(printer):
"""
Gets the configuration and parses its script
@ -366,6 +397,9 @@ def printing_script(printer):
if key == 'TEXT':
print_text(printer, value)
if key == 'CALENDAR':
print_text(printer)
printer.set(align="center",
text_type="NORMAL",
font="b",
@ -391,6 +425,7 @@ def create_parser():
parser.add_argument("--text", nargs=1, default=None, help="Print the given text, use '-' to read from stdin")
parser.add_argument("--file", default=None, type=open, help="Loads a file and sends it to the printer (as text)")
parser.add_argument("--image", nargs=1, default=None, help="Print an image")
parser.add_argument("--calendar", action="store_true", help="Print configured calendar events for today")
parser.add_argument("--genqr", action="store_true", help="Activates the generation of QR codes for links in news and gemini")
return parser
@ -435,6 +470,11 @@ if __name__ == "__main__":
print_image(printer, args.image[0])
printer.text("\n\n")
if args.calendar:
ops_called += 1
print_calendar(printer)
printer.text("\n\n")
# In case we get called (almost) empty
if ops_called == 0:
# Try to parse a config script

View file

@ -1,7 +1,8 @@
import datetime
import os
import os.path
import tempfile
from icalendar import Calendar, Event
from icalendar import Calendar
from webdav3.client import Client as WDClient
@ -22,7 +23,8 @@ class Client:
"""
Retrieves the calendar list
"""
return self.client.list(self.path)
paths = [path for path in self.client.list(self.calendar_path) if ".ics" in path]
return paths
def read_calendar(self, pathname):
"""
@ -33,8 +35,9 @@ class Client:
self.client.download_sync(remote_path=rpath,
local_path=temp_path)
# Read the temporary data, close the file, remove it
data = f.read()
f.close()
fics = open(temp_path, 'r')
data = fics.read()
fics.close()
os.remove(temp_path)
# Now with the data read, we can process it
return self.parse_calendar(data)
@ -43,15 +46,30 @@ class Client:
"""
Parses ICS calendar data received
"""
events = []
now = datetime.datetime.now()
start_day = datetime.datetime(now.year, now.month, now.day)
tomorrow = start_day + datetime.timedelta(days=1)
calendar = Calendar.from_ical(data)
for component in calendar.walk():
if component.name == 'VEVENT':
pass
event_date = component.decoded('dtstart')
ed = datetime.datetime(event_date.year, event_date.month, event_date.day)
if ed >= start_day and ed < tomorrow:
events.append((component.get('summary'),
component.decoded('dtstart')))
return events
def get_todays_events(self):
"""
Get todays events
"""
pass
calendars = self.retrieve_calendars_list()
events = []
for calendar in calendars:
cal_events = self.read_calendar(calendar)
for ev in cal_events:
events.append(ev)
return events