Created
July 31, 2012 20:31
-
-
Save dandelionmood/3220230 to your computer and use it in GitHub Desktop.
Quick And Dirty Python Background Changer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python -O | |
# | |
# Quick And Dirty Python Background Changer | |
# | |
# Designed for Ubuntu Precise 12.04 | |
# | |
# Please install beautifulsoup python module first : | |
# sudo aptitude install python-beautifulsoup | |
# | |
# Then run the command adding a keyword, eg. : | |
# ./qadpbc.py robot | |
# ./qadpbc.py bird | |
# | |
# That's it, your background has changed ! | |
# | |
# Author Pierre Quillery http://quillery.fr | |
# Licence Apache/BSD (= do whatever you want with it) | |
# | |
# Getting the keyword from CLI | |
import sys | |
key_word = sys.argv[1] if len(sys.argv) > 1 else 'blue' | |
# Getting a feed from Colorlovers XML API | |
import urllib2 | |
import urllib | |
url = 'http://www.colourlovers.com/api/patterns/top' | |
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' | |
values = { 'keywords': key_word } | |
headers = { 'User-Agent' : user_agent } | |
data = urllib.urlencode(values) | |
req = urllib2.Request(url, data, headers) | |
response = urllib2.urlopen(req) | |
the_page = response.read() | |
# Parsing it using BeautifulSoup | |
from BeautifulSoup import BeautifulStoneSoup | |
from random import choice | |
soup = BeautifulStoneSoup(the_page) | |
soup.prettify() | |
# Getting all the background URLs | |
urls = [] | |
for url in soup.findAll('imageurl'): | |
urls.append( url.text ) | |
# We only need one of them, so we choose one randomly | |
url = choice(urls) | |
# Now, let's download one of them in the user's home directory | |
import os | |
home = os.getenv('HOME') | |
file_path = home + "/qadpbc_background.png" | |
urllib.urlretrieve (url, file_path) | |
# We can set it easily using these two commands | |
os.system("gsettings set org.gnome.desktop.background picture-uri file://"+file_path) | |
os.system("gsettings set org.gnome.desktop.background picture-options wallpaper") | |
# And that's all folks ! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment