This commit is contained in:
Stuce 2025-11-17 17:42:26 +01:00
parent cc1950aa94
commit f1ff6afdc8
5 changed files with 130 additions and 180 deletions

47
main.py
View file

@ -5,43 +5,32 @@ from caldav import DAVClient, Calendar, Principal, Todo
import configparser
# TODO:
# [x] settings file to not have hardcoded credentials
# [ ] change production credentials
# [ ] nix flake with complete module
# [ ] post behavior (simple)
# [ ] post behavior (concurrent protection)
# [ ] check if better http server availible or fastcgi better suited
#!/usr/bin/env python
def parseConfig():
def passwordFromFile() -> str:
calDavPasswordFile = config.get("calDAV", "passwordFile")
with open(calDavPasswordFile, "r") as file:
return file.readline().strip()
config = configparser.ConfigParser()
config.read("/home/stuce/calDAVtoCSV/config.ini") # TODO: change this
global calDavAddress
global calDavUsername
global calDavPassword
global calDavCalendarName
global serverPort
config.read("/etc/calDAVtoCSV/config.ini")
calDavAddress = config.get("calDAV", "address")
print(calDavAddress)
calDavUsername = config.get("calDAV", "username")
calDavPassword = config.get("calDAV", "password")
calDavPasswordFile = config.get("calDAV", "passwordFile")
caldavpwd = config.get("calDAV", "password", fallback=None)
caldavpwd = caldavpwd if caldavpwd is not None else passwordFromFile()
calDavCalendarName = config.get("calDAV", "calendarName")
serverPort = config.getint("server", "port")
return calDavAddress, calDavUsername, caldavpwd, calDavCalendarName
def fetch_10_next_todos_as_csv() -> str:
def auth() -> Principal:
def auth(add: str, user: str, pwd: str) -> Principal:
# TODO: on the final version, fetch it locally
client = DAVClient(
url=calDavAddress, username=calDavUsername, password=calDavPassword
)
client = DAVClient(url=add, username=user, password=pwd)
principal = client.principal()
return principal
def get_calendar(principal: Principal) -> Calendar:
calendar = principal.calendar(name="Ouais le ménage")
def get_calendar(principal: Principal, name: str) -> Calendar:
calendar = principal.calendar(name=name)
return calendar
def todos_to_csv(todos: list[Todo]) -> str:
@ -51,8 +40,9 @@ def fetch_10_next_todos_as_csv() -> str:
)
return result
principal = auth()
calendar = get_calendar(principal)
add, user, pwd, name = parseConfig()
principal = auth(add, user, pwd)
calendar = get_calendar(principal, name)
sorted_todos = calendar.todos(sort_keys=("due"))
cut_todos = sorted_todos[:10]
@ -73,5 +63,4 @@ def send_events():
if __name__ == "__main__":
parseConfig()
app.run(host="0.0.0.0", port=serverPort)
app.run(host="0.0.0.0", port=8000)