Improve conditional for waybar

This commit is contained in:
Torjus Håkestad 2024-04-26 12:45:55 +02:00
parent 944c829f3a
commit a36fbb43be
5 changed files with 190 additions and 283 deletions

View File

@ -0,0 +1,5 @@
{ config, pkgs }:
{
sops.secrets."sonarr_api_key" = { };
sops.secrets."radarr_api_key" = { };
}

View File

@ -0,0 +1,71 @@
#!/usr/bin/env python3
import json
import os
import requests
from datetime import datetime, date, timedelta
SECRET_DIR = "/home/torjus/.config/sops-nix/secrets"
def sonarr_url():
xdg_dir = os.environ["XDG_RUNTIME_DIR"]
if not xdg_dir:
raise Exception("XDG_RUNTIME_DIR not set")
with open(f"{SECRET_DIR}/sonarr_base_url") as f:
return f.read().strip()
def radarr_url():
with open(f"{SECRET_DIR}/radarr_base_url") as f:
return f.read().strip()
def make_header(api_key: str):
return {"X-Api-Key": api_key, "Accept": "application/json"}
def get_sonarr_key():
with open(f"{SECRET_DIR}/sonarr_api_key") as f:
return f.read().strip()
def get_radarr_key():
with open(f"{SECRET_DIR}/radarr_api_key") as f:
return f.read().strip()
def get_sonarr_history(since: datetime|None=None):
api_key = get_sonarr_key()
if not since:
since = datetime.combine(date.today()-timedelta(days=1),datetime.min.time())
url = f"{sonarr_url()}/api/history/since"
url += f"?date={since.isoformat()}"
response = requests.get(url, headers=make_header(api_key))
response.raise_for_status()
data = response.json()
items = []
for item in data:
if item["eventType"] == "downloadFolderImported":
items.append(item["sourceTitle"])
return items
def get_radarr_history(since: datetime|None=None):
api_key = get_radarr_key()
if not since:
since = datetime.combine(date.today()-timedelta(days=7),datetime.min.time())
url = f"{radarr_url()}/api/v3/history/since"
url += f"?date={since.isoformat()}"
response = requests.get(url, headers=make_header(api_key))
response.raise_for_status()
data = response.json()
items = []
for item in data:
if item["eventType"] == "downloadFolderImported":
items.append(item["sourceTitle"])
return items
if __name__ == "__main__":
sonarr_items = get_sonarr_history()
radarr_items = get_radarr_history()
output = {
"text": f"Son: {len(sonarr_items)}|Rad: {len(radarr_items)}",
"tooltip": "\n".join(radarr_items) + "\n".join(sonarr_items)
}
print(json.dumps(output))

View File

