update caldav
This commit is contained in:
@@ -7,10 +7,9 @@
|
|||||||
libraries = with pkgs.python3Packages; [ caldav ics pytz ];
|
libraries = with pkgs.python3Packages; [ caldav ics pytz ];
|
||||||
flakeIgnore = [ "E302" "E305" "E501" "E261" ];
|
flakeIgnore = [ "E302" "E305" "E501" "E261" ];
|
||||||
} /* python */ ''
|
} /* python */ ''
|
||||||
import os
|
|
||||||
import json
|
import json
|
||||||
from caldav import DAVClient
|
from caldav import DAVClient
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone, timedelta
|
||||||
from ics import Calendar
|
from ics import Calendar
|
||||||
from pytz import UTC
|
from pytz import UTC
|
||||||
|
|
||||||
@@ -33,13 +32,15 @@ def datetime_parser(dct):
|
|||||||
return dct
|
return dct
|
||||||
|
|
||||||
def load_cache(cache_file):
|
def load_cache(cache_file):
|
||||||
if os.path.exists(cache_file):
|
try:
|
||||||
with open(cache_file, "r") as file:
|
with open(cache_file, "r") as file:
|
||||||
return json.load(file, object_hook=datetime_parser)
|
return json.load(file, object_hook=datetime_parser)
|
||||||
|
except (json.JSONDecodeError, FileNotFoundError):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def save_cache(cache_file, data):
|
def save_cache(cache_file, data):
|
||||||
with open(cache_file, "w") as file:
|
with open(cache_file, "w") as file:
|
||||||
|
data['last_checked'] = datetime.now(timezone.utc)
|
||||||
json.dump(data, file, default=datetime_converter, indent=4)
|
json.dump(data, file, default=datetime_converter, indent=4)
|
||||||
|
|
||||||
|
|
||||||
@@ -83,6 +84,11 @@ def is_expired(event_dict: dict):
|
|||||||
event_end = event_dict['event_end'].timestamp()
|
event_end = event_dict['event_end'].timestamp()
|
||||||
return not (now <= event_end)
|
return not (now <= event_end)
|
||||||
|
|
||||||
|
def is_too_old(event_dict: dict) -> bool:
|
||||||
|
last_checked = event_dict['last_checked']
|
||||||
|
now = datetime.now(timezone.utc)
|
||||||
|
return now - last_checked >= timedelta(minutes=10)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
password_file = "${config.sops.secrets."nx2site/radicale/password".path}" # Path to password file
|
password_file = "${config.sops.secrets."nx2site/radicale/password".path}" # Path to password file
|
||||||
cache_file = "/tmp/caldav_event_cache.json" # Path to cache file
|
cache_file = "/tmp/caldav_event_cache.json" # Path to cache file
|
||||||
@@ -93,7 +99,7 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
event_dict = load_cache(cache_file)
|
event_dict = load_cache(cache_file)
|
||||||
|
|
||||||
if (event_dict is None) or (is_expired(event_dict)):
|
if (event_dict is None) or is_expired(event_dict) or is_too_old(event_dict):
|
||||||
event_dict = get_ongoing_or_next_event(url, username, password)
|
event_dict = get_ongoing_or_next_event(url, username, password)
|
||||||
save_cache(cache_file, event_dict)
|
save_cache(cache_file, event_dict)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user