Ubuntu使用Python脚本实现每日自动同步Bing壁纸


1
#!/usr/bin/python3 2 #-*-coding=utf-8-*- 3 4 import os # 利用os.system(command)实现设置壁纸 5 import re # 用来匹配必应首页图片地址 6 import time 7 import sys # for stopping this script in csae of any mistake 8 from urllib.request import urlopen # for fetching web pages 9 10 patten = r'g_img={url:\'(?P<h>.*)\',id' # find where the picture is by its html patten 11 path = '/home/legendlc/Desktop/Python/WallPapers/' # folder for storing pics 12 url = "https://cn.bing.com" # target url 13 refreshInterval = 10 * 60 # 单位为秒 14 15 _filename = 'none' 16 filename = 'none' 17 while True: 18 now = time.localtime() 19 _filename = filename 20 filename = str(now.tm_year) + '_' + str(now.tm_mon) + '_' + str(now.tm_mday) + '.jpg' 21 22 if _filename != filename: 23 try: 24 response = urlopen(url) 25 except Exception as e: 26 print(e) 27 html = response.read().decode('utf-8') # get html in form of string 28 29 pos = re.search(patten, html) 30 if pos == None: 31 print('Html Patten out-of-date') 32 sys.exit(0) 33 img_url = 'http://s.cn.bing.net' + pos.group('h') # get image url 34 35 x = urlopen(img_url) 36 f = open(path + filename, 'wb') 37 f.write(x.read()) 38 f.close() # download the image into a local file named by date 39 40 c_path = '"file://' + path + filename + '"' # absolute path 41 os.system('gsettings set org.gnome.desktop.background picture-uri ' + c_path) # call system command to change the gnome background 42 time.sleep(refreshInterval) 43 else: 44 time.sleep(refreshInterval)
posted @ 2016-03-12 23:11  legendlc  阅读(651)  评论(0编辑  收藏  举报