summaryrefslogtreecommitdiff
path: root/arch/scripts/.local/bin
diff options
context:
space:
mode:
authorsteven-na <noreply.github@stvnc.dev>2026-04-27 12:54:48 -0700
committersteven-na <noreply.github@stvnc.dev>2026-04-27 12:54:48 -0700
commitf131fac65e297c1322edf88bd571a4282171596d (patch)
tree73ad47cdd31c2e76a1018ce2748851dbe051a9cf /arch/scripts/.local/bin
parent525bb75b51c640f8960463d372ad599a6fe9f57e (diff)
new stuff
Diffstat (limited to 'arch/scripts/.local/bin')
-rwxr-xr-xarch/scripts/.local/bin/get-new-wallpaper59
1 files changed, 59 insertions, 0 deletions
diff --git a/arch/scripts/.local/bin/get-new-wallpaper b/arch/scripts/.local/bin/get-new-wallpaper
new file mode 100755
index 0000000..1e97af1
--- /dev/null
+++ b/arch/scripts/.local/bin/get-new-wallpaper
@@ -0,0 +1,59 @@
+#!/usr/bin/env bash
+set -euo pipefail
+
+subreddit="EarthPorn"
+wallpaper_dir="$HOME/wallpapers"
+min_width=1920
+mkdir -p "$wallpaper_dir"
+
+json=$(curl -sf -A "Mozilla/5.0 (X11; Linux x86_64) get-new-wallpaper/1.0" \
+ "https://www.reddit.com/r/${subreddit}.json?limit=25")
+
+# Keep posts where:
+# - preview source exists and is at least min_width wide
+# - aspect ratio is wider than 4:3
+# For Reddit-hosted images use i.redd.it directly; for external images fall back
+# to the signed preview.redd.it URL so hotlink protection doesn't bite us.
+mapfile -t candidates < <(echo "$json" | jq -r \
+ --argjson min "$min_width" '
+ .data.children[].data |
+ select(.preview.images[0].source != null) |
+ select(.preview.images[0].source.width >= $min) |
+ select((.preview.images[0].source.width / .preview.images[0].source.height) > (4/3)) |
+ [
+ .title,
+ (if (.url | test("^https://i\\.redd\\.it/")) then .url
+ else (.preview.images[0].source.url | gsub("&amp;"; "&"))
+ end)
+ ] | @tsv
+')
+
+if [[ ${#candidates[@]} -eq 0 ]]; then
+ echo "No suitable images found." >&2
+ exit 1
+fi
+
+selected="${candidates[$((RANDOM % ${#candidates[@]}))]}"
+title=$(printf '%s' "$selected" | cut -f1)
+img_url=$(printf '%s' "$selected" | cut -f2)
+
+safe_name=$(printf '%s' "$title" \
+ | tr '[:upper:]' '[:lower:]' \
+ | sed 's/[^a-z0-9 ]//g' \
+ | tr -s ' ' '-' \
+ | sed 's/^-//; s/-$//' \
+ | cut -c1-60)
+
+ext=$(printf '%s' "$img_url" | grep -oP '\.(jpg|jpeg|png|webp)' | tail -1 | tr -d '.')
+[[ -z "$ext" ]] && ext="jpg"
+
+filename="${wallpaper_dir}/${safe_name}.${ext}"
+
+echo "Downloading: $title"
+curl -sfL \
+ -A "Mozilla/5.0 (X11; Linux x86_64; rv:120.0) Gecko/20100101 Firefox/120.0" \
+ --referer "https://www.reddit.com/" \
+ -o "$filename" "$img_url" || { echo "Download failed." >&2; rm -f "$filename"; exit 1; }
+
+echo "Saved: $filename"
+set-wallpaper "$filename"