first commit

This commit is contained in:
Stuce 2025-11-16 10:08:35 +01:00
commit 3a2241e434
4 changed files with 247 additions and 0 deletions

77
main.py Executable file
View file

@ -0,0 +1,77 @@
#!/usr/bin/env python
from flask import Flask
from caldav import DAVClient, Calendar, Principal, Todo
# TODO:
# [ ] 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 fetch_10_next_todos_as_csv() -> str:
def auth() -> Principal:
# TODO: on the final version, fetch it locally
client = DAVClient(
url="https://cal.stuce.ch", username="eInk", password="4Ftxy9rp8e$F*f"
)
principal = client.principal()
return principal
def get_calendar(principal: Principal) -> Calendar:
calendar = principal.calendar(name="Ouais le ménage")
return calendar
def todos_to_csv(todos: list[Todo]) -> str:
result = "".join(
f"{todo.icalendar_component['uid']},{todo.icalendar_component['summary']}\n"
for todo in todos
)
return result
principal = auth()
calendar = get_calendar(principal)
sorted_todos = calendar.todos(sort_keys=("due"))
cut_todos = sorted_todos[:10]
csv = todos_to_csv(cut_todos)
return csv
def put(todos, uid):
item = filter(lambda todo: todo.icalendar_component["uid"] == uid, todos)
# class SimpleHandler(BaseHTTPRequestHandler):
# def do_GET(self):
# self.send_response(200)
# self.send_header("Content-type", "text/plain")
# self.end_headers()
# response_body_str = fetch_10_next_todos_as_csv()
# response_body_utf8 = response_body_str.encode("utf8")
# self.wfile.write(response_body_utf8)
#
#
# def main(server_class=HTTPServer, port=8000):
# server_address = ("", port)
# httpd = server_class(server_address, SimpleHandler)
# httpd.serve_forever()
#
#
# if __name__ == "__main__":
# main()
app = Flask(__name__)
@app.route("/")
def send_events():
return fetch_10_next_todos_as_csv()
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8080)