@ -8,8 +8,26 @@ let
]; ];
text = builtins.readFile ./flakestat.sh; text = builtins.readFile ./flakestat.sh;
}; };
arrhist = pkgs.stdenv.mkDerivation {
name = "arrhist";
propagatedBuildInputs = [
(pkgs.python3.withPackages (pythonPackages: with pythonPackages; [
requests
]))
];
src = ./arrhist.py;
dontUnpack = true;
installPhase = "install -Dm755 ${./arrhist.py} $out/bin/arrhist";
};
withArrhist = if (osConfig.system.name == "gunter") then true else false;
withBattery = if (osConfig.system.name == "magicman") then true else false;
in in
{ {
sops.secrets."sonarr_base_url" = { };
sops.secrets."sonarr_api_key" = { };
sops.secrets."radarr_base_url" = { };
sops.secrets."radarr_api_key" = { };
xdg.configFile."waybar/macchiato.css" = { xdg.configFile."waybar/macchiato.css" = {
source = pkgs.fetchFromGitHub source = pkgs.fetchFromGitHub
{ {
@ -108,6 +126,7 @@ in
#battery, #battery,
#custom-powermenu, #custom-powermenu,
#custom-flakestat, #custom-flakestat,
#custom-arrhist,
#custom-cava-internal { #custom-cava-internal {
padding-left: 10px; padding-left: 10px;
padding-right: 10px; padding-right: 10px;
@ -149,86 +168,95 @@ in
font-family: "Hack Nerd Font"; font-family: "Hack Nerd Font";
} }
''; '';
settings = [{ settings = [
"layer" = "top"; ({
"position" = "top"; "layer" = "top";
modules-left = [ "custom/launcher" "hyprland/workspaces" ]; "position" = "top";
modules-center = [ "clock" ]; modules-left = [ "custom/launcher" "hyprland/workspaces" ];
modules-right = modules-center = [ "clock" ];
[ modules-right =
"custom/flakestat" [
"pulseaudio" (lib.mkIf (withArrhist) "custom/arrhist")
"pulseaudio#microphone" "custom/flakestat"
"memory" "pulseaudio"
"cpu" "pulseaudio#microphone"
"tray" "memory"
(lib.mkIf (osConfig.system.name == "magicman") "battery") "cpu"
]; "tray"
"custom/launcher" = { (lib.mkIf (withBattery) "battery")
"format" = " "; ];
"on-click" = "pkill rofi || ~/.config/rofi/launcher.sh"; "custom/launcher" = {
"tooltip" = false; "format" = " ";
}; "on-click" = "pkill rofi || ~/.config/rofi/launcher.sh";
"hyprland/workspaces" = { "tooltip" = false;
"format" = "{name}";
"on-click" = "activate";
"on-scroll-up" = "hyprctl dispatch workspace e+1";
"on-scroll-down" = "hyprctl dispatch workspace e-1";
};
"pulseaudio" = {
"scroll-step" = 5;
"format" = "{icon} {volume}%";
"format-muted" = "";
"format-icons" = { "default" = [ "" "" "" ]; };
"on-click" = "pamixer -t";
};
"pulseaudio#microphone" = {
"format" = "{format_source}";
"format-source" = " {volume}%";
"format-source-muted" = "";
"on-click" = "pamixer --default-source -t";
"on-scroll-up" = "pamixer --default-source -i 5";
"on-scroll-down" = "pamixer --default-source -d 5";
"scroll-step" = 5;
"on-click-right" = "pavucontrol";
};
"clock" = {
"interval" = 1;
"format" = "{:%H:%M %A %b %d}";
"tooltip" = true;
};
"memory" = {
"interval" = 3;
"format" = " {percentage}%";
"states" = { "warning" = 85; };
};
"cpu" = {
"interval" = 3;
"format" = " {usage}%";
};
"tray" = {
"icon-size" = 15;
"spacing" = 6;
};
"battery" = {
"interval" = 60;
"states" = {
"warning" = 20;
"critical" = 5;
}; };
"format" = "{icon} {capacity}%"; "hyprland/workspaces" = {
"format-icons" = [ "format" = "{name}";
"" "on-click" = "activate";
"" "on-scroll-up" = "hyprctl dispatch workspace e+1";
"" "on-scroll-down" = "hyprctl dispatch workspace e-1";
"" };
"" "pulseaudio" = {
]; "scroll-step" = 5;
}; "format" = "{icon} {volume}%";
"custom/flakestat" = { "format-muted" = "";
"exec" = "${flakestat}/bin/flakestat"; "format-icons" = { "default" = [ "" "" "" ]; };
"interval" = 600; "on-click" = "pamixer -t";
}; };
}]; "pulseaudio#microphone" = {
"format" = "{format_source}";
"format-source" = " {volume}%";
"format-source-muted" = "";
"on-click" = "pamixer --default-source -t";
"on-scroll-up" = "pamixer --default-source -i 5";
"on-scroll-down" = "pamixer --default-source -d 5";
"scroll-step" = 5;
"on-click-right" = "pavucontrol";
};
"clock" = {
"interval" = 1;
"format" = "{:%H:%M %A %b %d}";
"tooltip" = true;
};
"memory" = {
"interval" = 3;
"format" = " {percentage}%";
"states" = { "warning" = 85; };
};
"cpu" = {
"interval" = 3;
"format" = " {usage}%";
};
"tray" = {
"icon-size" = 15;
"spacing" = 6;
};
"battery" = {
"interval" = 60;
"states" = {
"warning" = 20;
"critical" = 5;
};
"format" = "{icon} {capacity}%";
"format-icons" = [
""
""
""
""
""
];
};
"custom/flakestat" = {
"exec" = "${flakestat}/bin/flakestat";
"interval" = 600;
};
} // lib.optionalAttrs (withArrhist) {
"custom/arrhist" = {
"exec" = "${arrhist}/bin/arrhist";
"return-type" = "json";
"interval" = 30;
};
})
];
}; };
} }

View File

