calendar waybar
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
{ pkgs, rice, ... }: let
|
||||
sep = " ";
|
||||
{ config, pkgs, rice, domain, user, ... }:
|
||||
let
|
||||
sep = " ";
|
||||
in {
|
||||
home.packages =
|
||||
let
|
||||
waybar_mode_script = /*bash*/ ''
|
||||
sops.secrets = {
|
||||
"nx2site/radicale/password" = { };
|
||||
};
|
||||
home.packages = with pkgs; [
|
||||
(writeShellApplication { name = "waybar_mode"; text = /*bash*/ ''
|
||||
print_help() {
|
||||
echo "Usage: waybar_mode {set <string>|unset}"
|
||||
}
|
||||
@@ -33,19 +36,118 @@ in {
|
||||
;;
|
||||
esac
|
||||
exit 0
|
||||
'';
|
||||
cclock_script = /*bash*/ ''
|
||||
'';})
|
||||
(writeShellApplication { name = "cclock"; text = /*bash*/ ''
|
||||
ord=$(date +"%e" | awk '{printf("%d%s\n", $1, ($1==11||$1==12||$1==13)?"th":((($1%10)==1)?"st":((($1%10)==2)?"nd":((($1%10)==3)?"rd":"th"))))}')
|
||||
if [ $# -eq 0 ]; then
|
||||
echo "${sep}$(date +'%A the')" "$ord" "of" "$(date +'%B')" " ${sep}$(date +'%R')"
|
||||
elif [ "$1" = "--no-icons" ]; then
|
||||
echo "$(date +'%A the')" "$ord" "of" "$(date +'%B')" "$(date +'%R')"
|
||||
fi
|
||||
'';
|
||||
in
|
||||
with pkgs; [
|
||||
(writeShellApplication { name = "waybar_mode"; text = waybar_mode_script;})
|
||||
(writeShellApplication { name = "cclock"; text = cclock_script;})
|
||||
'';})
|
||||
(writers.writePython3Bin "caldav_event" {
|
||||
libraries = with pkgs.python3Packages; [ caldav ics pytz ];
|
||||
flakeIgnore = [ "E302" "E305""E501" ];
|
||||
} /* python */ ''
|
||||
import os
|
||||
from caldav import DAVClient
|
||||
from datetime import datetime, timezone
|
||||
import json
|
||||
from ics import Calendar
|
||||
|
||||
def get_password(password_file):
|
||||
with open(password_file, "r") as file:
|
||||
return file.read().strip()
|
||||
|
||||
def load_cache(cache_file):
|
||||
if os.path.exists(cache_file):
|
||||
with open(cache_file, "r") as file:
|
||||
return json.load(file)
|
||||
return None
|
||||
|
||||
def save_cache(cache_file, data):
|
||||
with open(cache_file, "w") as file:
|
||||
json.dump(data, file)
|
||||
|
||||
def get_ongoing_and_next_event(url, username, password):
|
||||
now = datetime.now(timezone.utc)
|
||||
ongoing_events = []
|
||||
upcoming_events = []
|
||||
|
||||
try:
|
||||
client = DAVClient(url, username=username, password=password)
|
||||
principal = client.principal()
|
||||
calendars = principal.calendars()
|
||||
|
||||
for calendar in calendars:
|
||||
events = calendar.events()
|
||||
for event in events:
|
||||
ical_data = event.data
|
||||
calendar_parsed = Calendar(ical_data)
|
||||
|
||||
for event in calendar_parsed.events:
|
||||
event_name = event.name or "(No Title)"
|
||||
start_time = event.begin.astimezone(timezone.utc)
|
||||
end_time = event.end.astimezone(timezone.utc)
|
||||
|
||||
if start_time <= now <= end_time:
|
||||
ongoing_events.append((event_name, start_time.timestamp(), end_time.timestamp()))
|
||||
elif start_time > now:
|
||||
upcoming_events.append((event_name, start_time.timestamp(), end_time.timestamp()))
|
||||
except Exception as e:
|
||||
print(f"Error accessing {url}: {e}")
|
||||
|
||||
upcoming_events.sort(key=lambda x: x[1]) # Sort by start time
|
||||
return ongoing_events, upcoming_events[0] if upcoming_events else None
|
||||
|
||||
if __name__ == "__main__":
|
||||
password_file = "${config.sops.secrets."nx2site/radicale/password".path}" # Path to password file
|
||||
cache_file = "/tmp/caldav_event_cache.json" # Path to cache file
|
||||
url = "https://dav.${domain}/"
|
||||
username = "${user}"
|
||||
password = get_password(password_file)
|
||||
|
||||
cache = load_cache(cache_file)
|
||||
now = datetime.now(timezone.utc).timestamp()
|
||||
|
||||
if cache and cache.get("next_event_start") and now < cache["next_event_start"]:
|
||||
ongoing_events = cache.get("ongoing_events", [])
|
||||
next_event = (cache["next_event_name"], cache["next_event_start"], cache["next_event_end"]) if "next_event_name" in cache else None
|
||||
else:
|
||||
ongoing_events, next_event = get_ongoing_and_next_event(url, username, password)
|
||||
|
||||
cache_data = {
|
||||
"ongoing_events": ongoing_events,
|
||||
"next_event_name": next_event[0] if next_event else None,
|
||||
"next_event_start": next_event[1] if next_event else None,
|
||||
"next_event_end": next_event[2] if next_event else None
|
||||
}
|
||||
save_cache(cache_file, cache_data)
|
||||
|
||||
if ongoing_events:
|
||||
for event_name, start_time, end_time in ongoing_events:
|
||||
time_remaining = end_time - now
|
||||
hours, rem = divmod(int(time_remaining), 3600)
|
||||
minutes, _ = divmod(rem, 60)
|
||||
|
||||
if hours == 0:
|
||||
print(f"{event_name} {minutes} minute{'s ' if minutes > 1 else ' '}left")
|
||||
else:
|
||||
print(f"{event_name} {hours} hour{'s ' if hours > 1 else ' '}and {minutes} minute{'s ' if minutes > 1 else ' '}left")
|
||||
else:
|
||||
if next_event:
|
||||
event_name, start_time, end_time = next_event
|
||||
time_until_start = start_time - now
|
||||
hours, rem = divmod(int(time_until_start), 3600)
|
||||
minutes, _ = divmod(rem, 60)
|
||||
|
||||
if hours == 0:
|
||||
print(f"'{event_name}' starts in {minutes} minute{'s ' if minutes > 1 else ' '}")
|
||||
else:
|
||||
print(f"'{event_name}' starts in {hours} hour{'s ' if hours > 1 else ' '}and {minutes} minute{'s ' if minutes > 1 else ' '}")
|
||||
else:
|
||||
print("No upcoming events found.")
|
||||
'')
|
||||
];
|
||||
|
||||
programs.waybar = {
|
||||
@@ -78,7 +180,7 @@ in {
|
||||
];
|
||||
modules-right = [
|
||||
"custom/mode"
|
||||
"custom/ctimeremaining"
|
||||
"custom/caldav_event"
|
||||
"custom/cclock"
|
||||
"tray"
|
||||
];
|
||||
@@ -97,10 +199,11 @@ in {
|
||||
exec = "cclock";
|
||||
restart-interval = 60;
|
||||
};
|
||||
# "custom/ctimeremaining" = {
|
||||
# exec = "nx_gcal_event lookup";
|
||||
# restart-interval = 60;
|
||||
# };
|
||||
"custom/caldav_event" = {
|
||||
format = "${sep}{}";
|
||||
exec = "caldav_event";
|
||||
restart-interval = 60;
|
||||
};
|
||||
"custom/mode" = {
|
||||
exec = "cat /tmp/waybar-mode";
|
||||
interval = "once";
|
||||
@@ -182,6 +285,7 @@ in {
|
||||
#clock,
|
||||
#custom-cclock,
|
||||
#custom-mode,
|
||||
#custom-caldav-event,
|
||||
#battery,
|
||||
#cpu,
|
||||
#tray,
|
||||
@@ -242,12 +346,12 @@ in {
|
||||
color: rgb(${f green.base});
|
||||
}
|
||||
|
||||
#battery.critical {
|
||||
#battery.critical {
|
||||
background: rgb(${f negative.base});
|
||||
color: rgb(${f foreground});
|
||||
}
|
||||
'';
|
||||
|
||||
#battery.critical:not(.charging) {
|
||||
#battery.critical:not(.charging) {
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user