konachan-wallpaper/updateWallpaper.sh

87 lines
1.8 KiB
Bash
Executable File

#!/bin/bash
tags="scenic order:random width:1600"
picdir=`realpath $(dirname "${BASH_SOURCE[0]}")`
del="30m"
sud=0
lockfile=/tmp/updateWallpaper.lock
kill_running=0
for i in "$@"; do
case $i in
-s=*|--start-up-delay=*)
sud="${i#*=}"
shift
;;
-d=*|--delay=*)
del="${i#*=}"
shift
;;
-t=*|--tags=*)
tags="${i#*=}"
shift
;;
-k|--kill*)
kill_running=1
;;
-h|--help*)
echo "usage: $0 <options>"
echo "Options:"
echo " -s --start-up-delay=del Set time between start and first image request."
echo " -d --delay=del Set delay between image requests."
echo " -t --tags=tags The actual search string. Special fields allowed."
echo " -k --kill Kill a previously running instance of the script."
echo "Notes:"
echo " All times need to be formatted for the sleep command."
exit 1
;;
*)
# Unknown
;;
esac
done
if [ -e "$lockfile" ]; then
kill -0 $(cat $lockfile)
if [ $? -eq 0 ]; then
if [ $kill_running -eq 0 ]; then
echo "Error: Lock file $lockfile exists!"
echo " This most likely means another instance of the script is already running."
echo " Use -k to kill the running process."
exit 2;
else
echo "Warning: Killing old process..."
kill $(cat $lockfile)
fi
fi
fi
echo $$ > "$lockfile"
if [ "$sud" != "0" ]; then
sleep "$sud";
fi
tags=`echo -n "$tags" | sed "s/ /%20/g"`
while [ 0 ]; do
json=`curl "http://konachan.com/post.json?tags=$tags&limit=1"`
id=`echo $json | jq -r '.[0].id'`
url=`echo $json | jq -r '.[0].file_url'`
ext=`echo $url | sed "s/.*\.//g"`
url="$url"
local_url="$picdir/$id.$ext"
echo "Updating wallpaper to $id..."
if [ -f "$local_url" ]; then
echo "Already downloaded."
else
echo "Downloading $url..."
curl -o "$local_url" "$url"
fi
gsettings set org.gnome.desktop.background picture-uri "$local_url"
sleep "$del"
done