Skip to content

Instantly share code, notes, and snippets.

@lukauskas
Last active November 28, 2018 13:28
Show Gist options
  • Save lukauskas/1589c55b1a75c33480da to your computer and use it in GitHub Desktop.
Save lukauskas/1589c55b1a75c33480da to your computer and use it in GitHub Desktop.
Random Wallpaper for GNOME 3
#!/usr/bin/env python
"""
Sets a random wallpaper on Mac OSX Mavericks.
The wallpaper is fetched from interfacelift's random page for Retina resolution
"""
from urllib.request import Request, urlopen
import re
import os
# We need to pretend we are Chrome, otherwise HTTP 403 Forbidden is thrown
HEADERS={'User-Agent': "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36"}
# Set this to your resolution's random page
URL='http://interfacelift.com/wallpaper/downloads/random/widescreen/5120x2880/'
def fetch_random_wallpaper(filename):
r = Request(URL, headers=HEADERS)
response = urlopen(r).read()
match = re.search("<a\s+href=['\"](/wallpaper/[^'\"]*?\.jpg)['\"]", response.decode('utf-8'))
wallpaper_url = match.group(1)
wallpaper_url = ''.join(['http://www.interfacelift.com', wallpaper_url])
wallpaper_request = Request(wallpaper_url, headers=HEADERS)
wallpaper_response = urlopen(wallpaper_request).read()
with open(filename, 'wb') as f:
f.write(wallpaper_response)
def set_as_wallpaper(filename, change_lockscreen=True):
# From: https://wiki.archlinux.org/index.php/GNOME#Desktop_and_Lock_screen_background
full_path = os.path.abspath(filename)
cmd_template = "gsettings set org.gnome.desktop.{0} picture-uri 'file://{1}'"
cmd = cmd_template.format('background', full_path)
os.system(cmd)
if change_lockscreen:
os.system(cmd_template.format('screensaver', full_path))
def main(filename):
fetch_random_wallpaper(filename)
set_as_wallpaper(filename)
if __name__ == '__main__':
import sys
if len(sys.argv) >= 2:
filename_ = sys.argv[1]
else:
filename_ = os.path.join(os.path.dirname(__file__), 'wallpaper.jpg')
main(filename_)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment