Skip to content

Instantly share code, notes, and snippets.

@panicbit
Last active December 20, 2015 00:50
Show Gist options
  • Save panicbit/4a00231c783dea1710de to your computer and use it in GitHub Desktop.
Save panicbit/4a00231c783dea1710de to your computer and use it in GitHub Desktop.
#!/bin/bash
SUBREDDIT="winterporn"
MIN_WIDTH="1920"
MIN_HEIGHT="1080"
DLPATH="${HOME}/.cache/reddit_wallpaper"
if [ -z "$1" ]; then
NTH="0"
else
NTH="$1"
fi
# Define how to set the wallpaper.
# The path to the wallpaper is passed as $1
function set_wallpaper {
fbsetbg -a "$1"
}
# Grab a wallpaper url from reddit
IMGURL="$((curl "https://www.reddit.com/r/${SUBREDDIT}.json" || exit 1) | jq -r '
.data.children
| map(select(.data.url | test(".*(jpg|png)$")).data)
| map({url: .url, res: .preview.images[0].source
| del (.url), ups: .ups})
| map(select(.res | .width >= '${MIN_WIDTH}' and .height >= '${MIN_HEIGHT}'))
| sort_by(.ups)
| reverse
| .['${NTH}'].url
')"
# Create wallpaper directory
mkdir -p "${DLPATH}" || exit 1
# Check if this wallpaper is the same as last time
if [ -e "${DLPATH}/lasturl" ]; then
if [ "${IMGURL}" = "$(cat "${DLPATH}/lasturl")" ]; then
echo "Wallpaper already downloaded"
exit 1
fi
fi
# Download the wallpaper to a temporary location
wget "${IMGURL}" -O "${DLPATH}/dl" || exit 1
# Overwrite the last wallpaper with the finished download
mv "${DLPATH}/dl" "${DLPATH}/wallpaper" || exit 1
# Save last url to avoid unecessary downloads
echo "${IMGURL}" > "${DLPATH}/lasturl"
# Set wallpaper
set_wallpaper "${DLPATH}/wallpaper"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment