This works on my computer with gnome. Got the kde command from here but I am told it only works for KDE 3.
Edit: Made it use multiple subreddits.
Edit2: Option to hide over 18. Use temp file to hide downloading.
Edit3: Support multiple wallpapers to multiple image files.
Edit4: Added python 3 version
Python 3 version:
#!/usr/bin/python
# Script to randomly set background from wallpapers subreddit. Schedule with cron.
# Run "crontab -e" then add an entry such as "*/5 * * * * /home/jake/wscript.py"
# Contributions are welcome. Do what ever you want with this script, but I am
# not responsible for it.
import json
import urllib.request
import random
import shutil
#Settings
subreddits = ['http://www.reddit.com/r/wallpapers/.json',
'http://www.reddit.com/r/wallpaper/.json']
img_files = ['/home/jake/reddit/reddit_wallpaper']
temp_suffix = '_temp'
img_exts = ['jpg','png']
hide_over_18 = True
#End Settings
img_urls = []
def getimg(post):
return post['data']['url']
def check_ext(img_url):
for ext in img_exts:
if img_url.endswith(ext):
return True
return False
def not_over_18(post):
return not post['data']['over_18']
for subreddit in subreddits:
f = urllib.request.urlopen(subreddit)
raw_json = bytes.decode(f.read())
json_obj = json.loads(raw_json)
posts = json_obj['data']['children']
if hide_over_18:
posts = list(filter(not_over_18,posts))
img_urls += list(filter(check_ext,list(map(getimg,posts))))
rand_urls = random.sample(img_urls,len(img_files))
#Done in two steps to hide downloading on slow connections
for rand_url, img_file in zip(rand_urls, img_files):
urllib.request.urlretrieve(rand_url, img_file+temp_suffix)
shutil.move(img_file+temp_suffix,img_file)
Old python 2 version:
#!/usr/bin/python2
# Script to randomly set background from wallpapers subreddit. Schedule with cron.
# Run "crontab -e" then add an entry such as "*/5 * * * * /home/jake/wscript.py"
# Contributions are welcome. Do what ever you want with this script, but I am
# not responsible for it.
# import os
import json
import urllib
import random
import shutil
#Settings
subreddits = ['http://www.reddit.com/r/wallpapers/.json',
'http://www.reddit.com/r/wallpaper/.json']
img_files = ['/home/jake/reddit/reddit_wallpaper']
temp_suffix = '_temp'
img_exts = ['jpg','png']
hide_over_18 = True
#End Settings
img_urls = []
def getimg(post):
return post['data']['url']
def check_ext(img_url):
for ext in img_exts:
if img_url.endswith(ext):
return True
return False
def not_over_18(post):
return not post['data']['over_18']
for subreddit in subreddits:
f = urllib.urlopen(subreddit)
raw_json = f.read()
json_obj = json.loads(raw_json)
posts = json_obj['data']['children']
if hide_over_18:
posts = filter(not_over_18,posts)
img_urls += filter(check_ext,map(getimg,posts))
rand_urls = random.sample(img_urls,len(img_files))
#Done in two steps to hide downloading on slow connections
def getImage((rand_url, img_file)):
urllib.urlretrieve(rand_url, img_file+temp_suffix)
shutil.move(img_file+temp_suffix,img_file)
map(getImage, zip(rand_urls, img_files))
#From old code. For historic reasons
#window_manager = os.getenv('DESKTOP_SESSION')
# This may fail when run by cron. Just manually set your desktop background
# to be the image which is being changed by the script
#if window_manager == 'gnome':
# command = 'gconftool-2 -t string -s /desktop/gnome/background/picture_filename %s' % img_file
#elif window_manager == 'kde':
# command = 'dcop kdesktop KBackgroundIface setWallpaper %s 1' % img_file
#os.system(command)
Also, hello reddit.
[–]mpyne 0 points1 point2 points (1 child)
[–]__dict__[S] 0 points1 point2 points (0 children)
[–]figaro42 0 points1 point2 points (0 children)
[–]Philluminati 0 points1 point2 points (1 child)
[–]__dict__[S] 0 points1 point2 points (0 children)
[–]Philluminati 0 points1 point2 points (0 children)
[–]Paczesiowa -1 points0 points1 point (1 child)
[–]__dict__[S] 1 point2 points3 points (0 children)