Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
rotate wallpaper daily from a flickr album with usage recording, so while it is random it is guaranteed each photo will be shown exactly once. connected machines report whether the wallpaper was shown (the machine was turned on on that day) to a twitter account.
#!/bin/bash
#run from crontab and anacrontab:
#@reboot ~/bin/wallpaper-cleanup.sh
#1 1 wallpaper-cleanup su --shell /bin/bash --command "/home/user/bin/wallpaper-cleanup.sh" user
rm /home/user/.kde/cache-user-desktop/plasma-wallpapers/var/cache/user/wallpaper.jpg* 2>/dev/null
rm /home/user/.kde/cache-user-desktop/plasma-wallpapers/var/cache/wallpaper/wallpaper.jpg* 2>/dev/null
#!/bin/bash
#run from crontab and anacrontab:
#@reboot ~/bin/wallpaper.sh
#1 1 wallpaper su --shell /bin/bash --command "/home/wallpaper/bin/wallpaper.sh" wallpaper
path='/var/cache/wallpaper'
func_inarray () {
local e
for e in "${@:2}" ; do if [ "$e" = "$1" ] ; then return 0 ; fi ; done
return 1
}
case "$HOSTNAME" in
'user-main' | 'user2-desktop' )
if [ -f "$path/wallpaper-$(date +%Y%m%d).jpg" ] ; then
ln --force "$path/wallpaper-$(date +%Y%m%d).jpg" $path/wallpaper.jpg
case "$HOSTNAME" in
'user2-desktop' )
ttytter -status="wallpaper $(date +%Y-%m-%d) $HOSTNAME"
;;
esac
fi
;;
esac
find "$path/" -maxdepth 1 -type f -name 'wallpaper-*.jpg' -printf "%f\n" | php -R 'if(preg_match("/([0-9]{4})\-?([0-9]{2})\-?([0-9]{2})\.jpg$/",$argn,$reg)&&"{$reg[1]}{$reg[2]}{$reg[3]}"<=date("Ymd"))echo"$argn\n";' | while read -r file ; do
rm "$path/$file"
done
case "$HOSTNAME" in
'user-main' )
if ping -q -n -c1 -w4 -W4 user-server >/dev/null 2>&1 ; then
arr=()
for i in $(ssh wallpaper@user-server "find \"$path\" -maxdepth 1 -type f -name 'wallpaper-*.jpg' -printf '%f\n'" 2>/dev/null) ; do
arr+=("$i")
done
find "$path" -maxdepth 1 -type f -name 'wallpaper-*.jpg' -printf '%f\n' | while read -r i ; do
if func_inarray "$i" "${arr[@]}" ; then true
else
scp -B -q "$path/$i" "wallpaper@user-server:$path/"
fi
done
fi
;;
'user-server' )
if ping -q -n -c1 -w4 -W4 user2-desktop >/dev/null 2>&1 ; then
arr=()
for i in $(ssh -o StrictHostKeyChecking=no wallpaper@user2-desktop "find \"$path\" -maxdepth 1 -type f -name 'wallpaper-*.jpg' -printf '%f\n'" 2>/dev/null) ; do
arr+=("$i")
done
find "$path" -maxdepth 1 -type f -name 'wallpaper-*.jpg' -printf '%f\n' | while read -r i ; do
if func_inarray "$i" "${arr[@]}" ; then true
else
if ping -q -n -c1 -w4 -W4 user2-desktop >/dev/null 2>&1 ; then
scp -o StrictHostKeyChecking=no -B -q "$path/$i" "wallpaper@user2-desktop:$path/" 2>/dev/null
fi
fi
done
fi
;;
esac
#!/bin/bash
#run from anacrontab:
#1 1 wallpapers su --shell /bin/bash --command "/home/wallpaper/bin/wallpapers.sh" wallpaper
path='/var/cache/wallpaper'
sqlite='/var/local/wallpaper/wallpaper.sqlite' # CREATE TABLE wallpaper (flickrid INTEGER NOT NULL UNIQUE, date DATE DEFAULT NULL UNIQUE, used INTEGER NOT NULL DEFAULT 0);
ttytter -runcommand='/again twitterhandle' | grep -E '<twitterhandle> wallpaper [0-9]{4}\-[0-9]{2}\-[0-9]{2} ' | grep -Eo '[0-9]{4}\-[0-9]{2}\-[0-9]{2}' | while read -r date ; do
sqlite3 -bail -batch "$sqlite" "UPDATE wallpaper SET used=1 WHERE date='$date'"
done
flickcurl photosets.getPhotos 123456789 | grep -E '^photo with URI .+ ID [0-9]+ and [0-9]+ tags$' | grep -Eo '[0-9]{11,}' | sort --random-sort | while read -r flickrid ; do
if [ "$flickrid" -eq "$flickrid" ] 2>/dev/null ; then
sqlite3 -bail -batch "$sqlite" "INSERT OR IGNORE INTO wallpaper (flickrid) VALUES ($flickrid)"
fi
done
start=$(date --date="+1 day" +%Y-%m-%d)
end=$(date --date="+21 day" +%Y-%m-%d)
php -r 'for($i=strtotime("'$start'");$i<=strtotime("'$end'");$i=mktime(0,0,0,idate("m",$i),idate("d",$i)+1,idate("Y",$i)))echo date("Y-m-d\n",$i);' | while read -r date ; do
if [ $(sqlite3 -bail -batch "$sqlite" "SELECT COUNT(*) FROM wallpaper WHERE date IS NULL") -eq 0 ] 2>/dev/null ; then
sqlite3 -bail -batch "$sqlite" "UPDATE wallpaper SET date=NULL WHERE used!=1 AND date<=DATE()"
if [ $(sqlite3 -bail -batch "$sqlite" "SELECT COUNT(*) FROM wallpaper WHERE date IS NULL") -eq 0 ] ; then
sqlite3 -bail -batch "$sqlite" "UPDATE wallpaper SET date=NULL, used=0 WHERE date<=DATE()"
fi
fi
sqlite3 -bail -batch "$sqlite" "UPDATE OR IGNORE wallpaper SET date='$date' WHERE date IS NULL ORDER BY RANDOM() LIMIT 1"
done
if [ ! -f "$path/wallpaper.jpg" ] ; then
date=$(date +%Y-%m-%d)
datepure=$(echo $date | tr -d '-')
flickrid=$(sqlite3 -bail -batch "$sqlite" "SELECT flickrid FROM wallpaper WHERE date='$date'")
if [ "$flickrid" -eq "$flickrid" ] 2>/dev/null ; then
url=$(flickcurl photos.getSizes $flickrid | grep -Ei '^ source http.+[ko]\.jpg$' | grep -Eoi 'http.+\.jpg$' | head -1)
if [[ "$url" =~ ^http.+\.[jJ][pP][gG]$ ]] ; then
if wget --quiet --output-document="$path/wallpaper-tmp.jpg" "$url" ; then
mv "$path/wallpaper-tmp.jpg" "$path/wallpaper.jpg"
fi
fi
fi
fi
php -r 'for($i=strtotime("'$start'");$i<=strtotime("'$end'");$i=mktime(0,0,0,idate("m",$i),idate("d",$i)+1,idate("Y",$i)))echo date("Y-m-d\n",$i);' | while read -r date ; do
datepure=$(echo $date | tr -d '-')
if [ ! -f "$path/wallpaper-$datepure.jpg" ] ; then
flickrid=$(sqlite3 -bail -batch "$sqlite" "SELECT flickrid FROM wallpaper WHERE date='$date'")
if [ "$flickrid" -eq "$flickrid" ] 2>/dev/null ; then
url=$(flickcurl photos.getSizes $flickrid | grep -Ei '^ source http.+[ko]\.jpg$' | grep -Eoi 'http.+\.jpg$' | head -1)
if [[ "$url" =~ ^http.+\.[jJ][pP][gG]$ ]] ; then
if wget --quiet --output-document="$path/wallpaper-tmp.jpg" "$url" ; then
mv "$path/wallpaper-tmp.jpg" "$path/wallpaper-$datepure.jpg"
fi
fi
fi
fi
done
Owner
phpmoli commented Apr 28, 2016

my setup:
user-main is a desktop downloading the photos from flickr, fetching usage from twitter, maintaining the usage-database, uploading wallpapers to my user-server and showing the wallpaper
user-server is a headless server storing the wallpapers for third-party desktops on remote networks
user2-desktop is a remote desktop showing the wallpaper and reporting to twitter (user-main is not reporting to twitter, but it is very easy to implement)

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