网站运营
位置:首页>> 网站运营>> ubuntu 16.04LTS 开机启动自动更换壁纸的实现方法

ubuntu 16.04LTS 开机启动自动更换壁纸的实现方法

作者:jingxian  发布时间:2023-08-06 05:53:15 

标签:ubuntu,16.04,LTS,开机启动,更换,壁纸

前言

上周电脑重装,换了ubuntu 16.04,想起来之前上课老师也是ubuntu而且他还提到他桌面是他自己写的个小脚本实现的自动更换桌面壁纸的,昨天晚上心血来潮自己网上搜了点资料实现了一下 = =

功能

功能的话,是从必应壁纸爬取最新的那张壁纸,然后本来因为我怕电脑又放了太多东西,就设置了个图片最大的数目,到达这个阈值就删除所有的图片= =|| 通过python的os模块,设置壁纸,

这里可以参考:how-to-change-desktop-background-from-command-line-in-unity , 还有就是考虑到刚开机时我不一定连得上网(辛酸 T_T),所以设置了一个爬取的间隔

代码


# -*- coding: utf-8 -*-
#!/usr/bin/python
#!/bin/bash

# ---------------------------------------------------------- #
# This is a script which can change background automatically #
# every time the system starts.        #
# author: Huang Zhenyang          #
# email: 745125931@qq.com         #
# ---------------------------------------------------------- #
# ----- Import ----- #
import os
import re
import urllib
import time
import socket
# --- End Import --- #

class Spider(object):
"""
This is the spider to get the img from being
"""
def __init__( self, img_matched_pattern_para, url_para, file_name_para ):
 """
 init function
 :param img_matched_pattern_para: the pattern to match a img
 :param url_para: url to crawler
 :param file_name_para: file name
 """
 self.img_matched_pattern = img_matched_pattern_para
 self.url = url_para
 self.file_name = file_name_para
def get_img(self):
 """
 download the image
 :return:
 """
 html = self.get_html()
 img_pos = re.search(self.img_matched_pattern, html)
 img_page_href = self.url + img_pos.group()[6:-7] + "download"
 urllib.urlretrieve(img_page_href, self.file_name)

def get_html(self):
 """
 return the html
 :return: page's html <type 'str'>
 """
 page = urllib.urlopen(self.url)
 html = page.read()
 return html

class Controller(object):
"""
This is the controller to control the spider's parameters.
"""
def __init__(self, pattern_href_para, path_para, url_para, img_max_num_para):
 """
 :param pattern_href_para: href's match pattern
 :param path_para: path to save images
 :param url_para: url
 :param img_max_num_para: max number that the
 """
 self.pattern_href = pattern_href_para
 self.path = path_para
 self.url = url_para
 self.img_max_num = img_max_num_para
 self.file_name = ""
 self.init_file_name = "0.jpg"
 self.file_extension_name = ".jpg"
def judge(self):
 """
 judge if the number of images is grater than img_max_num.
 if true, delete all of them and then run spider, else directly run spider.
 Also, we should set the file name.
 :return:
 """
 root = None
 _dirs = None
 files = None
 for root, _dirs, files in os.walk(self.path, True):
  pass
 files_len = len(files)
 if files_len == 10:
  for i in range(0, 10):
   os.remove(root + files[i])
  self.file_name = self.path + self.init_file_name
 else:
  self.file_name = self.path + str(files_len) + self.file_extension_name
def run_spider(self):
 """
 run spider.
 TODO: This function needs to be modified in the future which makes these two class coupling too much.
 :return:
 """
 # In case user's computer hasn't connect the internet.
 for i in range(0, 60):
  try:
   spider = Spider(self.pattern_href, self.url, self.file_name)
   spider.get_img()
   break
  except IOError as e:
   print "Connection error: %s" % e
   time.sleep(60)
   continue
  except Exception as e:
   print "Connection error: %s" % e
   time.sleep(60)
   continue
 c_path = '"file://' + self.file_name + '"' # absolute path
 # call system command to change the gnome background
 os.system('gsettings set org.gnome.desktop.background picture-uri ' + c_path)
 print "gsettings set org.gnome.desktop.background picture-uri " + c_path

if __name__ == '__main__':
pattern_href = r'href="/photo/.*?" rel="external nofollow" '
path = '/home/hzy/图片/backgrounds/'
url = 'https://bing.ioliu.cn/'
img_max_num = 10
controller = Controller(pattern_href, path, url, img_max_num)
controller.judge()
controller.run_spider()

开机自启动

在/home/hzy/.config/autostart中,新建一个xxx.desktop的文件,内容如下:


[Desktop Entry]
Name=autoChangeBackgroundImg
Comment=Python Program
Exec=python /home/hzy/Script/autoChangeBackgroundImg/autoChangeBackgroundImg.py
Icon=/home/hzy/Script/autoChangeBackgroundImg/autoChangeBackgroundImg.png
Terminal=false
MultipleArgs=false
Type=Application
Categories=Application;Development;
StartupNotify=true

说明:

1. Exec 后面的路径就是该脚本的路径

2. Icon的话可以自己随便找个图片,设置成该路径即可

来源:http://blog.csdn.net/qq_35056292/article/details/79225376

0
投稿

猜你喜欢

  • 7.网站经常无法访问也许你拥有最好的网页内容,最好的网络服务,但是如果你的网站因为各种原因经常无法访问,或者访问速度很慢,那么,你拥有的用户
  • 升级IE8浏览器后,无法上传图片,总是提示“无效图片”,重置IE浏览器无效,使用IE7浏览器时一起正常。那么这个情况应该怎么办呢?请用户打开
  • Deluxe Plan主机怎样升级Zend呢?因为杰奇系统需要更高版本的ZEND,所以求教大家具体的安装步骤。 设置php4+Ze
  • Fedora Core 3 在安装时默认把SELinux的选项激活了。SELinux比普通的Linux内核提供了更高的安全性,理论上说,在系
  • 最新消息,康盛创想(Comsenz)旗下核心产品UCenter Home(简称UCHome)当前应用站点已经超过15万家。UCenter H
  • 在搜索引擎优化过程,我觉得最难就是解决搜索引擎收录网页的数量,再就是轻松的获取站外链接。突然看到有一个系统做泛外链,用404作优化。猛如醍醐
  • 一、 前言随着各大网站纷纷推出邮箱收费服务,广大网民正在逐渐适应 "有偿服务"的概念,与此同时,他们也对收费邮箱的服务质
  • 面对国内严峻的互联网市场,面对国内混乱的域名市场,有些站长想把域名转移到了国外,国外的域名注册商比较多,像godaddy就是全球最大的域名注
  • 做seo优化,友情链接的整站链接和只是首页链接是有很大区别的。会分散权重。很多常见wordpress主题模板,都是设置全站友情链接的,所以我
  • 今天喝了点酒,谈下建站2年的经验,实在是不爱打字,尽量简洁点,希望能帮助下新人朋友。我和网络接触的真正时候,大约3年前。其实也不算接触网络,
  • 加固外网上的IIS服务器安全关于IIS服务器的安全主要包括六步:1、使用安全配置向导(Security Configuration Wiza
  • 算算从12月17日晚上正式把小站放到服务器上开始已经一个月了,我就把我这一个月的体会与大家分享一下。我2000年就开始接触网络,2003年做
  • 找到data/bbscache/config.php把$db_ifsafecv='1'; 改成$db_ifsafe
  • 安装好Serv-U初次运行时,会自动弹出创建域和账户操作向导窗口。由于使用向导创建的账户会带来一些未知的安全问题,因此在这里建议单击“取消”
  • 得百度者得天下。这个道理很简单,百度收录越多,被用户搜索到你的网站的机会也就越多,流量也就来的越大。在中国,八成网民会选择百度来搜索所需要的
  • 近日,搜狗率先将输入法发挥到极致,在全球范围首个推出云输入法。搜狗云输入法是基于搜狗拼音输入法,依托云计算技术的一个&ldquo;概
  • 这里常远分享下用织梦CMS建站前,为更利于SEO,对织梦CMS的修改工作。现在站长都知道SEO对于一个网站重要性,我们规划一个网站,选好网站
  • 在我们建站起初对于站点无论我们做的是什么样的站点,对自己的站点定位一定要很清晰,现在像很多朋友站点建立做的比较杂,甚至很多自己自己都不知道自
  • 假如你的托管帐户托管了一个二级域名,你可以通过FTP上载文档到那里,这是从你的本地机传输你的网站到你的托管帐户的一种方法。在你的主域名目录里
  • 职业的炒信用公司见过很多,我还真是第一次听说&ldquo;中差评删除公司&rdquo;。这两天有机会与一位自称是&l
手机版 网站运营 asp之家 www.aspxhome.com