Last active
August 5, 2024 15:50
-
-
Save bitsmanent/6ce604a5f09bd77b59fbbdf9dab818d1 to your computer and use it in GitHub Desktop.
pexels.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |head -1)" | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's a sample
~/.pexelsrc
file: