Python実用:デスクトップ壁紙を毎日自動的に必ずトップページの画像に更新させる

7544 ワード

本編は目的を達成できるが、踏み込みが多く、更新後の文章を直接見ることができる.
シナリオ
デスクトップの壁紙が単調すぎて、適当な壁紙の出所がなくて、手動で更新するのは面倒だと感じています...発見するのは難しくなくて、毎日必ず対応するトップページのピクチャーはすべて更新して、その上ピクチャーは直接壁紙に持つことができます.だから、手を動かすと上の問題を解決することができます.
構想
  • 必須トップページ画像(python requestsライブラリ)
  • を取得する.
  • はローカルファイル
  • として保存する.
  • ファイル自動ネーミング:当日の日付に基づいて(python timeライブラリ)
  • コマンドで画像を壁紙(ubuntu環境、gnomeデスクトップ、python osライブラリ)
  • に設定
  • コードを毎日1回走らせる(crontabコマンド)
  • コード実装
    #!/usr/bin/python3
    # -*- "encoding: utf-8" -*-
    
    import requests
    import time
    import os
    
    #         ,           
    img_url = "https://area.sinaapp.com/bingImg/"
    #           
    date = time.localtime()
    year = date.tm_year
    month = date.tm_mon
    day = date.tm_mday
    filename = "bing_%s_%s_%s.jpg" % (year, month, day)
    
    #          
    r = requests.get(img_url)
    with open("/home/ubuntu/Wallpapers/%s"%filename, "wb") as f:
        f.write(r.content)
    
    #  shell      
    os.system("gsettings set org.gnome.desktop.background picture-uri 'file:///home/ubuntu/Wallpapers/%s'" % filename)
    

    次に、コードが正しく実行されるかどうかをテストします.
    コマンドラインで直接実行するこのコードは問題なく、画像は正常にダウンロードして保存することができる.壁紙は正常に交換されています
    ubuntu@ubuntu-X550VQ:~$ ./autoUpdateWallpapers.py
    

    ただし、このプログラムを毎日自分で実行することはできないでしょうから、cronanacronを利用してコードを毎日走らせておけばいいのです
    しかし最后にやっと発见して、crongsettingに対して/etc/cron.dailyに対して友好的ではありません—>だからもし间违いを试みたくないならば振り回したくないならば、下をスキップしてプログラムをcronに入れてください.dailyディレクトリの方法
    具体的な動作:
  • スクリプトに実行権限を付与
  • chmod 755 autoUpdateWallpapers.py
    
  • スクリプトをcron.*/ディレクトリの下に
  • 配置
    sudo mv ./autoUpdateWallpapers.py /etc/cron.daily/autoUpdateWallpapers
    
    sudo chown root /etc/cron.daily/autoUpdateWallpapers
    sudo chgrp root /etc/cron.daily/autoUpdateWallpapers
    

    スクリプトをcronに配置します.dailyでは実行されていません...次のいくつかの問題を確認しました.
  • run-partsディレクトリの下に置かれたファイルはrun-parts --test /etc/cron.daily/によって実行されるため、ファイル名にはドット(dots)などを持ち込めないなどの要求があるため、スクリプトをターゲットフォルダに移動した後、接尾辞を書かないでください.

  • (文書が仕様に適合しているかどうかを確認するコマンドrun-parts命令出力リストに適合しているかどうかを確認する)cron文書を詳細に表示する
  • このディレクトリの下のすべての実行可能ファイルの所有者はrootでなければならないので、スクリプトの所有者(owner)をrootに変更します.これは、As described above,the files under these directories have to be pass some sanity checks including the following:be executable,be owned by root、 not be writable by group or other and, if symlinks, point to files owned by root. Additionally, the file names must conform to the filename requirements of run-parts: they must be entirely made up of letters, digits and can only con‐ tain the special signs underscores (’_’) and hyphens (’-’). Any file that does not conform to these requirements will not be executed by run-parts. For example, any file containing dots will be ignored. This is done to prevent cron from running any of the files that are left by the Debian package management system when handling files in/etc/cron.d/as configuration files (i.e. files ending in .dpkg-dist, .dpkg-orig, and .dpkg-new).
  • cron.daily/のフォルダに入れるファイルシステムは、毎日1回実行することを保証し、具体的な時間はcrontabanacronの配置状況によって決定する.プロファイルはそれぞれ/etc/crontab/etc/anacrontabである.

  • 壁紙の交換に失敗しました...cronを借りません.daily/了,直接crontabでループタスクを設定する
  • スクリプトをcron.daily/フォルダから他のフォルダ、例えば/usr/bin/ディレクトリの下
  • に移動する.
  • $HOMEの下にshellスクリプトautoUpdateWallpapersを新規作成し、shellスクリプトによってpythonプログラム
  • を呼び出す.
    #!/bin/sh
    python3 /usr/bin/autoUpdateWallpapers.py
    
  • は、crontab -eを使用してshellスクリプトを現在のユーザのタイミングタスクリストに書き込む、この時点で時間が明確に決定される.欠点は、実行時間になってコンピュータが正常に動作しないと、今回のタスクがスキップされることである.逆に、cron.*/シリーズのフォルダの下に置くスクリプトは、anacronによって時間間隔の確認が行われ、「補完」することができる.
  • #      crontab -e   ,              
    0 22 * * * /home/ubuntu/autoUpdateWallpapers
    

    再度テストして、画像は正常にダウンロードすることができて、しかし壁紙はやはり交換していません
  • 最終的に1つのウェブサイトのコメントで問題点を見つけました:
  • gsettings won’t work from cron, however. you need to set the DBUS_SESSION_BUS_ADDRESS environment >variable in order for gsettings to work. You can do that by adding these two lines before gsettings PID=$(pgrep gnome-session) export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-) – willbradley Jan 13 at 0:25
  • 上記のように、shellスクリプトに2つのshellコマンド(具体的な意味はよくわかりません)
  • が追加されました.
    #!/bin/sh
    
    PID=$(pgrep gnome-session)
    export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)
    
    python3 /usr/bin/autoUpdateWallpapers.py
    
  • それから壁紙はやっと正常に毎日自動的に
  • を交換することができます