@ -1,201 +0,0 @@
{ pkgs, ... }:
{
programs.waybar = {
enable = true;
systemd = {
enable = false; # disable it,autostart it in hyprland conf
target = "graphical-session.target";
};
style = ''
* {
font-family: "FiraCodeNerdFont-Regular";
font-size: 14px;
font-weight: bold;
border-radius: 0px;
transition-property: background-color;
transition-duration: 0.5s;
}
@keyframes blink_red {
to {
background-color: rgb(242, 143, 173);
color: rgb(26, 24, 38);
}
}
.warning,
.critical,
.urgent {
animation-name: blink_red;
animation-duration: 1s;
animation-timing-function: linear;
animation-iteration-count: infinite;
animation-direction: alternate;
}
window#waybar {
background-color: transparent;
}
window > box {
margin-left: 0px;
margin-right: 0px;
margin-top: 0px;
border-bottom: 2px solid @color1;
background-color: @background;
}
#workspaces {
padding-left: 0px;
padding-right: 4px;
}
#workspaces button {
padding-top: 5px;
padding-bottom: 5px;
padding-left: 6px;
padding-right: 6px;
color: @color1;
}
#workspaces button.active {
background-color: @color1;
color: rgb(26, 24, 38);
}
#workspaces button.urgent {
color: rgb(26, 24, 38);
}
#workspaces button:hover {
background-color: #b38dac;
color: rgb(26, 24, 38);
}
tooltip {
background: #3b4253;
}
tooltip label {
color: #e4e8ef;
}
#custom-launcher {
font-size: 20px;
padding-left: 8px;
padding-right: 6px;
color: #7ebae4;
}
#mode,
#clock,
#memory,
#temperature,
#cpu,
#mpd,
#custom-wall,
#temperature,
#backlight,
#pulseaudio,
#network,
#battery,
#custom-powermenu,
#custom-cava-internal {
padding-left: 10px;
padding-right: 10px;
color: @color1;
}
#network.disconnected {
color: #cccccc;
}
#battery.charging,
#battery.full,
#battery.discharging {
color: #cf876f;
}
#battery.critical:not(.charging) {
color: #d6dce7;
}
#custom-powermenu {
color: #bd6069;
}
#tray {
padding-right: 8px;
padding-left: 10px;
}
#tray menu {
background: #3b4252;
color: #dee2ea;
}
#mpd.paused {
color: rgb(192, 202, 245);
font-style: italic;
}
#mpd.stopped {
background: transparent;
}
#mpd {
color: #e4e8ef;
}
#custom-cava-internal {
font-family: "Hack Nerd Font";
}
'';
settings = [{
"layer" = "top";
"position" = "top";
modules-left = [ "custom/launcher" "hyprland/workspaces" ];
modules-center = [ "clock" ];
modules-right =
[ "pulseaudio" "pulseaudio#microphone" "memory" "cpu" "tray" "battery" ];
"custom/launcher" = {
"format" = " ";
"on-click" = "pkill rofi || ~/.config/rofi/launcher.sh";
"tooltip" = false;
};
"hyprland/workspaces" = {
"format" = "{name}";
"on-click" = "activate";
"on-scroll-up" = "hyprctl dispatch workspace e+1";
"on-scroll-down" = "hyprctl dispatch workspace e-1";
};
"pulseaudio" = {
"scroll-step" = 5;
"format" = "{icon} {volume}%";
"format-muted" = "";
"format-icons" = { "default" = [ "" "" "" ]; };
"on-click" = "pamixer -t";
};
"pulseaudio#microphone" = {
"format" = "{format_source}";
"format-source" = " {volume}%";
"format-source-muted" = "";
"on-click" = "pamixer --default-source -t";
"on-scroll-up" = "pamixer --default-source -i 5";
"on-scroll-down" = "pamixer --default-source -d 5";
"scroll-step" = 5;
"on-click-right" = "pavucontrol";
};
"clock" = {
"interval" = 1;
"format" = "{:%H:%M %A %b %d}";
"tooltip" = true;
};
"memory" = {
"interval" = 3;
"format" = " {percentage}%";
"states" = { "warning" = 85; };
};
"cpu" = {
"interval" = 3;
"format" = " {usage}%";
};
"tray" = {
"icon-size" = 15;
"spacing" = 6;
};
"battery" = {
"interval" = 60;
"states" = {
"warning" = 30;
"critical" = 5;
};
"format" = "{icon} {capacity}%";
"format-icons" = [
""
""
""
""
""
];
};
}];
};
}

View File

@ -1,4 +1,8 @@
gotify_backup_home: ENC[AES256_GCM,data:DV22pltF1db7mP8dK4fb,iv:487nKwVToOX2KSBmz3pp1T0wwi2JTMZzwH2arp8DatA=,tag:uVmONZ1fznTXDxySh+xXvA==,type:str] gotify_backup_home: ENC[AES256_GCM,data:DV22pltF1db7mP8dK4fb,iv:487nKwVToOX2KSBmz3pp1T0wwi2JTMZzwH2arp8DatA=,tag:uVmONZ1fznTXDxySh+xXvA==,type:str]
sonarr_api_key: ENC[AES256_GCM,data:mg19hxs+DY6wsLjt4FupjavwmnmMJAP2Us5i1Rt/yyk=,iv:6thnP4JSsi51IdEdKXX60OeT2Xr5qMplwsKlk8XTOg8=,tag:cAkhxe2Fjdudzy/WZ6bNqg==,type:str]
sonarr_base_url: ENC[AES256_GCM,data:0HiHIX4KcPEE62Ti1fLH230rC1A7xYg=,iv:mkAnl6t2H5xP9RPjTsbSZkfbrgli/7XKnPE5aGuZpTo=,tag:P2osFM60Jk8qkXJgLaGpjg==,type:str]
radarr_api_key: ENC[AES256_GCM,data:Db1ISKTF+m2H1on55/4vdGticfqBdxfIzKHBxC9LAx4=,iv:NhiG4SmsRYIunW1ljFbxeHvRoi9fOVE+9DJn6kmZ6oI=,tag:DoJzo56CW3kJlySYmB8NYQ==,type:str]
radarr_base_url: ENC[AES256_GCM,data:3UgOPQMblYhm0ysRB6VVosvZToIM5IA=,iv:o/s0bVBrjrma2Df2LlCCFL5Ks80063/4mABc6vzDrYg=,tag:eHKntLPM9yRRkMfIWSpIdg==,type:str]
sops: sops:
kms: [] kms: []
gcp_kms: [] gcp_kms: []
@ -14,8 +18,8 @@ sops:
Q3gxalhGVjNlS3B3YlFsK0VQMUFITEUKE87+RpOG6ucXHHQ0DMQ9F3yo0n1aXbv7 Q3gxalhGVjNlS3B3YlFsK0VQMUFITEUKE87+RpOG6ucXHHQ0DMQ9F3yo0n1aXbv7
OX5ibHU7RroUQwFmDj87u59VUTvpWRQjsBW4c4WrZRk9KcjwinZZZQ== OX5ibHU7RroUQwFmDj87u59VUTvpWRQjsBW4c4WrZRk9KcjwinZZZQ==
-----END AGE ENCRYPTED FILE----- -----END AGE ENCRYPTED FILE-----
lastmodified: "2024-03-06T20:24:35Z" lastmodified: "2024-04-25T19:19:54Z"
mac: ENC[AES256_GCM,data:1+fxim6Z9BLqpxRVUse5yfGyv5Y1OYLnWjuw//WtPU1Y1noXQC2SapbqaMgrJo5wDddom41RnOnJw7wjXLmA4cKndcrmotpXQIq1gYFrQtDoVuZjhcBzbY4rQiHUsMtQHQXFvn9SpreO5RMz9o5Zl25cWe1txH0K/DqavHlh+1c=,iv:1sWteDKqcDTPBfnFVSVO0V1JBfw9aj2OC/K0mVbwsdI=,tag:/AlPi/Y0Ztd6KghctMD9jg==,type:str] mac: ENC[AES256_GCM,data:VGBiDi71DHAXLhi7XC0XSTqnSwcJXv1Lj53qriFER7BXXZNPUdbeknlYR+KMdL3hgKGiK+ElWK5foDAy6jpl1H3U7Y9B4d40pVZSzEoN+fCwUgfP+yym1HwKZZoJok2ksXZIL4MZyZSNS+ONjDeFEcyHobIx8pRThxic3CcvptI=,iv:QwnFcYeIWibx5q8C/ur1eE8F9vbyGHg5raInDHBoyVs=,tag:JJWEYAyVhfny4hWrKBAKig==,type:str]
pgp: [] pgp: []
unencrypted_suffix: _unencrypted unencrypted_suffix: _unencrypted
version: 3.8.1 version: 3.8.1