added datetime tracking

This commit is contained in:
2026-01-22 20:09:20 +01:00
parent b56fe56f84
commit 00ab59f0e4
8 changed files with 36 additions and 6 deletions

9
app/dater.py Normal file
View File

@@ -0,0 +1,9 @@
from ics import Calendar
import utils
def date_calendar(calendar: Calendar):
time = utils.format_datetime("%d/%m/%Y %H:%M:%S")
for event in calendar.events:
old_description = f"event.description\n" if event.description else ""
event.description = f"{old_description}Retrieved on: {time}"

View File

@@ -57,7 +57,7 @@ def generate_class(response_callback):
def do_OPTIONS(self):
self._handle_request()
def log_message(self, fmt, *args):
def log_message(self, format, *args):
return
return Handler

View File

@@ -1,13 +1,13 @@
import sys
import threading
from datetime import datetime
import utils
_console_lock = threading.Lock()
def _log(prefix: str, message: str):
time = datetime.now().strftime("[%H:%M:%S]")
time = utils.format_datetime("%H:%M:%S")
with _console_lock:
print(f"{time} {prefix} {message}")
print(f"[{time}] {prefix} {message}")
def info(message: str):
_log("INFO", message)

View File

@@ -2,6 +2,7 @@ import signal
import sys
import os
import logger
import utils
from server import Server
def _handle_exit(sig, frame):
@@ -30,10 +31,13 @@ def main():
port = _get_environment_variable("PORT")
cache_duration = _get_environment_variable("CACHE_DURATION")
ics_url = _get_environment_variable("ICS_URL")
time_zone = _get_environment_variable("TIME_ZONE")
port_int = _parse_environment_variable("PORT", port)
cache_duration_int = _parse_environment_variable("CACHE_DURATION", cache_duration)
utils.set_time_zone(time_zone)
server = Server(host, port_int, cache_duration_int, ics_url)
server.serve()

View File

@@ -1,5 +1,6 @@
import time
import filter
import dater
import handler
from http.server import HTTPServer
import logger
@@ -16,10 +17,13 @@ class Server:
def _cache_calendar(self):
if not self._cached_calendar or self._cached_time + self._cache_duration < time.time():
self._cached_calendar = grabber.grab_calendar(self._ics_url)
if not self._cached_calendar:
calendar = grabber.grab_calendar(self._ics_url)
if not calendar:
return False
dater.date_calendar(calendar)
self._cached_calendar = calendar
self._cached_time = time.time()
logger.info("Successfully cached calendar")

11
app/utils.py Normal file
View File

@@ -0,0 +1,11 @@
from datetime import datetime
from zoneinfo import ZoneInfo
_time_zone = ""
def set_time_zone(time_zone: str):
global _time_zone
_time_zone = time_zone
def format_datetime(format: str):
return datetime.now(ZoneInfo(_time_zone)).strftime(format)