Compare commits
3 Commits
ff1130a16a
...
0162b27d79
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0162b27d79 | ||
|
|
1191019cf8 | ||
|
|
880b3abd60 |
@@ -2,4 +2,125 @@
|
|||||||
sops.secrets = {
|
sops.secrets = {
|
||||||
"nx2site/radicale/password" = { };
|
"nx2site/radicale/password" = { };
|
||||||
};
|
};
|
||||||
|
home.packages = [
|
||||||
|
(pkgs.writers.writePython3Bin "caldav_event" {
|
||||||
|
libraries = with pkgs.python3Packages; [ caldav ics pytz ];
|
||||||
|
flakeIgnore = [ "E302" "E305" "E501" "E261" ];
|
||||||
|
} /* python */ ''
|
||||||
|
import os
|
||||||
|
import json
|
||||||
|
from caldav import DAVClient
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
from ics import Calendar
|
||||||
|
from pytz import UTC
|
||||||
|
|
||||||
|
def get_password(password_file):
|
||||||
|
with open(password_file, "r") as file:
|
||||||
|
return file.read().strip()
|
||||||
|
|
||||||
|
def datetime_converter(obj):
|
||||||
|
if isinstance(obj, datetime):
|
||||||
|
return obj.isoformat()
|
||||||
|
return obj
|
||||||
|
|
||||||
|
def datetime_parser(dct):
|
||||||
|
for key, value in dct.items():
|
||||||
|
if isinstance(value, str):
|
||||||
|
try:
|
||||||
|
dct[key] = datetime.fromisoformat(value)
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
return dct
|
||||||
|
|
||||||
|
def load_cache(cache_file):
|
||||||
|
if os.path.exists(cache_file):
|
||||||
|
with open(cache_file, "r") as file:
|
||||||
|
return json.load(file, object_hook=datetime_parser)
|
||||||
|
return None
|
||||||
|
|
||||||
|
def save_cache(cache_file, data):
|
||||||
|
with open(cache_file, "w") as file:
|
||||||
|
json.dump(data, file, default=datetime_converter, indent=4)
|
||||||
|
|
||||||
|
|
||||||
|
def get_ongoing_or_next_event(url, username, password):
|
||||||
|
now = datetime.now(timezone.utc)
|
||||||
|
|
||||||
|
try:
|
||||||
|
client = DAVClient(url, username=username, password=password)
|
||||||
|
principal = client.principal()
|
||||||
|
calendars = principal.calendars()
|
||||||
|
|
||||||
|
next_event_dict = {
|
||||||
|
'event_name': "fake",
|
||||||
|
'event_begin': datetime(9000, 1, 1, tzinfo=UTC), # in the year 9000
|
||||||
|
'event_end': datetime(9000, 1, 1, 8, tzinfo=UTC),
|
||||||
|
}
|
||||||
|
|
||||||
|
for calendar in calendars:
|
||||||
|
for event in calendar.search(start=now):
|
||||||
|
calendar_parsed = Calendar(event.data)
|
||||||
|
for ics_event in calendar_parsed.events:
|
||||||
|
event_dict = {}
|
||||||
|
event_dict['event_name'] = ics_event.name or "(No Title)"
|
||||||
|
event_dict['event_begin'] = ics_event.begin.astimezone(timezone.utc)
|
||||||
|
event_dict['event_end'] = ics_event.end.astimezone(timezone.utc)
|
||||||
|
|
||||||
|
if event_dict['event_begin'] <= now and now <= event_dict['event_end']:
|
||||||
|
return event_dict
|
||||||
|
elif event_dict['event_begin'] >= now and next_event_dict['event_begin'] > event_dict['event_begin']:
|
||||||
|
next_event_dict = event_dict
|
||||||
|
return next_event_dict
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error accessing {url}: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
def is_expired(event_dict: dict):
|
||||||
|
now = datetime.now(timezone.utc).timestamp()
|
||||||
|
event_end = event_dict['event_end'].timestamp()
|
||||||
|
return not (now <= event_end)
|
||||||
|
|
||||||
|
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.${hyper.domain}/"
|
||||||
|
username = "nx2"
|
||||||
|
password = get_password(password_file)
|
||||||
|
now = datetime.now(timezone.utc).timestamp()
|
||||||
|
|
||||||
|
event_dict = load_cache(cache_file)
|
||||||
|
|
||||||
|
if (event_dict is None) or (is_expired(event_dict)):
|
||||||
|
event_dict = get_ongoing_or_next_event(url, username, password)
|
||||||
|
save_cache(cache_file, event_dict)
|
||||||
|
|
||||||
|
if event_dict is None: # none were found
|
||||||
|
print("* zen *")
|
||||||
|
exit(0)
|
||||||
|
|
||||||
|
event_start = event_dict['event_begin'].timestamp()
|
||||||
|
event_end = event_dict['event_end'].timestamp()
|
||||||
|
|
||||||
|
if event_start <= now <= event_end: # is currently ongoing
|
||||||
|
action_string = "ends"
|
||||||
|
t = event_end - now # time_remaining
|
||||||
|
else: # is in the future
|
||||||
|
action_string = "starts"
|
||||||
|
t = event_start - now # time_remaining
|
||||||
|
|
||||||
|
hours, rem = divmod(int(t), 3600)
|
||||||
|
minutes, _ = divmod(rem, 60)
|
||||||
|
hour_string = f"{hours} hour{'s ' if hours != 1 else ' '}" if hours > 0 else ""
|
||||||
|
minu_string = f"{minutes} minute{'s ' if minutes != 1 else ' '}" if minutes > 0 else ""
|
||||||
|
if hour_string == "" and minu_string == "":
|
||||||
|
time_string = "now"
|
||||||
|
elif hour_string == "" or minu_string == "":
|
||||||
|
time_string = "in " + hour_string + minu_string
|
||||||
|
else:
|
||||||
|
time_string = "in " + hour_string + "and " + minu_string
|
||||||
|
|
||||||
|
print(f"{event_dict['event_name']} {action_string} {time_string}")
|
||||||
|
'')
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
{ pkgs, ... }@all: with all; {
|
{ pkgs, ... }: let
|
||||||
home.packages = with pkgs; [
|
sep = " ";
|
||||||
|
in {
|
||||||
(pkgs.writeShellApplication { name = "cclock"; text = /*bash*/ ''
|
home.packages = [
|
||||||
|
(pkgs.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"))))}')
|
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
|
if [ $# -eq 0 ]; then
|
||||||
echo "${sep}$(date +'%A the')" "$ord" "of" "$(date +'%B')" " ${sep}$(date +'%R')"
|
echo "${sep}$(date +'%A the')" "$ord" "of" "$(date +'%B')" " ${sep}$(date +'%R')"
|
||||||
@@ -9,125 +10,5 @@
|
|||||||
echo "$(date +'%A the')" "$ord" "of" "$(date +'%B')" "$(date +'%R')"
|
echo "$(date +'%A the')" "$ord" "of" "$(date +'%B')" "$(date +'%R')"
|
||||||
fi
|
fi
|
||||||
'';})
|
'';})
|
||||||
|
];
|
||||||
(pkgs.writers.writePython3Bin "caldav_event" {
|
|
||||||
libraries = with pkgs.python3Packages; [ caldav ics pytz ];
|
|
||||||
flakeIgnore = [ "E302" "E305" "E501" "E261" ];
|
|
||||||
} /* python */ ''
|
|
||||||
import os
|
|
||||||
import json
|
|
||||||
from caldav import DAVClient
|
|
||||||
from datetime import datetime, timezone
|
|
||||||
from ics import Calendar
|
|
||||||
from pytz import UTC
|
|
||||||
|
|
||||||
def get_password(password_file):
|
|
||||||
with open(password_file, "r") as file:
|
|
||||||
return file.read().strip()
|
|
||||||
|
|
||||||
def datetime_converter(obj):
|
|
||||||
if isinstance(obj, datetime):
|
|
||||||
return obj.isoformat()
|
|
||||||
return obj
|
|
||||||
|
|
||||||
def datetime_parser(dct):
|
|
||||||
for key, value in dct.items():
|
|
||||||
if isinstance(value, str):
|
|
||||||
try:
|
|
||||||
dct[key] = datetime.fromisoformat(value)
|
|
||||||
except ValueError:
|
|
||||||
pass
|
|
||||||
return dct
|
|
||||||
|
|
||||||
def load_cache(cache_file):
|
|
||||||
if os.path.exists(cache_file):
|
|
||||||
with open(cache_file, "r") as file:
|
|
||||||
return json.load(file, object_hook=datetime_parser)
|
|
||||||
return None
|
|
||||||
|
|
||||||
def save_cache(cache_file, data):
|
|
||||||
with open(cache_file, "w") as file:
|
|
||||||
json.dump(data, file, default=datetime_converter, indent=4)
|
|
||||||
|
|
||||||
|
|
||||||
def get_ongoing_or_next_event(url, username, password):
|
|
||||||
now = datetime.now(timezone.utc)
|
|
||||||
|
|
||||||
try:
|
|
||||||
client = DAVClient(url, username=username, password=password)
|
|
||||||
principal = client.principal()
|
|
||||||
calendars = principal.calendars()
|
|
||||||
|
|
||||||
next_event_dict = {
|
|
||||||
'event_name': "fake",
|
|
||||||
'event_begin': datetime(9000, 1, 1, tzinfo=UTC), # in the year 9000
|
|
||||||
'event_end': datetime(9000, 1, 1, 8, tzinfo=UTC),
|
|
||||||
}
|
|
||||||
|
|
||||||
for calendar in calendars:
|
|
||||||
for event in calendar.search(start=now):
|
|
||||||
calendar_parsed = Calendar(event.data)
|
|
||||||
for ics_event in calendar_parsed.events:
|
|
||||||
event_dict = {}
|
|
||||||
event_dict['event_name'] = ics_event.name or "(No Title)"
|
|
||||||
event_dict['event_begin'] = ics_event.begin.astimezone(timezone.utc)
|
|
||||||
event_dict['event_end'] = ics_event.end.astimezone(timezone.utc)
|
|
||||||
|
|
||||||
if event_dict['event_begin'] <= now and now <= event_dict['event_end']:
|
|
||||||
return event_dict
|
|
||||||
elif event_dict['event_begin'] >= now and next_event_dict['event_begin'] > event_dict['event_begin']:
|
|
||||||
next_event_dict = event_dict
|
|
||||||
return next_event_dict
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error accessing {url}: {e}")
|
|
||||||
return None
|
|
||||||
|
|
||||||
def is_expired(event_dict: dict):
|
|
||||||
now = datetime.now(timezone.utc).timestamp()
|
|
||||||
event_end = event_dict['event_end'].timestamp()
|
|
||||||
return not (now <= event_end)
|
|
||||||
|
|
||||||
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.${hyper.domain}/"
|
|
||||||
username = "nx2"
|
|
||||||
password = get_password(password_file)
|
|
||||||
now = datetime.now(timezone.utc).timestamp()
|
|
||||||
|
|
||||||
event_dict = load_cache(cache_file)
|
|
||||||
|
|
||||||
if (event_dict is None) or (is_expired(event_dict)):
|
|
||||||
event_dict = get_ongoing_or_next_event(url, username, password)
|
|
||||||
save_cache(cache_file, event_dict)
|
|
||||||
|
|
||||||
if event_dict is None: # none were found
|
|
||||||
print("* zen *")
|
|
||||||
exit(0)
|
|
||||||
|
|
||||||
event_start = event_dict['event_begin'].timestamp()
|
|
||||||
event_end = event_dict['event_end'].timestamp()
|
|
||||||
|
|
||||||
if event_start <= now <= event_end: # is currently ongoing
|
|
||||||
action_string = "ends"
|
|
||||||
t = event_end - now # time_remaining
|
|
||||||
else: # is in the future
|
|
||||||
action_string = "starts"
|
|
||||||
t = event_start - now # time_remaining
|
|
||||||
|
|
||||||
hours, rem = divmod(int(t), 3600)
|
|
||||||
minutes, _ = divmod(rem, 60)
|
|
||||||
hour_string = f"{hours} hour{'s ' if hours != 1 else ' '}" if hours > 0 else ""
|
|
||||||
minu_string = f"{minutes} minute{'s ' if minutes != 1 else ' '}" if minutes > 0 else ""
|
|
||||||
if hour_string == "" and minu_string == "":
|
|
||||||
time_string = "now"
|
|
||||||
elif hour_string == "" or minu_string == "":
|
|
||||||
time_string = "in " + hour_string + minu_string
|
|
||||||
else:
|
|
||||||
time_string = "in " + hour_string + "and " + minu_string
|
|
||||||
|
|
||||||
print(f"{event_dict['event_name']} {action_string} {time_string}")
|
|
||||||
'')
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
xdg.configFile = {
|
xdg.configFile = {
|
||||||
"hyprpanel/modules.scss".text = with rice.color; /* scss */ ''
|
"hyprpanel/modules.scss".text = with rice.color; /* scss */ ''
|
||||||
@include styleModule('cmodule-cclock', (
|
@include styleModule('cmodule-cclock', (
|
||||||
'text-color': #${accent.base},
|
'text-color': ${accent.base},
|
||||||
/* 'icon-color': , */
|
/* 'icon-color': , */
|
||||||
/* 'icon-background': , */
|
/* 'icon-background': , */
|
||||||
/* 'label-background': #242438, */
|
/* 'label-background': #242438, */
|
||||||
@@ -12,9 +12,27 @@
|
|||||||
/* 'icon-size': 1.2em */
|
/* 'icon-size': 1.2em */
|
||||||
));
|
));
|
||||||
@include styleModule('cmodule-caldav_event', (
|
@include styleModule('cmodule-caldav_event', (
|
||||||
'text-color': #${accent.base},
|
'text-color': ${accent.base},
|
||||||
));
|
));
|
||||||
'';
|
'';
|
||||||
|
"hyprpanel/modules.json".text = builtins.toJSON {
|
||||||
|
"custom/cclock" = {
|
||||||
|
execute = "cclock";
|
||||||
|
executeOnAction = "";
|
||||||
|
label = "{}";
|
||||||
|
interval = 60000;
|
||||||
|
hideOnEmpty = true;
|
||||||
|
actions.onLeftClick = "menu:calendar";
|
||||||
|
};
|
||||||
|
"custom/caldav_evnet" = {
|
||||||
|
execute = "caldav_event";
|
||||||
|
executeOnAction = "";
|
||||||
|
label = "{}";
|
||||||
|
interval = 60000;
|
||||||
|
hideOnEmpty = true;
|
||||||
|
actions = {};
|
||||||
|
};
|
||||||
|
};
|
||||||
};
|
};
|
||||||
programs.hyprpanel = {
|
programs.hyprpanel = {
|
||||||
enable = true;
|
enable = true;
|
||||||
|
|||||||
@@ -1,38 +1,37 @@
|
|||||||
{ pkgs, ... }@all: with all; {
|
{ pkgs, ... }: {
|
||||||
home.packages = with pkgs; [
|
home.packages = [
|
||||||
|
(pkgs.writeShellApplication { name = "submap_indicator"; text = /*bash*/ ''
|
||||||
(pkgs.writeShellApplication { name = "submap_indicator"; text = /*bash*/ ''
|
print_help() {
|
||||||
print_help() {
|
echo "Usage: submap_indicator {set <string>|unset}"
|
||||||
echo "Usage: submap_indicator {set <string>|unset}"
|
}
|
||||||
}
|
if [ $# -lt 1 ]; then
|
||||||
if [ $# -lt 1 ]; then
|
print_help; exit 1;
|
||||||
print_help; exit 1;
|
fi
|
||||||
fi
|
case "$1" in
|
||||||
case "$1" in
|
set)
|
||||||
set)
|
# Check if there is a second argument for the 'set' operation
|
||||||
# Check if there is a second argument for the 'set' operation
|
if [ $# -eq 2 ]; then
|
||||||
if [ $# -eq 2 ]; then
|
echo "$2" > /tmp/submap-indictor
|
||||||
echo "$2" > /tmp/submap-indictor
|
pkill -RTMIN+8 waybar
|
||||||
pkill -RTMIN+8 waybar
|
pkill -RTMIN+8 hyprpanel
|
||||||
pkill -RTMIN+8 hyprpanel
|
else
|
||||||
else
|
echo "Error: 'set' operation requires exactly one string argument."
|
||||||
echo "Error: 'set' operation requires exactly one string argument."
|
print_help
|
||||||
print_help
|
exit 1
|
||||||
exit 1
|
fi
|
||||||
fi
|
;;
|
||||||
;;
|
unset)
|
||||||
unset)
|
echo "" > /tmp/submap-indictor
|
||||||
echo "" > /tmp/submap-indictor
|
pkill -RTMIN+8 waybar
|
||||||
pkill -RTMIN+8 waybar
|
pkill -RTMIN+8 hyprpanel
|
||||||
pkill -RTMIN+8 hyprpanel
|
;;
|
||||||
;;
|
*)
|
||||||
*)
|
echo "Error: Unknown command '$1'"
|
||||||
echo "Error: Unknown command '$1'"
|
print_help
|
||||||
print_help
|
exit 1
|
||||||
exit 1
|
;;
|
||||||
;;
|
esac
|
||||||
esac
|
exit 0
|
||||||
exit 0
|
'';})
|
||||||
'';})
|
];
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
{ pkgs, ... }@all: with all; {
|
{ pkgs, ... }@all: with all; let
|
||||||
|
sep = " ";
|
||||||
|
in {
|
||||||
programs.waybar = {
|
programs.waybar = {
|
||||||
enable = false;
|
enable = false;
|
||||||
package = pkgs.waybar;
|
package = pkgs.waybar;
|
||||||
|
|||||||
Reference in New Issue
Block a user