每天定时(cron)抓取bing的主页图片作为linux桌面背景,以下是python脚本
import urllib
import re
img_p = re.compile(r'g_img={url:\'(.*?)\'', re.I | re.M | re.S)
#download bing homepage
content = urllib.urlopen('http://cn.bing.com/').read()
#find backgroud image url
m = re.search(img_p, content)
if m is not None:
img_url = m.group(1)
name = img_url.split('/')[-1]
#replace special character
name = name.replace('=', '_').replace('?', '_').replace('%', '_')
name = '/path/to/%s' % name
#save backgroud image
urllib.urlretrieve(img_url, name)
#refresh desktop backgroud
import os
os.system('gsettings set org.gnome.desktop.background picture-uri "file://%s"' % name)
crontab: 30 8 * * * env DISPLAY=:0 python refresh_gb.py 注意,如果不加env DISPLAY=:0是没法更新桌面背景的,因为cron默认是后台操作,无法修改UI