Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bitsmanent/6ce604a5f09bd77b59fbbdf9dab818d1 to your computer and use it in GitHub Desktop.
Save bitsmanent/6ce604a5f09bd77b59fbbdf9dab818d1 to your computer and use it in GitHub Desktop.
pexels.sh
#!/bin/sh
# Gist: https://gist.github.com/bitsmanent/6ce604a5f09bd77b59fbbdf9dab818d1
# https://www.pexels.com/it-it/api/documentation
# include resource file
. ~/.pexelsrc
# kill another instance if running
mypid=$$
argv0="$(basename "$0")"
pid="$(pgrep -o "$argv0" |grep -v "$mypid")"
if [ -n "$pid" ]; then
kill "$pid"
echo "WARNING: killed another instance with pid $pid"
fi
echo "Running at $(date)..."
last_index=0
while true;
do
nitems=$NITEMS
if [ $nitems -le 0 ]; then
# find collection and get total items
# Note: assume the user has at most 80 collections...
uri="https://api.pexels.com/v1/collections?per_page=80"
json="$(curl -sH "Authorization: $APIKEY" "$uri")"
nitems="$(echo "$json" |sed "s/.*\"$CID\".*\"media_count\":\([0-9]*\).*/\1/")"
fi
# prevent using the same index twice
index="$last_index"
while [ "$index" = "$last_index" ]; do
index="$(shuf -zn 1 -i 1-$nitems)"
done
last_index="$index"
# get item url
qs="per_page=1&page=$index"
uri="https://api.pexels.com/v1/collections/$CID?$qs"
json="$(curl -sH "Authorization: $APIKEY " "$uri")"
pic="$(echo "$json" |sed -n 's/.*"original":"\([^"]*\).*/\1/p')"
# get screen resolution
WxH="$(xrandr |grep '*' |cut -d ' ' -f4)"
w="$(echo "$WxH" |cut -dx -f1)"
h="$(echo "$WxH" |cut -dx -f2)"
# set item as wallpaper
picuri="$pic?auto=compress&cs=tinysrgb&fit=crop&w=$w&h=$h"
feh --bg-max --no-fehbg "$picuri"
echo "cid=$CID index=$index $pic" >> "$LOGFILE"
sleep "$DELAY"
done
@bitsmanent
Copy link
Author

Here's a sample ~/.pexelsrc file:

APIKEY="api-key"
CID="collection-id"

# Responses are cached for 24 hours thus adding new images will increment total
# items for a lot of time. Use this variable to bypass the counter. When is set
# to 0 an HTTP request is made to get the number of photos into the collection.
NITEMS=0

DELAY=300
LOGFILE="/tmp/wallpapers.log"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment