Skip to content

Instantly share code, notes, and snippets.

@flytwokites
Last active December 10, 2015 21:08
Show Gist options
  • Save flytwokites/4492607 to your computer and use it in GitHub Desktop.
Save flytwokites/4492607 to your computer and use it in GitHub Desktop.
Change Gnome wallpaper automatically.
#!/usr/bin/env python
import urllib
import bs4
import random
import re
import os
import sys
import base64
import json
from gi.repository import Gio
wallpaper_path = os.path.expanduser('~/.autowallpaper')
config_path = os.path.expanduser('~/.autowallpaper-used.json')
index_url = 'http://wallbase.cc/toplist/%d/12/eqeq/0x0/0/010/60/3d' % (random.randint(0, 100)*60)
index_html = urllib.urlopen(index_url).read()
index_dom = bs4.BeautifulSoup(index_html)
detail_urls = [link['href'] for link in index_dom.select('.thumb a.thlink')]
print 'find:', len(detail_urls)
try:
with open(config_path, 'r') as f:
used = json.load(f)
except Exception:
used = []
print 'used:', len(used)
detail_urls = [url for url in detail_urls if url not in used]
if not detail_urls:
print 'no new wallpapers'
sys.exit(1)
detail_url = detail_urls[0]
detail_html = urllib.urlopen(detail_url).read()
image_url_b64 = re.search(r"B\('([a-zA-Z0-9+/=]+)'\)", detail_html).group(1)
image_url = base64.b64decode(image_url_b64)
image_data = urllib.urlopen(image_url).read()
with open(wallpaper_path, 'w') as f:
f.write(image_data)
used.append(detail_url)
with open(config_path, 'w') as f:
json.dump(used, f)
bg_settings = Gio.Settings.new('org.gnome.desktop.background')
bg_settings.set_string('picture-uri', 'file://%s' % wallpaper_path)
@flytwokites
Copy link
Author

运行前先安装beautifulsoup4:

pip install beautifulsoup4

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