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

前言

上周电脑重装,换了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的话可以自己随便找个图片,设置成该路径即可

以上这篇ubuntu 16.04LTS 开机启动自动更换壁纸的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • Ubuntu 16.04 LTS下安装MATLAB 2014B的方法教程

    环境配置说明: 环境:Ubuntu 16.04LTS 软件:MATLAB 2014B MATLAB 2014B 下载地址(带Crack)如下: 百度云下载:   链接: https://pan.baidu.com/s/1kVJQVlX  密码: pj9m 本 地 下 载:    Ubuntu 16.04 LTS 官方正式版 32位                          Ubuntu 16.04 LTS 官方正式版 64位 步骤如下:  1.直接解压 matlab_R2014b.iso

  • ubuntu14.04LTS安装nginx+mariaDB+php7+YAF的方法

    本文讲述了ubuntu14.04LTS安装nginx+mariaDB+php7+YAF的方法.分享给大家供大家参考,具体如下: ubuntu apt-get方式安装nginx 参考: http://nginx.org/en/linux_packages.html 首先 in order to authenticate the nginx repository signature and to eliminate warnings about missing PGP key during inst

  • Ubuntu 16.04 LTS系统里中文txt文件打开的问题解决

    前言 大家都知道,Ubuntu系统对中文的支持还是比较友好,除了对TEXT文件编码的识别有点问题以外. gedit直接打开中文txt文件 出现这种问题的原因是txt文件是在Windows环境下生成,Ubuntu下文本阅读器如vim和gedit对文件编码格式不能自动识别. 这里给出几种可能的解决方案. 文件格式转换 利用 dos2unix unix2dos unix2mac 等命令将txt文件从源操作系统转换到目标操作系统. 如果这几个命令不存在,请使用下面的命令进行安装. sudo apt in

  • Ubuntu Server 16.04 LTS 上安装 LAMP图解教程

    LAMP 方案是一系列自由和开源软件的集合,包含了 Linux.Web 服务器 (Apache). 数据库服务器 (MySQL / MariaDB) 和 PHP (脚本语言).LAMP 是那些需要安装和构建动态网页应用的基础平台,比如WordPress.Joomla.OpenCart 和 Drupal. 在这篇文章中,我将描述如何在 Ubuntu Server 16.04 LTS 上安装 LAMP,众所周知 Ubuntu 是一个基于 Linux 的操作系统,因此它构成了 LAMP 的第一个部分,

  • Ubuntu 16.04 LTS中源码安装Python 3.6.0的方法教程

    前提 官网上提供了 Mac 和 Windows 上的安装包和 Linux 上安装需要的源码. 下载地址如下: https://www.python.org/downloads/release/python-360/ 安装 wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tar.xz xz -d Python-3.6.0.tar.xz tar -xvf Python-3.6.0.tar cd Python-3.6.0 ./conf

  • Ubuntu16.04 LTS 下安装 Android Studio 2.2.2 的详细步骤

    下载 Android SDK,官网:www.android.com我相信很多人跟我一样,进不去.Android Studio 中文官网 www.android-studio.org 进去下载, 在终端解压到想安装SDK的目录(设SDK目录为 $ASDK_HOME )即安装完成. 进入目录 $ASDK_HOME/tools/,终端运行命令 ./android 进入 SDK Manager. 通过这个软件,可以选择下载相关的开发包,有不同版本的 SDK 及其源码文档等,注意:必须下载 Extras

  • Ubuntu16.04.4LTS安装mininet遇到的问题及解决方案

    Mininet Mininet 是一个轻量级软件定义网络和测试平台:它采用轻量级的虚拟化技术使一个单一的系统看起来像一个完整的网络运行相关的内核系统和用户代码,也可简单理解为 SDN 网络系统中的一种基于进程虚拟化平台,它支持 OpenFlow.OpenvSwith 等各种协议,Mininet 也可以模拟一个完整的网络主机.链接和交换机在同一台计算机上且有助于互动开发.测试和演示,尤其是那些使用 OpenFlow 和 SDN 技术:同时也可将此进程虚拟化的平台下代码迁移到真实的环境中. 下列图片

  • VMware14.0.0版本虚拟机安装Ubuntu16.04 LTS版本Linux系统图文教程

    VMware14.0.0 版本虚拟机安装Ubuntu16.04 LTS版本Linux系统多图详细步骤:一.前期准备工作 1.成功安装完成VMware14软件: 2.去Ubuntu官网下载Ubuntu16.0.4 LTS 版本的镜像文件. 二.安装步骤 1.打开VMware软件,选择<创建虚拟机>: 2.选择<自定义>,并点击<下一步>: 3.硬件兼容性选择<Workstation14.x>,并点击<下一步>: 4.选择<稍后安装操作系统&g

  • Ubuntu LTS服务器部署Jenkins详细介绍

    在Ubuntu 16.04 LTS服务器部署Jenkins 本文讲述怎样在Ubuntu 16.04 LTS服务器上部署Jenkins环境.Jenkins是基于Java语言开发的持续集成工具,为软件开发提供了一个持续集成服务.Jenkins是一个基于服务器的系统,运行在Servlet容器(比如Apache Tomcat)中.Jenkins支持SCM工具,包括AccuRev.CVS.Subversion.Git.Mercurial.Perforce.Clearcase和RTC等,可以只需基于Apac

  • ubuntu 16.04 LTS 安装mongodb 3.2.8教程

    mogodb 安装非常简单,直接解压就可以了. 1. 下载 MongoDB 官网上下载 mongodb-Linux-x86_64-ubuntu1604-3.2.8.tgz 2. 解压 解压命令: tar -zxvf mongodb-linux-x86_64-ubuntu1604-3.2.8.tgz 3. 创建目录结构 log: 用于保存mongodb产生的日志 data: 用于保存mongodb 数据库文件 bin: 用于保存mongodb 启动,关闭等脚本 conf: 用于保存mongodb

  • Ubuntu 18.04 LTS安装KVM虚拟机的方法步骤

    本文介绍了Ubuntu 18.04 LTS安装KVM虚拟机的方法步骤,分享给大家,具体如下: 前一阵使用在最新的Ubuntu 18.04上安装了KVM来虚拟一个小的VM集群,将主要过程和其中遇到的一些问题记录下来. 准备工作 首先需要检查一下CPU是否支持虚拟化,执行一下命令来检查/proc/cpuinfo文件中是否又虚拟化相关的字眼,如果有的话表明CPU支持虚拟化技术. egrep -c '(svm|vmx)' /proc/cpuinfo 上面命令执行结果如果返回0,表示CPU不支持虚拟化技术

  • 详解从Ubuntu 14.04 LTS版升级到Ubuntu 16.04 LTS

    从Ubuntu 14.04 LTS版升级到Ubuntu 16.04 LTS Ubuntu 16.04 (Xerial Xerus) Long Term Support版于最近发布了.要想了解它的新功能和新特性,就必须升级或安装这个新系统. 本文讲述怎样一步步从Ubuntu 14.04 LTS版升级到Ubuntu 16.04 LTS版.要注意在升级前做好重要数据的备份,以免造成数据损失.因为各种情况都有可能发生,升级也未必能够100%成功. 1.检查系统是否为最新 运行Ubuntu的更新管理器进行

随机推荐