Skip to content

Instantly share code, notes, and snippets.

@Em-AK
Last active June 8, 2018 17:29
Show Gist options
  • Save Em-AK/e54653c3ca390d7eaf6bc89dc46cc1c2 to your computer and use it in GitHub Desktop.
Save Em-AK/e54653c3ca390d7eaf6bc89dc46cc1c2 to your computer and use it in GitHub Desktop.
#!/bin/bash
### README
#############################################
# A basic wallpaper rotator, fetching pictures from https://unsplash.com
#
## Install
#
# Dependencies:
# * [feh image viewer](https://feh.finalrewind.org/)
# * [wget](https://www.gnu.org/software/wget/)
# Both should be available in the packages of your OS.
#
# Save this file to a directory listed in your PATH, as: `~/bin`
#
# wget https://gist.githubusercontent.com/Em-AK/e54653c3ca390d7eaf6bc89dc46cc1c2/raw/wallpaper > ~/bin/wallpaper
#
# Make the file executable
#
# chmod +x ~/bin/wallpaper
#
# Edit the value of STORAGE_DIR in the script below to specify where you want
# to store your saved wallpapers. Or create the default directory:
#
# mkdir -p ~/images/unsplash
#
## Usage
#
# wallpaper [-rsl]
#
# options:
# -r : set a random wallpaper from unsplash.com. The file is saved in a
# temporary location (see TMP_PICTURE_PATH). This file is erased every
# time a new remote picture is fetched.
# This option fallsback to the behaviour of option -l if unsplash.com is
# not reachable in PING_TIMEOUT (default 2) seconds.
# -s : save the current wallpaper in the local storage.
# -l : set a random wallpaper from the local storage.
#
# `wallpaper` (no option) is equivalent to `wallpaper -r`
#
## Extra
#
# You can add a crontab job to change your wallpaper periodically with `crontab -e`
# For example this job will rotate your wallpaper every hour at 42min
#
# 42 * * * * /home/emak/bin/wallpaper
### Constants
#############################################
STORAGE_DIR="$HOME/images/unsplash"
TMP_PICTURE_PATH="$HOME/.wallpaper.jpg"
UNSPLASH_DOMAIN="unsplash.com"
PING_TIMEOUT=2
NEW_PICTURE_URL="https://source.unsplash.com/random/1920x1080"
### Functions
#############################################
function download_random_remote {
wget $NEW_PICTURE_URL -O $1
}
function set_wallpaper {
DISPLAY=:0 feh --bg-fill $1
}
function set_random_local_wallpaper {
DISPLAY=:0
shuf -ezn 1 $STORAGE_DIR/* | xargs -0 -n1 feh --bg-fill
}
function try_remote {
ping $UNSPLASH_DOMAIN -c 1 -w $PING_TIMEOUT &> /dev/null
if [ $? -ne 0 ]; then
set_random_local_wallpaper
else
download_random_remote $TMP_PICTURE_PATH
set_wallpaper $TMP_PICTURE_PATH
fi
}
function save_temp_to_store {
filename="photo-$(sha1sum $TMP_PICTURE_PATH | cut -d ' ' -f 1).jpg"
cp $TMP_PICTURE_PATH $STORAGE_DIR/$filename
}
### Main
#############################################
if (($# == 0)); then
# when no argument is given
try_remote
else
while getopts ":lrs" option
do
case "$option" in
# -l local
# set a random wallpaper from local
l)
set_random_local_wallpaper
;;
# -r remote
# set a random wallpaper from remote
r)
try_remote
;;
# -s save/store
# save temporary wallpaper to store
s)
save_temp_to_store
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment