refactor wallpaper

This commit is contained in:
Sean Kovacs 2026-01-24 01:43:15 -05:00
commit 0368b5ff69
Signed by: sckova
GPG key ID: 00F325187C68651A

View file

@ -3,67 +3,6 @@
config, config,
... ...
}: }:
let
script = pkgs.writeShellScript "bing-wallpaper" ''
set -euo pipefail
# Configuration
SIZE="UHD"
MARKET="en-US"
OUTPUT_PATH="''${XDG_DATA_HOME:-$HOME/.local/share}/wallpaper/daily.jpg"
DAY="0"
# API configuration
BING="https://www.bing.com"
API="/HPImageArchive.aspx"
REQ_IMG="$BING$API?format=js&idx=$DAY&mkt=$MARKET&n=1"
echo "Pinging Bing API..."
# Fetch API response
API_RESP=$(${pkgs.wget}/bin/wget --quiet --output-document=- "$REQ_IMG")
if (( $? > 0 )); then
echo "Ping failed!"
exit 1
fi
# Extract image URL base
URL_BASE=$(echo "$API_RESP" | ${pkgs.gnugrep}/bin/grep -oP 'urlbase":"[^"]*' | ${pkgs.coreutils}/bin/cut -d '"' -f 3)
REQ_IMG_URL="$BING$URL_BASE\_$SIZE.jpg"
# Extract title
TITLE=$(echo "$API_RESP" | ${pkgs.gnugrep}/bin/grep -oP 'title":"[^"]*' | ${pkgs.coreutils}/bin/cut -d '"' -f 3)
echo "Bing Image of the day: $TITLE"
# Check if specific size exists, fallback to default
if ! ${pkgs.wget}/bin/wget --quiet --spider --max-redirect 0 "$REQ_IMG_URL"; then
REQ_IMG_URL="$BING$(echo "$API_RESP" | ${pkgs.gnugrep}/bin/grep -oP 'url":"[^"]*' | ${pkgs.coreutils}/bin/cut -d '"' -f 3)"
fi
echo "$REQ_IMG_URL"
# Extract filename
IMG_NAME="''${REQ_IMG_URL##*/}"
IMG_NAME="''${IMG_NAME#th?id=OHR.}"
IMG_NAME="''${IMG_NAME%&rf=*}"
echo "$IMG_NAME"
# Create parent directory
${pkgs.coreutils}/bin/mkdir -p "$(${pkgs.coreutils}/bin/dirname "$OUTPUT_PATH")"
# Download image, overwrite if exists
${pkgs.wget}/bin/wget --quiet --output-document="$OUTPUT_PATH" "$REQ_IMG_URL"
echo "Wallpaper saved to $OUTPUT_PATH"
# Send notification
if command -v ${pkgs.libnotify}/bin/notify-send &> /dev/null; then
${pkgs.libnotify}/bin/notify-send -u low -t 10000 -i preferences-desktop-wallpaper \
"Bing Wallpaper of the Day" "$TITLE"
fi
echo "Wallpaper downloaded and applied successfully."
'';
in
{ {
home.packages = with pkgs; [ home.packages = with pkgs; [
wpaperd wpaperd
@ -73,7 +12,6 @@ in
text = '' text = ''
[default] [default]
mode = "center" mode = "center"
[any] [any]
path = "/home/${config.userOptions.username}/.local/share/wallpaper/daily.jpg" path = "/home/${config.userOptions.username}/.local/share/wallpaper/daily.jpg"
''; '';
@ -91,33 +29,40 @@ in
ExecStart = "${pkgs.wpaperd}/bin/wpaperd"; ExecStart = "${pkgs.wpaperd}/bin/wpaperd";
}; };
Install = { Install = {
WantedBy = [ WantedBy = [ "niri.service" ];
"niri.service"
];
}; };
}; };
systemd.user.services.bing-wallpaper = { systemd.user.services.bing-wallpaper = {
Unit = { Unit = {
Description = "Download and set Bing wallpaper of the day"; Description = "Download and set Bing wallpaper of the day";
After = [ After = [ "network-online.target" ];
"network-online.target" Wants = [ "network-online.target" ];
];
Wants = [
"network-online.target"
];
}; };
Service = { Service = {
Type = "oneshot"; Type = "oneshot";
ExecStart = "${script}"; ExecStart = pkgs.writeShellScript "bing-wallpaper" ''
OUT="''${XDG_DATA_HOME:-$HOME/.local/share}/wallpaper/daily.jpg"
API_RESP=$(${pkgs.wget}/bin/wget -qO- "https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&mkt=en-US&n=1") || exit 1
URL_BASE=$(echo "$API_RESP" | ${pkgs.gnugrep}/bin/grep -oP 'urlbase":"[^"]*' | cut -d '"' -f 3)
TITLE=$(echo "$API_RESP" | ${pkgs.gnugrep}/bin/grep -oP 'title":"[^"]*' | cut -d '"' -f 3)
${pkgs.coreutils}/bin/mkdir -p "$(dirname "$OUT")"
${pkgs.wget}/bin/wget -qO "$OUT" "https://www.bing.com$URL_BASE\_UHD.jpg" || \
${pkgs.wget}/bin/wget -qO "$OUT" "https://www.bing.com$(echo "$API_RESP" | ${pkgs.gnugrep}/bin/grep -oP 'url":"[^"]*' | cut -d '"' -f 3)"
${pkgs.libnotify}/bin/notify-send \
-a "Bing Wallpaper Service" \
-u low \
-i preferences-desktop-wallpaper \
"$TITLE"
'';
ExecStartPost = "${pkgs.systemd}/bin/systemctl --user restart wpaperd.service"; ExecStartPost = "${pkgs.systemd}/bin/systemctl --user restart wpaperd.service";
}; };
Install = { Install = {
WantedBy = [ WantedBy = [ "niri.service" ];
"niri.service"
];
}; };
}; };