Skip to content

Instantly share code, notes, and snippets.

@esecules
Last active September 16, 2016 21:08
Show Gist options
  • Save esecules/a422849352261c860e5cf1426e65b5bc to your computer and use it in GitHub Desktop.
Save esecules/a422849352261c860e5cf1426e65b5bc to your computer and use it in GitHub Desktop.
imgur sourced wallpaper switcher: full solution
####################################
Scripts Needed
####################################
eric@eric-UX305CA:~$ cat /usr/local/bin/wallpaperSwitcher
#! /bin/bash
PID=$(pgrep gnome-session)
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)
gsettings set org.gnome.desktop.background picture-uri file://"$(wprand)"
eric@eric-UX305CA:~$ cat /usr/local/bin/wpdownload
#! /usr/bin/python
import json
import requests
import os
import random
dest='/home/eric/Pictures/wallpaper/'
subreddit='wallpaper'
baseurl = 'http://imgur.com'
r = requests.get(baseurl + '/r/%s/top.json' % subreddit)
j = json.loads(r.text)
data = [ img for img in j['data'] if img['nsfw'] == False and img['width'] >= 1920 and img['height'] >= 1080]
best = ["%s/%s%s" % (baseurl, img['hash'], img['ext']) for img in data[:7]]
rest = data[7:]
random.shuffle(rest)
randoms = ["%s/%s%s" % (baseurl, img['hash'], img['ext']) for img in rest[:7]]
urls = best + randoms
if len(urls) == 0:
exit(1)
for the_file in os.listdir(dest):
file_path = os.path.join(dest, the_file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
except Exception as e:
print e
for url in urls:
r=requests.get(url)
with open(dest + url.split('/')[-1], 'wb') as f:
f.write(r.content)
eric@eric-UX305CA:~$ cat /usr/local/bin/wprand
#! /bin/bash
wallpapers=($(find /home/eric/Pictures/wallpaper -type f))
nextWallpaper=${wallpapers[$RANDOM % ${#wallpapers[@]} ]}
echo $nextWallpaper
#################################
Cron Jobs
#################################
eric@eric-UX305CA:~$ crontab -l
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
*/15 * * * * . wallpaperSwitcher
eric@eric-UX305CA:~$ cat /etc/cron.daily/downloadWallpaper
#! /bin/bash
wpdownload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment